Skip to content

Commit

Permalink
Bless and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jan 23, 2025
1 parent 04d141b commit 6833c27
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 73 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4637,3 +4637,6 @@ fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Deb
}
DebugFn(f)
}

#[cfg(test)]
mod tests;
83 changes: 83 additions & 0 deletions compiler/rustc_hir/src/hir/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use rustc_span::def_id::DefIndex;

use super::*;

macro_rules! define_tests {
($($name:ident $kind:ident $variant:ident {$($init:tt)*})*) => {$(
#[test]
fn $name() {
let unambig = $kind::$variant::<'_, ()> { $($init)* };
let unambig_to_ambig = unsafe { std::mem::transmute::<_, $kind<'_, AmbigArg>>(unambig) };

assert!(matches!(&unambig_to_ambig, $kind::$variant { $($init)* }));

let ambig_to_unambig = unsafe { std::mem::transmute::<_, $kind<'_, ()>>(unambig_to_ambig) };

assert!(matches!(&ambig_to_unambig, $kind::$variant { $($init)* }));
}
)*};
}

define_tests! {
cast_never TyKind Never {}
cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }] }
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
cast_array TyKind Array {
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
hir_id: HirId::INVALID,
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
body: BodyId { hir_id: HirId::INVALID },
span: DUMMY_SP,
})}
}

cast_anon ConstArgKind Anon {
0: &AnonConst {
hir_id: HirId::INVALID,
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
body: BodyId { hir_id: HirId::INVALID },
span: DUMMY_SP,
}
}
}

#[test]
fn trait_object_roundtrips() {
trait_object_roundtrips_impl(TraitObjectSyntax::Dyn);
trait_object_roundtrips_impl(TraitObjectSyntax::DynStar);
trait_object_roundtrips_impl(TraitObjectSyntax::None);
}

fn trait_object_roundtrips_impl(syntax: TraitObjectSyntax) {
let unambig = TyKind::TraitObject::<'_, ()>(
&[],
TaggedRef::new(
&const {
Lifetime {
hir_id: HirId::INVALID,
ident: Ident::new(sym::name, DUMMY_SP),
res: LifetimeName::Static,
}
},
syntax,
),
);
let unambig_to_ambig = unsafe { std::mem::transmute::<_, TyKind<'_, AmbigArg>>(unambig) };

match unambig_to_ambig {
TyKind::TraitObject(_, tagged_ref) => {
assert!(tagged_ref.tag() == syntax)
}
_ => panic!("`TyKind::TraitObject` did not roundtrip"),
};

let ambig_to_unambig = unsafe { std::mem::transmute::<_, TyKind<'_, ()>>(unambig_to_ambig) };

match ambig_to_unambig {
TyKind::TraitObject(_, tagged_ref) => {
assert!(tagged_ref.tag() == syntax)
}
_ => panic!("`TyKind::TraitObject` did not roundtrip"),
};
}
9 changes: 9 additions & 0 deletions tests/ui/closures/binder/forbid_ambig_const_infers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(generic_arg_infer, closure_lifetime_binder)]

struct Foo<const N: usize>([u32; N]);

fn main() {
let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0[0] };
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
c(&Foo([1_u32; 1]));
}
10 changes: 10 additions & 0 deletions tests/ui/closures/binder/forbid_ambig_const_infers.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/forbid_ambig_const_infers.rs:6:33
|
LL | let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0[0] };
| ------- ^
| |
| `for<...>` is here

error: aborting due to 1 previous error

9 changes: 9 additions & 0 deletions tests/ui/closures/binder/forbid_ambig_type_infers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(generic_arg_infer, closure_lifetime_binder)]

struct Foo<T>(T);

fn main() {
let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0 };
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
c(&Foo(1_u32));
}
10 changes: 10 additions & 0 deletions tests/ui/closures/binder/forbid_ambig_type_infers.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/forbid_ambig_type_infers.rs:6:33
|
LL | let c = for<'a> |b: &'a Foo<_>| -> u32 { b.0 };
| ------- ^
| |
| `for<...>` is here

