1
Fork 0

Test invalid define_opaques attributes

This commit is contained in:
Oli Scherer 2025-02-25 09:52:55 +00:00
parent cb4751d4b8
commit 3e4e65ee8b
12 changed files with 121 additions and 11 deletions

View file

@ -1683,16 +1683,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
let Some(define_opaque) = define_opaque.as_ref() else {
return;
};
let define_opaque = define_opaque
.iter()
// TODO: error reporting for non-local items being mentioned and tests that go through these code paths
.map(|(id, _path)| {
self.resolver
.get_partial_res(*id)
.unwrap()
.expect_full_res()
.def_id()
.expect_local()
let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
let res = self.resolver.get_partial_res(*id).unwrap();
let Some(did) = res.expect_full_res().opt_def_id() else {
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
return None;
};
let Some(did) = did.as_local() else {
self.dcx().span_err(
path.span,
"only opaque types defined in the local crate can be defined",
);
return None;
};
Some(did)
});
let define_opaque = self.arena.alloc_from_iter(define_opaque);
self.define_opaque = Some(define_opaque);

View file

@ -0,0 +1,5 @@
#![feature(type_alias_impl_trait)]
#[define_opaque(String)]
//~^ ERROR: only opaque types defined in the local crate can be defined
fn main() {}

View file

@ -0,0 +1,8 @@
error: only opaque types defined in the local crate can be defined
--> $DIR/foreign_type.rs:3:17
|
LL | #[define_opaque(String)]
| ^^^^^^
error: aborting due to 1 previous error

View file

@ -0,0 +1,13 @@
#![feature(type_alias_impl_trait)]
type Tait<T> = impl Sized;
//~^ ERROR: unconstrained opaque type
#[define_opaque(Tait::<()>)]
//~^ ERROR: expected unsuffixed literal
fn foo() {}
#[define_opaque(Tait<()>)]
//~^ ERROR: expected one of `(`, `,`, `::`, or `=`, found `<`
fn main() {}
//~^ ERROR: `main` function not found

View file

@ -0,0 +1,29 @@
error: expected unsuffixed literal, found `<`
--> $DIR/generics.rs:6:23
|
LL | #[define_opaque(Tait::<()>)]
| ^
error: expected one of `(`, `,`, `::`, or `=`, found `<`
--> $DIR/generics.rs:10:21
|
LL | #[define_opaque(Tait<()>)]
| ^ expected one of `(`, `,`, `::`, or `=`
error[E0601]: `main` function not found in crate `generics`
--> $DIR/generics.rs:12:13
|
LL | fn main() {}
| ^ consider adding a `main` function to `$DIR/generics.rs`
error: unconstrained opaque type
--> $DIR/generics.rs:3:16
|
LL | type Tait<T> = impl Sized;
| ^^^^^^^^^^
|
= note: `Tait` must be used in combination with a concrete type within the same crate
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0601`.

View file

@ -0,0 +1,5 @@
#![feature(type_alias_impl_trait)]
#[define_opaque(Boom)]
//~^ ERROR: cannot find type alias or associated type
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0412]: cannot find type alias or associated type with opaqaue types `Boom` in this scope
--> $DIR/invalid_path.rs:3:17
|
LL | #[define_opaque(Boom)]
| ^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0412`.

View file

@ -0,0 +1,5 @@
#![feature(type_alias_impl_trait)]
#[define_opaque]
//~^ ERROR: expected list of type aliases
fn main() {}

View file

@ -0,0 +1,8 @@
error: expected list of type aliases
--> $DIR/missing_parens.rs:3:1
|
LL | #[define_opaque]
| ^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View file

@ -0,0 +1,8 @@
//@ check-pass
#![feature(type_alias_impl_trait)]
type Thing = ();
#[define_opaque(Thing)]
fn main() {}

View file

@ -0,0 +1,7 @@
#![feature(type_alias_impl_trait)]
fn foo() {}
#[define_opaque(foo)]
//~^ ERROR: expected type alias or associated type with opaqaue types
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0573]: expected type alias or associated type with opaqaue types, found function `foo`
--> $DIR/non_type.rs:5:17
|
LL | #[define_opaque(foo)]
| ^^^ not a type alias or associated type with opaqaue types
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0573`.