Skip to content

Commit a98b534

Browse files
committed
typeck/type_of: don't ignore incorrect defining uses of opaque types.
1 parent 6465113 commit a98b534

16 files changed

+48
-67
lines changed

src/librustc_typeck/collect/type_of.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
405405

406406
let opaque_generics = self.tcx.generics_of(self.def_id);
407407
let mut used_params: FxHashSet<ty::ParamTy> = FxHashSet::default();
408-
let mut has_errors = false;
408+
let mut duplicate_params: FxHashSet<ty::ParamTy> = FxHashSet::default();
409409
for (i, arg) in substs.iter().enumerate() {
410410
// FIXME(eddyb) enforce lifetime and const param 1:1 mapping.
411411
if let GenericArgKind::Type(ty) = arg.unpack() {
412412
if let ty::Param(p) = ty.kind {
413-
if !used_params.insert(p) {
413+
if !used_params.insert(p) && duplicate_params.insert(p) {
414414
// There was already an entry for `p`, meaning a generic parameter
415415
// was used twice.
416416
self.tcx.sess.span_err(
@@ -421,7 +421,6 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
421421
p,
422422
),
423423
);
424-
return;
425424
}
426425
} else {
427426
let param = opaque_generics.param_at(i, self.tcx);
@@ -435,15 +434,10 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
435434
arg,
436435
),
437436
);
438-
has_errors = true;
439437
}
440438
}
441439
}
442440

443-
if has_errors {
444-
return;
445-
}
446-
447441
if let Some((prev_span, prev_ty)) = self.found {
448442
if *concrete_type != prev_ty {
449443
debug!("find_opaque_ty_constraints: span={:?}", span);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ trait TraitWithAssoc {
88
}
99

1010
type Foo<V> = impl Trait<V>;
11-
//~^ ERROR could not find defining uses
12-
//~| ERROR the trait bound `T: TraitWithAssoc` is not satisfied
11+
//~^ ERROR the trait bound `T: TraitWithAssoc` is not satisfied
1312

1413
trait Trait<U> {}
1514

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ LL | fn foo_desugared<T: TraitWithAssoc + TraitWithAssoc>(_: T) -> Foo<T::Assoc>
1010
| ^^^^^^^^^^^^^^^^
1111

1212
error: defining opaque type use does not fully define opaque type: generic parameter `V` is specified as concrete type `<T as TraitWithAssoc>::Assoc`
13-
--> $DIR/bound_reduction2.rs:18:1
13+
--> $DIR/bound_reduction2.rs:17:1
1414
|
1515
LL | / fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> {
1616
LL | | ()
1717
LL | | }
1818
| |_^
1919

20-
error: could not find defining uses
21-
--> $DIR/bound_reduction2.rs:10:1
22-
|
23-
LL | type Foo<V> = impl Trait<V>;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
26-
error: aborting due to 3 previous errors
20+
error: aborting due to 2 previous errors
2721

2822
For more information about this error, try `rustc --explain E0277`.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ fn main() {}
66

77
// test that unused generic parameters are ok
88
type Two<T, U> = impl Debug;
9-
//~^ could not find defining uses
109

1110
fn one<T: Debug>(t: T) -> Two<T, T> {
1211
//~^ ERROR defining opaque type use restricts opaque type
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
error: defining opaque type use restricts opaque type by using the generic parameter `T` twice
2-
--> $DIR/generic_duplicate_param_use.rs:11:1
2+
--> $DIR/generic_duplicate_param_use.rs:10:1
33
|
44
LL | / fn one<T: Debug>(t: T) -> Two<T, T> {
55
LL | |
66
LL | | t
77
LL | | }
88
| |_^
99

10-
error: could not find defining uses
11-
--> $DIR/generic_duplicate_param_use.rs:8:1
12-
|
13-
LL | type Two<T, U> = impl Debug;
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
16-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
1711

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ fn main() {}
88
type Two<T, U> = impl Debug;
99

1010
fn one<T: Debug>(t: T) -> Two<T, T> {
11-
//~^ defining opaque type use restricts opaque type
11+
//~^ ERROR defining opaque type use restricts opaque type
1212
t
1313
}
1414

1515
fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
16+
//~^ ERROR concrete type differs from previous defining opaque type use
1617
t
1718
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,23 @@ LL | | t
77
LL | | }
88
| |_^
99

10-
error: aborting due to previous error
10+
error: concrete type differs from previous defining opaque type use
11+
--> $DIR/generic_duplicate_param_use2.rs:15:1
12+
|
13+
LL | / fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
14+
LL | |
15+
LL | | t
16+
LL | | }
17+
| |_^ expected `U`, got `T`
18+
|
19+
note: previous use here
20+
--> $DIR/generic_duplicate_param_use2.rs:10:1
21+
|
22+
LL | / fn one<T: Debug>(t: T) -> Two<T, T> {
23+
LL | |
24+
LL | | t
25+
LL | | }
26+
| |_^
27+
28+
error: aborting due to 2 previous errors
1129

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ fn main() {}
88
type Two<T, U> = impl Debug;
99

1010
fn one<T: Debug>(t: T) -> Two<T, T> {
11-
//~^ defining opaque type use restricts opaque type
11+
//~^ ERROR defining opaque type use restricts opaque type
1212
t
1313
}
1414

1515
fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
16+
//~^ ERROR concrete type differs from previous defining opaque type use
1617
t
1718
}
1819