error: aborting due to 1 previous error

7 changes: 7 additions & 0 deletions tests/ui/closures/binder/forbid_const_infer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(generic_arg_infer, closure_lifetime_binder)]

fn main() {
let c = for<'a> |b: &'a [u32; _]| -> u32 { b[0] };
//~^ ERROR: implicit types in closure signatures are forbidden when `for<...>` is present
c(&[1_u32; 2]);
}
10 changes: 10 additions & 0 deletions tests/ui/closures/binder/forbid_const_infer.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: implicit types in closure signatures are forbidden when `for<...>` is present
--> $DIR/forbid_const_infer.rs:4:35
|
LL | let c = for<'a> |b: &'a [u32; _]| -> u32 { b[0] };
| ------- ^
| |
| `for<...>` is here

error: aborting due to 1 previous error

14 changes: 6 additions & 8 deletions tests/ui/const-generics/issues/issue-62878.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
LL + #![feature(adt_const_params)]
|

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

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0747, E0770.
For more information about an error, try `rustc --explain E0747`.
Some errors have detailed explanations: E0658, E0770.
For more information about an error, try `rustc --explain E0658`.
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-62878.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fn foo<const N: usize, const A: [u8; N]>() {}

fn main() {
foo::<_, { [1] }>();
//[min]~^ ERROR: type provided when a constant was expected
//[min]~^ ERROR: const arguments cannot yet be inferred with `_`
}
5 changes: 0 additions & 5 deletions tests/ui/did_you_mean/bad-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,6 @@ LL | fn foo<X: K<_, _>>(x: X) {}
| ^ ^ not allowed in type signatures
| |
| not allowed in type signatures
|
help: use type parameters instead
|
LL | fn foo<X: K<T, T>, T>(x: X) {}
| ~ ~ +++

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> $DIR/bad-assoc-ty.rs:54:34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ LL | let _y: [u8; _] = [0; 3];
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0747]: type provided when a constant was expected
error[E0658]: const arguments cannot yet be inferred with `_`
--> $DIR/feature-gate-generic_arg_infer.rs:18:20
|
LL | let _x = foo::<_>([1,2]);
LL | let _x = foo::<_>([1, 2]);
| ^
|
= help: const arguments cannot yet be inferred with `_`
help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
LL + #![feature(generic_arg_infer)]
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: using `_` for array lengths is unstable
--> $DIR/feature-gate-generic_arg_infer.rs:11:27
Expand All @@ -32,5 +30,4 @@ LL | let _x: [u8; 3] = [0; _];

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0658, E0747.
For more information about an error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0658`.
6 changes: 3 additions & 3 deletions tests/ui/feature-gates/feature-gate-generic_arg_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![cfg_attr(feature, feature(generic_arg_infer))]

fn foo<const N: usize>(_: [u8; N]) -> [u8; N] {
[0; N]
[0; N]
}

fn bar() {
Expand All @@ -15,7 +15,7 @@ fn bar() {
}

fn main() {
let _x = foo::<_>([1,2]);
//[normal]~^ ERROR: type provided when a constant was expected
let _x = foo::<_>([1, 2]);
//[normal]~^ ERROR: const arguments cannot yet be inferred with `_`
bar();
}
5 changes: 0 additions & 5 deletions tests/ui/generics/issue-79605.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
|
LL | impl X<'_, _> {}
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | impl<T> X<'_, T> {}
| +++ ~

error: aborting due to 1 previous error

Expand Down
7 changes: 0 additions & 7 deletions tests/ui/macros/macro-span-issue-116502.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ LL | T: Trait<m!()>;
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use type parameters instead
|
LL ~ U
LL | };
LL | }
LL ~ struct S<U>(m!(), T)
|

error: aborting due to 1 previous error

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/parser/issues/issue-14303-fncall.full.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0747]: type provided when a lifetime was expected
--> $DIR/issue-14303-fncall.rs:15:26
error[E0747]: placeholder provided when a lifetime was expected
--> $DIR/issue-14303-fncall.rs:12:77
|
LL | .collect::<Vec<S<_, 'a>>>();
| ^
LL | let _x = (*start..*end).map(|x| S { a: start, b: end }).collect::<Vec<S<_, 'a>>>();
| ^

error: aborting due to 1 previous error

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/parser/issues/issue-14303-fncall.generic_arg.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0747]: inferred provided when a lifetime was expected
--> $DIR/issue-14303-fncall.rs:15:26
error[E0747]: placeholder provided when a lifetime was expected
--> $DIR/issue-14303-fncall.rs:12:77
|
LL | .collect::<Vec<S<_, 'a>>>();
| ^
LL | let _x = (*start..*end).map(|x| S { a: start, b: end }).collect::<Vec<S<_, 'a>>>();
| ^

error: aborting due to 1 previous error

Expand Down
9 changes: 3 additions & 6 deletions tests/ui/parser/issues/issue-14303-fncall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
// we need the above to avoid ast borrowck failure in recovered code
#![cfg_attr(generic_arg, feature(generic_arg_infer))]


struct S<'a, T> {
a: &'a T,
b: &'a T,
}

fn foo<'a, 'b>(start: &'a usize, end: &'a usize) {
let _x = (*start..*end)
.map(|x| S { a: start, b: end })
.collect::<Vec<S<_, 'a>>>();
//[generic_arg]~^ ERROR inferred provided when a lifetime was expected
//[full]~^^ ERROR type provided when a lifetime was expected
let _x = (*start..*end).map(|x| S { a: start, b: end }).collect::<Vec<S<_, 'a>>>();
//[generic_arg]~^ ERROR placeholder provided when a lifetime was expected
//[full]~^^ ERROR placeholder provided when a lifetime was expected
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ error[E0282]: type annotations needed
LL | .sum::<_>()
| ^^^ cannot infer type of the type parameter `S` declared on the method `sum`
|
help: consider specifying the generic argument
|
LL | .sum::<S>()
| ~~~~~

error: aborting due to 2 previous errors

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/type-alias-impl-trait/issue-77179.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
--> $DIR/issue-77179.rs:18:25
|
LL | fn bar() -> Pointer<_>;
| ^
| |
| not allowed in type signatures
| help: use type parameters instead: `T`
| ^ not allowed in type signatures

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
type Pat<const START: u32, const END: u32> =
std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
//~^ ERROR type and const arguments are not allowed on const parameter `START`
//~| ERROR type arguments are not allowed on const parameter `END`
//~| ERROR generic arguments are not allowed on const parameter `END`
//~| ERROR associated item constraints are not allowed here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ note: const parameter `START` defined here
LL | type Pat<const START: u32, const END: u32> =
| ^^^^^

error[E0109]: type arguments are not allowed on const parameter `END`
error[E0109]: generic arguments are not allowed on const parameter `END`
--> $DIR/bad_const_generics_args_on_const_param.rs:5:64
|
LL | std::pat::pattern_type!(u32 is START::<(), i32, 2>..=END::<_, Assoc = ()>);
| --- ^ type argument not allowed
| --- ^ generic argument not allowed
| |
| not allowed on const parameter `END`
|
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/typeck/typeck_type_placeholder_item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -507,22 +507,12 @@ LL | impl BadTrait<_> for BadStruct<_> {}
| ^ ^ not allowed in type signatures
| |
| not allowed in type signatures
|
help: use type parameters instead
|
LL | impl<T> BadTrait<T> for BadStruct<T> {}
| +++ ~ ~

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> $DIR/typeck_type_placeholder_item.rs:162:34
|
LL | fn impl_trait() -> impl BadTrait<_> {
| ^ not allowed in type signatures
|
help: use type parameters instead
|
LL | fn impl_trait<T>() -> impl BadTrait<T> {
| +++ ~

error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
--> $DIR/typeck_type_placeholder_item.rs:167:25
Expand Down

0 comments on commit 6833c27

Please sign in to comment.