aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.rust/unsized2.rs
blob: ee5eaee04474d848b231e7af16df6f867239f250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2024-2025 Free Software Foundation, Inc.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_assignments)]

fn ignore<T>(x: T) { }

// A generic struct that is unsized if T is unsized.
pub struct MaybeUnsizedStruct<T: ?Sized> {
    pub regular: u32,
    pub rest: T,
}

// Same but without an ordinary part.
pub struct MaybeUnsizedStruct2<T: ?Sized> {
    value: T,
}

fn main() {
    // This struct is still sized because T is a fixed-length array
    let sized_struct = &MaybeUnsizedStruct {
        regular: 23,
        rest: [5, 6, 7],
    };

    // This struct is still sized because T is sized
    let nested_sized_struct = &MaybeUnsizedStruct {
        regular: 91,
        rest: MaybeUnsizedStruct {
            regular: 23,
            rest: [5, 6, 7],
        },
    };

    // This will be a fat pointer, containing the length of the final
    // field.
    let unsized_struct: &MaybeUnsizedStruct<[u32]> = sized_struct;

    // This will also be a fat pointer, containing the length of the
    // final field.
    let nested_unsized_struct:
        &MaybeUnsizedStruct<MaybeUnsizedStruct<[u32]>> = nested_sized_struct;

    let alpha: MaybeUnsizedStruct2<[u8; 4]> = MaybeUnsizedStruct2 { value: *b"abc\0" };
    let beta: &MaybeUnsizedStruct2<[u8]> = &alpha;

    let reference = &unsized_struct;

    ignore(sized_struct); // set breakpoint here
    ignore(nested_sized_struct);
    ignore(unsized_struct);
    ignore(nested_unsized_struct);
}