1920
fn three<T, U: Debug>(_: T, u: U) -> Two<T, U> {
20-
//~^ concrete type differs from previous defining opaque type use
2121
u
2222
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ LL | | }
88
| |_^
99

1010
error: concrete type differs from previous defining opaque type use
11-
--> $DIR/generic_duplicate_param_use3.rs:19:1
11+
--> $DIR/generic_duplicate_param_use3.rs:15:1
1212
|
13-
LL | / fn three<T, U: Debug>(_: T, u: U) -> Two<T, U> {
13+
LL | / fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
1414
LL | |
15-
LL | | u
15+
LL | | t
1616
LL | | }
17-
| |_^ expected `T`, got `U`
17+
| |_^ expected `U`, got `T`
1818
|
1919
note: previous use here
20-
--> $DIR/generic_duplicate_param_use3.rs:15:1
20+
--> $DIR/generic_duplicate_param_use3.rs:10:1
2121
|
22-
LL | / fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
22+
LL | / fn one<T: Debug>(t: T) -> Two<T, T> {
23+
LL | |
2324
LL | | t
2425
LL | | }
2526
| |_^

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

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

55
type Cmp<T> = impl 'static;
6-
//~^ ERROR could not find defining uses
7-
//~^^ ERROR: at least one trait must be specified
6+
//~^ ERROR: at least one trait must be specified
87

98

109
// not a defining use, because it doesn't define *all* possible generics

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ LL | type Cmp<T> = impl 'static;
55
| ^^^^^^^^^^^^
66

77
error: defining opaque type use does not fully define opaque type: generic parameter `T` is specified as concrete type `u32`
8-
--> $DIR/generic_nondefining_use.rs:11:1
8+
--> $DIR/generic_nondefining_use.rs:10:1
99
|
1010
LL | / fn cmp() -> Cmp<u32> {
1111
LL | | 5u32
1212
LL | | }
1313
| |_^
1414

15-
error: could not find defining uses
16-
--> $DIR/generic_nondefining_use.rs:5:1
17-
|
18-
LL | type Cmp<T> = impl 'static;
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
20-
21-
error: aborting due to 3 previous errors
15+
error: aborting due to 2 previous errors
2216

src/test/ui/type-alias-impl-trait/issue-60564.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ trait IterBits {
66
}
77

88
type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
9-
//~^ ERROR could not find defining uses
109

1110
impl<T: Copy, E> IterBits for T
1211
where
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: defining opaque type use does not fully define opaque type: generic parameter `I` is specified as concrete type `u8`
2-
--> $DIR/issue-60564.rs:20:5
2+
--> $DIR/issue-60564.rs:19:5
33
|
44
LL | / fn iter_bits(self, n: u8) -> Self::BitsIter {
55
LL | |
@@ -9,11 +9,5 @@ LL | | .map(move |shift| ((self >> T::from(shift)) & T::from(1)).try
99
LL | | }
1010
| |_____^
1111

12-
error: could not find defining uses
13-
--> $DIR/issue-60564.rs:8:1
14-
|
15-
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
18-
error: aborting due to 2 previous errors
12+
error: aborting due to previous error
1913

src/test/ui/type-alias-impl-trait/issue-68368-non-defining-use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#![feature(type_alias_impl_trait)]
66
trait Trait<T> {}
7-
type Alias<'a, U> = impl Trait<U>; //~ ERROR could not find defining uses
7+
type Alias<'a, U> = impl Trait<U>;
88
fn f<'a>() -> Alias<'a, ()> {}
99
//~^ ERROR defining opaque type use does not fully define opaque type: generic parameter `U`
1010

src/test/ui/type-alias-impl-trait/issue-68368-non-defining-use.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: defining opaque type use does not fully define opaque type: generic param
44
LL | fn f<'a>() -> Alias<'a, ()> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: could not find defining uses
8-
--> $DIR/issue-68368-non-defining-use.rs:7:1
9-
|
10-
LL | type Alias<'a, U> = impl Trait<U>;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ LL | | }
1616
| |_^ expected `(T, i8)`, got `(T, <U as Bar>::Blub)`
1717
|
1818
note: previous use here
19-
--> $DIR/not_a_defining_use.rs:14:1
19+
--> $DIR/not_a_defining_use.rs:9:1
2020
|
21-
LL | / fn three<T: Debug, U>(t: T) -> Two<T, U> {
22-
LL | | (t, 5i8)
21+
LL | / fn two<T: Debug>(t: T) -> Two<T, u32> {
22+
LL | |
23+
LL | | (t, 4i8)
2324
LL | | }
2425
| |_^
2526

0 commit comments

Comments
 (0)