Skip to content

Commit 3e4e65e

Browse files
committed
Test invalid define_opaques attributes
1 parent cb4751d commit 3e4e65e

File tree

12 files changed

+121
-11
lines changed

12 files changed

+121
-11
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1683,17 +1683,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
16831683
let Some(define_opaque) = define_opaque.as_ref() else {
16841684
return;
16851685
};
1686-
let define_opaque = define_opaque
1687-
.iter()
1688-
// TODO: error reporting for non-local items being mentioned and tests that go through these code paths
1689-
.map(|(id, _path)| {
1690-
self.resolver
1691-
.get_partial_res(*id)
1692-
.unwrap()
1693-
.expect_full_res()
1694-
.def_id()
1695-
.expect_local()
1696-
});
1686+
let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
1687+
let res = self.resolver.get_partial_res(*id).unwrap();
1688+
let Some(did) = res.expect_full_res().opt_def_id() else {
1689+
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
1690+
return None;
1691+
};
1692+
let Some(did) = did.as_local() else {
1693+
self.dcx().span_err(
1694+
path.span,
1695+
"only opaque types defined in the local crate can be defined",
1696+
);
1697+
return None;
1698+
};
1699+
Some(did)
1700+
});
16971701
let define_opaque = self.arena.alloc_from_iter(define_opaque);
16981702
self.define_opaque = Some(define_opaque);
16991703
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
#[define_opaque(String)]
4+
//~^ ERROR: only opaque types defined in the local crate can be defined
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: only opaque types defined in the local crate can be defined
2+
--> $DIR/foreign_type.rs:3:17
3+
|
4+
LL | #[define_opaque(String)]
5+
| ^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
type Tait<T> = impl Sized;
4+
//~^ ERROR: unconstrained opaque type
5+
6+
#[define_opaque(Tait::<()>)]
7+
//~^ ERROR: expected unsuffixed literal
8+
fn foo() {}
9+
10+
#[define_opaque(Tait<()>)]
11+
//~^ ERROR: expected one of `(`, `,`, `::`, or `=`, found `<`
12+
fn main() {}
13+
//~^ ERROR: `main` function not found
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: expected unsuffixed literal, found `<`
2+
--> $DIR/generics.rs:6:23
3+
|
4+
LL | #[define_opaque(Tait::<()>)]
5+
| ^
6+
7+
error: expected one of `(`, `,`, `::`, or `=`, found `<`
8+
--> $DIR/generics.rs:10:21
9+
|
10+
LL | #[define_opaque(Tait<()>)]
11+
| ^ expected one of `(`, `,`, `::`, or `=`
12+
13+
error[E0601]: `main` function not found in crate `generics`
14+
--> $DIR/generics.rs:12:13
15+
|
16+
LL | fn main() {}
17+
| ^ consider adding a `main` function to `$DIR/generics.rs`
18+
19+
error: unconstrained opaque type
20+
--> $DIR/generics.rs:3:16
21+
|
22+
LL | type Tait<T> = impl Sized;
23+
| ^^^^^^^^^^
24+
|
25+
= note: `Tait` must be used in combination with a concrete type within the same crate
26+
27+
error: aborting due to 4 previous errors
28+
29+
For more information about this error, try `rustc --explain E0601`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
#[define_opaque(Boom)]
4+
//~^ ERROR: cannot find type alias or associated type
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0412]: cannot find type alias or associated type with opaqaue types `Boom` in this scope
2+
--> $DIR/invalid_path.rs:3:17
3+
|
4+
LL | #[define_opaque(Boom)]
5+
| ^^^^ not found in this scope
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
#[define_opaque]
4+
//~^ ERROR: expected list of type aliases
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected list of type aliases
2+
--> $DIR/missing_parens.rs:3:1
3+
|
4+
LL | #[define_opaque]
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ check-pass
2+
3+
#![feature(type_alias_impl_trait)]
4+
5+
type Thing = ();
6+
7+
#[define_opaque(Thing)]
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
fn foo() {}
4+
5+
#[define_opaque(foo)]
6+
//~^ ERROR: expected type alias or associated type with opaqaue types
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0573]: expected type alias or associated type with opaqaue types, found function `foo`
2+
--> $DIR/non_type.rs:5:17
3+
|
4+
LL | #[define_opaque(foo)]
5+
| ^^^ not a type alias or associated type with opaqaue types
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0573`.

0 commit comments

Comments
 (0)