Skip to content

Commit 29c8732

Browse files
committed
Make the error for opaque types that have no hidden types a bit informative
1 parent 4d2e965 commit 29c8732

File tree

45 files changed

+115
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+115
-56
lines changed

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,12 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
724724
Some((_, ty)) => ty,
725725
None => {
726726
let span = tcx.def_span(def_id);
727-
tcx.sess.span_err(span, "could not find defining uses");
727+
let name = tcx.item_name(tcx.parent(def_id.to_def_id()).unwrap());
728+
let label = format!(
729+
"`{}` must be used in combination with a concrete type within the same module",
730+
name
731+
);
732+
tcx.sess.struct_span_err(span, "unconstrained opaque type").note(&label).emit();
728733
tcx.ty_error()
729734
}
730735
}

src/test/ui/generic-associated-types/issue-87258_a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait Trait2 {
1616

1717
impl<'c, S: Trait2> Trait2 for &'c mut S {
1818
type FooFuture<'a> = impl Trait1;
19-
//~^ ERROR could not find defining uses
19+
//~^ ERROR unconstrained opaque type
2020
fn foo<'a>() -> Self::FooFuture<'a> {
2121
Struct(unimplemented!())
2222
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/issue-87258_a.rs:18:26
33
|
44
LL | type FooFuture<'a> = impl Trait1;
55
| ^^^^^^^^^^^
6+
|
7+
= note: `FooFuture` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/generic-associated-types/issue-87258_b.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub trait Trait2 {
1515
}
1616

1717
type Helper<'xenon, 'yttrium, KABOOM: Trait2> = impl Trait1;
18-
//~^ ERROR could not find defining uses
18+
//~^ ERROR unconstrained opaque type
1919

2020
impl<'c, S: Trait2> Trait2 for &'c mut S {
2121
type FooFuture<'a> = Helper<'c, 'a, S>;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/issue-87258_b.rs:17:49
33
|
44
LL | type Helper<'xenon, 'yttrium, KABOOM: Trait2> = impl Trait1;
55
| ^^^^^^^^^^^
6+
|
7+
= note: `Helper` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/generic-associated-types/issue-88595.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct C;
1818
impl<'a> A<'a> for C {
1919
type B<'b> = impl Clone;
2020
//~^ ERROR: lifetime bound not satisfied
21-
//~| ERROR: could not find defining uses
21+
//~| ERROR: unconstrained opaque type
2222

2323
fn a(&'a self) -> Self::B<'a> {} //~ ERROR: non-defining opaque type use in defining scope
2424
}

src/test/ui/generic-associated-types/issue-88595.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ LL | impl<'a> A<'a> for C {
2929
LL | type B<'b> = impl Clone;
3030
| ^^
3131

32-
error: could not find defining uses
32+
error: unconstrained opaque type
3333
--> $DIR/issue-88595.rs:19:18
3434
|
3535
LL | type B<'b> = impl Clone;
3636
| ^^^^^^^^^^
37+
|
38+
= note: `B` must be used in combination with a concrete type within the same module
3739

3840
error: aborting due to 3 previous errors
3941

src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mod a {
44
type Foo = impl PartialEq<(Foo, i32)>;
5-
//~^ ERROR could not find defining uses
5+
//~^ ERROR unconstrained opaque type
66

77
struct Bar;
88

@@ -15,7 +15,7 @@ mod a {
1515

1616
mod b {
1717
type Foo = impl PartialEq<(Foo, i32)>;
18-
//~^ ERROR could not find defining uses
18+
//~^ ERROR unconstrained opaque type
1919

2020
struct Bar;
2121

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:4:16
33
|
44
LL | type Foo = impl PartialEq<(Foo, i32)>;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `Foo` must be used in combination with a concrete type within the same module
68

7-
error: could not find defining uses
9+
error: unconstrained opaque type
810
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:17:16
911
|
1012
LL | type Foo = impl PartialEq<(Foo, i32)>;
1113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: `Foo` must be used in combination with a concrete type within the same module
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/impl-trait/two_tait_defining_each_other2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(type_alias_impl_trait)]
22

33
type A = impl Foo;
4-
//~^ ERROR could not find defining uses
4+
//~^ ERROR unconstrained opaque type
55
type B = impl Foo;
66

77
trait Foo {}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/two_tait_defining_each_other2.rs:3:10
33
|
44
LL | type A = impl Foo;
55
| ^^^^^^^^
6+
|
7+
= note: `A` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/lint/inline-trait-and-foreign-items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Trait for () {
2323
type T = Self;
2424

2525
#[inline] //~ ERROR attribute should be applied to function or closure
26-
type U = impl Trait; //~ ERROR could not find defining uses
26+
type U = impl Trait; //~ ERROR unconstrained opaque type
2727
}
2828

2929
extern "C" {

src/test/ui/lint/inline-trait-and-foreign-items.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ LL | #[inline]
6161
LL | type T;
6262
| ------- not a function or closure
6363

64-
error: could not find defining uses
64+
error: unconstrained opaque type
6565
--> $DIR/inline-trait-and-foreign-items.rs:26:14
6666
|
6767
LL | type U = impl Trait;
6868
| ^^^^^^^^^^
69+
|
70+
= note: `U` must be used in combination with a concrete type within the same module
6971

7072
error: aborting due to 6 previous errors; 2 warnings emitted
7173

src/test/ui/never_type/impl_trait_fallback3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait T {
77
}
88

99
type Foo = impl T;
10-
//~^ ERROR could not find defining uses
10+
//~^ ERROR unconstrained opaque type
1111

1212
fn a() -> Foo {
1313
// This is not a defining use, it doesn't actually constrain the opaque type.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/impl_trait_fallback3.rs:9:12
33
|
44
LL | type Foo = impl T;
55
| ^^^^^^
6+
|
7+
= note: `Foo` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/save-analysis/issue-68621.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait Service {
1111
struct Struct;
1212

1313
impl Service for Struct {
14-
type Future = impl Trait; //~ ERROR: could not find defining uses
14+
type Future = impl Trait; //~ ERROR: unconstrained opaque type
1515
}
1616

1717
fn main() {}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/issue-68621.rs:14:19
33
|
44
LL | type Future = impl Trait;
55
| ^^^^^^^^^^
6+
|
7+
= note: `Future` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/type-alias-impl-trait/bound_reduction2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait TraitWithAssoc {
77
}
88

99
type Foo<V> = impl Trait<V>;
10-
//~^ ERROR could not find defining uses
10+
//~^ ERROR unconstrained opaque type
1111

1212
trait Trait<U> {}
1313

src/test/ui/type-alias-impl-trait/bound_reduction2.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ note: used non-generic type `<T as TraitWithAssoc>::Assoc` for generic parameter
1010
LL | type Foo<V> = impl Trait<V>;
1111
| ^
1212

13-
error: could not find defining uses
13+
error: unconstrained opaque type
1414
--> $DIR/bound_reduction2.rs:9:15
1515
|
1616
LL | type Foo<V> = impl Trait<V>;
1717
| ^^^^^^^^^^^^^
18+
|
19+
= note: `Foo` must be used in combination with a concrete type within the same module
1820

1921
error: aborting due to 2 previous errors
2022

src/test/ui/type-alias-impl-trait/bounds-are-checked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![feature(type_alias_impl_trait)]
55

66
type X<'a> = impl Into<&'static str> + From<&'a str>;
7-
//~^ ERROR could not find defining uses
7+
//~^ ERROR unconstrained opaque type
88

99
fn f<'a: 'static>(t: &'a str) -> X<'a> {
1010
//~^ WARNING unnecessary lifetime parameter

src/test/ui/type-alias-impl-trait/bounds-are-checked.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ LL | type X<'a> = impl Into<&'static str> + From<&'a str>;
1515
LL | t
1616
| ^
1717

18-
error: could not find defining uses
18+
error: unconstrained opaque type
1919
--> $DIR/bounds-are-checked.rs:6:14
2020
|
2121
LL | type X<'a> = impl Into<&'static str> + From<&'a str>;
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: `X` must be used in combination with a concrete type within the same module
2325

2426
error: aborting due to 2 previous errors; 1 warning emitted
2527

src/test/ui/type-alias-impl-trait/declared_but_never_defined.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
fn main() {}
44

55
// declared but never defined
6-
type Bar = impl std::fmt::Debug; //~ ERROR could not find defining uses
6+
type Bar = impl std::fmt::Debug; //~ ERROR unconstrained opaque type
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/declared_but_never_defined.rs:6:12
33
|
44
LL | type Bar = impl std::fmt::Debug;
55
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `Bar` must be used in combination with a concrete type within the same module
68

79
error: aborting due to previous error
810

src/test/ui/type-alias-impl-trait/declared_but_not_defined_in_scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {}
44

55
mod boo {
66
// declared in module but not defined inside of it
7-
pub type Boo = impl ::std::fmt::Debug; //~ ERROR could not find defining uses
7+
pub type Boo = impl ::std::fmt::Debug; //~ ERROR unconstrained opaque type
88
}
99

1010
fn bomp() -> boo::Boo {

src/test/ui/type-alias-impl-trait/declared_but_not_defined_in_scope.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: could not find defining uses
1+
error: unconstrained opaque type
22
--> $DIR/declared_but_not_defined_in_scope.rs:7:20
33
|
44
LL | pub type Boo = impl ::std::fmt::Debug;
55
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `Boo` must be used in combination with a concrete type within the same module
68

79
error[E0308]: mismatched types
810
--> $DIR/declared_but_not_defined_in_scope.rs:11:5

src/test/ui/type-alias-impl-trait/generic_duplicate_lifetime_param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
fn main() {}
44

55
type Two<'a, 'b> = impl std::fmt::Debug;
6-
//~^ ERROR could not find defining uses
6+
//~^ ERROR unconstrained opaque type
77

88
fn one<'a>(t: &'a ()) -> Two<'a, 'a> {
99
t

src/test/ui/type-alias-impl-trait/generic_duplicate_lifetime_param.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ note: lifetime used multiple times
1010
LL | type Two<'a, 'b> = impl std::fmt::Debug;
1111
| ^^ ^^
1212

13-
error: could not find defining uses
13+
error: unconstrained opaque type
1414
--> $DIR/generic_duplicate_lifetime_param.rs:5:20
1515
|
1616
LL | type Two<'a, 'b> = impl std::fmt::Debug;
1717
| ^^^^^^^^^^^^^^^^^^^^
18+
|
19+
= note: `Two` must be used in combination with a concrete type within the same module
1820

1921
error: aborting due to 2 previous errors
2022

src/test/ui/type-alias-impl-trait/generic_duplicate_param_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ fn main() {}
66

77
// test that unused generic parameters are ok
88
type TwoTys<T, U> = impl Debug;
9-
//~^ ERROR could not find defining uses
9+
//~^ ERROR unconstrained opaque type
1010
type TwoLifetimes<'a, 'b> = impl Debug;
11-
//~^ ERROR could not find defining uses
11+
//~^ ERROR unconstrained opaque type
1212
type TwoConsts<const X: usize, const Y: usize> = impl Debug;
13-
//~^ ERROR could not find defining uses
13+
//~^ ERROR unconstrained opaque type
1414

1515
fn one_ty<T: Debug>(t: T) -> TwoTys<T, T> {
1616
t

src/test/ui/type-alias-impl-trait/generic_duplicate_param_use.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ note: type used multiple times
1010
LL | type TwoTys<T, U> = impl Debug;
1111
| ^ ^
1212

13-
error: could not find defining uses
13+
error: unconstrained opaque type
1414
--> $DIR/generic_duplicate_param_use.rs:8:21
1515
|
1616
LL | type TwoTys<T, U> = impl Debug;
1717
| ^^^^^^^^^^
18+
|
19+
= note: `TwoTys` must be used in combination with a concrete type within the same module
1820

1921
error: non-defining opaque type use in defining scope
2022
--> $DIR/generic_duplicate_param_use.rs:21:5
@@ -28,11 +30,13 @@ note: lifetime used multiple times
2830
LL | type TwoLifetimes<'a, 'b> = impl Debug;
2931
| ^^ ^^
3032

31-
error: could not find defining uses
33+
error: unconstrained opaque type
3234
--> $DIR/generic_duplicate_param_use.rs:10:29
3335
|
3436
LL | type TwoLifetimes<'a, 'b> = impl Debug;
3537
| ^^^^^^^^^^
38+
|
39+
= note: `TwoLifetimes` must be used in combination with a concrete type within the same module
3640

3741
error: non-defining opaque type use in defining scope
3842
--> $DIR/generic_duplicate_param_use.rs:26:5
@@ -46,11 +50,13 @@ note: constant used multiple times
4650
LL | type TwoConsts<const X: usize, const Y: usize> = impl Debug;
4751
| ^ ^
4852

49-
error: could not find defining uses
53+
error: unconstrained opaque type
5054
--> $DIR/generic_duplicate_param_use.rs:12:50
5155
|
5256
LL | type TwoConsts<const X: usize, const Y: usize> = impl Debug;
5357
| ^^^^^^^^^^
58+
|
59+
= note: `TwoConsts` must be used in combination with a concrete type within the same module
5460

5561
error: aborting due to 6 previous errors
5662

src/test/ui/type-alias-impl-trait/generic_nondefining_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::fmt::Debug;
55
fn main() {}
66

77
type OneTy<T> = impl Debug;
8-
//~^ ERROR could not find defining uses
8+
//~^ ERROR unconstrained opaque type
99
type OneLifetime<'a> = impl Debug;
10-
//~^ ERROR could not find defining uses
10+
//~^ ERROR unconstrained opaque type
1111
type OneConst<const X: usize> = impl Debug;
12-
//~^ ERROR could not find defining uses
12+
//~^ ERROR unconstrained opaque type
1313

1414
// Not defining uses, because they doesn't define *all* possible generics.
1515

0 commit comments

Comments
 (0)