Skip to content

Commit 97067f9

Browse files
committed
Bless and add tests
1 parent 4f1c2ea commit 97067f9

23 files changed

+172
-73
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4637,3 +4637,6 @@ fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Deb
46374637
}
46384638
DebugFn(f)
46394639
}
4640+
4641+
#[cfg(test)]
4642+
mod tests;

compiler/rustc_hir/src/hir/tests.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use rustc_span::def_id::DefIndex;
2+
3+
use super::*;
4+
5+
macro_rules! define_tests {
6+
($($name:ident $kind:ident $variant:ident {$($init:tt)*})*) => {$(
7+
#[test]
8+
fn $name() {
9+
let unambig = $kind::$variant::<'_, ()> { $($init)* };
10+
let unambig_to_ambig = unsafe { std::mem::transmute::<_, $kind<'_, AmbigArg>>(unambig) };
11+
12+
assert!(matches!(&unambig_to_ambig, $kind::$variant { $($init)* }));
13+
14+
let ambig_to_unambig = unsafe { std::mem::transmute::<_, $kind<'_, ()>>(unambig_to_ambig) };
15+
16+
assert!(matches!(&ambig_to_unambig, $kind::$variant { $($init)* }));
17+
}
18+
)*};
19+
}
20+
21+
define_tests! {
22+
cast_never TyKind Never {}
23+
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }] }
24+
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
25+
cast_array TyKind Array {
26+
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
27+
1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
28+
hir_id: HirId::INVALID,
29+
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
30+
body: BodyId { hir_id: HirId::INVALID },
31+
span: DUMMY_SP,
32+
})}
33+
}
34+
35+
cast_anon ConstArgKind Anon {
36+
0: &AnonConst {
37+
hir_id: HirId::INVALID,
38+
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
39+
body: BodyId { hir_id: HirId::INVALID },
40+
span: DUMMY_SP,
41+
}
42+
}
43+
}
44+
45+
#[test]
46+
fn trait_object_roundtrips() {
47+
trait_object_roundtrips_impl(TraitObjectSyntax::Dyn);
48+
trait_object_roundtrips_impl(TraitObjectSyntax::DynStar);
49+
trait_object_roundtrips_impl(TraitObjectSyntax::None);
50+
}
51+
52+
fn trait_object_roundtrips_impl(syntax: TraitObjectSyntax) {
53+
let unambig = TyKind::TraitObject::<'_, ()>(
54+
&[],
55+
TaggedRef::new(
56+
&const {
57+
Lifetime {
58+
hir_id: HirId::INVALID,
59+
ident: Ident::new(sym::name, DUMMY_SP),
60+
res: LifetimeName::Static,
61+
}
62+
},
63+
syntax,
64+
),
65+
);
66+
let unambig_to_ambig = unsafe { std::mem::transmute::<_, TyKind<'_, AmbigArg>>(unambig) };
67+
68+
match unambig_to_ambig {
69+
TyKind::TraitObject(_, tagged_ref) => {
70+
assert!(tagged_ref.tag() == syntax)
71+
}
72+
_ => panic!("`TyKind::TraitObject` did not roundtrip"),
73+
};
74+
75+
let ambig_to_unambig = unsafe { std::mem::transmute::<_, TyKind<'_, ()>>(unambig_to_ambig) };
76+
77+
match ambig_to_unambig {
78+
TyKind::TraitObject(_, tagged_ref) => {
79+
assert!(tagged_ref.tag() == syntax)
80+
}
81+
_ => panic!("`TyKind::TraitObject` did not roundtrip"),
82+
};
83+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(generic_arg_infer, closure_lifetime_binder)]
2+
3+
struct Foo<const N: usize>([u32; N]);
4+
5+
fn main() {
6+
let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0[0] };
7+
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
8+
c(&Foo([1_u32; 1]));
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: implicit types in closure signatures are forbidden when `for<...>` is present
2+
--> $DIR/forbid_ambig_const_infers.rs:6:33
3+
|
4+
LL | let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0[0] };
5+
| ------- ^
6+
| |
7+
| `for<...>` is here
8+
9+
error: aborting due to 1 previous error
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(generic_arg_infer, closure_lifetime_binder)]
2+
3+
struct Foo<T>(T);
4+
5+
fn main() {
6+
let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0 };
7+
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
8+
c(&Foo(1_u32));
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: implicit types in closure signatures are forbidden when `for<...>` is present
2+
--> $DIR/forbid_ambig_type_infers.rs:6:33
3+
|
4+
LL | let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0 };
5+
| ------- ^
6+
| |
7+
| `for<...>` is here
8+
9+
error: aborting due to 1 previous error
10+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(generic_arg_infer, closure_lifetime_binder)]
2+
3+
fn main() {
4+
let c = for<'a> |b: &'a [u32; _]| -> u32 { b[0] };
5+
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
6+
c(&[1_u32; 2]);
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: implicit types in closure signatures are forbidden when `for<...>` is present
2+
--> $DIR/forbid_const_infer.rs:4:35
3+
|
4+
LL | let c = for<'a> |b: &'a [u32; _]| -> u32 { b[0] };
5+
| ------- ^
6+
| |
7+
| `for<...>` is here
8+
9+
error: aborting due to 1 previous error
10+

tests/ui/const-generics/issues/issue-62878.min.stderr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
1818
LL + #![feature(adt_const_params)]
1919
|
2020

21-
error[E0747]: type provided when a constant was expected
21+
error[E0658]: const arguments cannot yet be inferred with `_`
2222
--> $DIR/issue-62878.rs:10:11
2323
|
2424
LL | foo::<_, { [1] }>();
2525
| ^
2626
|
27-
= help: const arguments cannot yet be inferred with `_`
28-
help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
29-
|
30-
LL + #![feature(generic_arg_infer)]
31-
|
27+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
28+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3230

3331
error: aborting due to 3 previous errors
3432

35-
Some errors have detailed explanations: E0747, E0770.
36-
For more information about an error, try `rustc --explain E0747`.
33+
Some errors have detailed explanations: E0658, E0770.
34+
For more information about an error, try `rustc --explain E0658`.

tests/ui/const-generics/issues/issue-62878.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fn foo<const N: usize, const A: [u8; N]>() {}
88

99
fn main() {
1010
foo::<_, { [1] }>();
11-
//[min]~^ ERROR: type provided when a constant was expected
11+
//[min]~^ ERROR: const arguments cannot yet be inferred with `_`
1212
}

0 commit comments

Comments
 (0)