Skip to content

Commit a745797

Browse files
committed
Stop generating inference vars for nested impl trait and let type equality handle it.
This means we stop supporting the case where a locally defined trait has only a single impl so we can always use that impl (see nested-tait-inference.rs).
1 parent 7bce50c commit a745797

File tree

9 files changed

+70
-50
lines changed

9 files changed

+70
-50
lines changed

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
21
use crate::infer::{InferCtxt, InferOk};
32
use crate::traits::{self, PredicateObligation, PredicateObligations};
43
use hir::def_id::{DefId, LocalDefId};
@@ -604,20 +603,6 @@ struct Instantiator<'a, 'tcx> {
604603
}
605604

606605
impl<'a, 'tcx> Instantiator<'a, 'tcx> {
607-
#[instrument(level = "trace", skip(self))]
608-
fn instantiate_opaque_types(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
609-
if let Some(ty) = self.fold_opaque_ty_new(ty, |infcx, span| {
610-
infcx.next_ty_var(TypeVariableOrigin {
611-
kind: TypeVariableOriginKind::TypeInference,
612-
span,
613-
})
614-
}) {
615-
return ty;
616-
}
617-
618-
ty
619-
}
620-
621606
fn fold_opaque_ty_new(
622607
&mut self,
623608
ty: Ty<'tcx>,
@@ -720,7 +705,6 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
720705
ty::Opaque(def_id2, substs2) if def_id == def_id2 && substs == substs2 => {
721706
ty_var
722707
}
723-
ty::Opaque(..) => self.instantiate_opaque_types(ty),
724708
_ => ty,
725709
},
726710
lt_op: |lt| lt,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ fn fine(x: impl Into<u32>) -> impl Into<u32> { x }
44

55
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
66
//~^ ERROR nested `impl Trait` is not allowed
7+
//~| ERROR `impl Into<u32>` doesn't implement `Debug`
78

89
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
910
//~^ ERROR nested `impl Trait` is not allowed
@@ -16,6 +17,7 @@ struct X;
1617
impl X {
1718
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
1819
//~^ ERROR nested `impl Trait` is not allowed
20+
//~| ERROR `impl Into<u32>` doesn't implement `Debug`
1921
}
2022

2123
fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {

src/test/ui/impl-trait/nested_impl_trait.stderr

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
88
| outer `impl Trait`
99

1010
error[E0666]: nested `impl Trait` is not allowed
11-
--> $DIR/nested_impl_trait.rs:8:42
11+
--> $DIR/nested_impl_trait.rs:9:42
1212
|
1313
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1414
| ----------^^^^^^^^^^-
@@ -17,7 +17,7 @@ LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1717
| outer `impl Trait`
1818

1919
error[E0666]: nested `impl Trait` is not allowed
20-
--> $DIR/nested_impl_trait.rs:12:37
20+
--> $DIR/nested_impl_trait.rs:13:37
2121
|
2222
LL | fn bad_in_arg_position(_: impl Into<impl Debug>) { }
2323
| ----------^^^^^^^^^^-
@@ -26,7 +26,7 @@ LL | fn bad_in_arg_position(_: impl Into<impl Debug>) { }
2626
| outer `impl Trait`
2727

2828
error[E0666]: nested `impl Trait` is not allowed
29-
--> $DIR/nested_impl_trait.rs:17:44
29+
--> $DIR/nested_impl_trait.rs:18:44
3030
|
3131
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
3232
| ----------^^^^^^^^^^-
@@ -35,18 +35,40 @@ LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
3535
| outer `impl Trait`
3636

3737
error[E0562]: `impl Trait` not allowed outside of function and method return types
38-
--> $DIR/nested_impl_trait.rs:8:32
38+
--> $DIR/nested_impl_trait.rs:9:32
3939
|
4040
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
4141
| ^^^^^^^^^^^^^^^^^^^^^
4242

4343
error[E0562]: `impl Trait` not allowed outside of function and method return types
44-
--> $DIR/nested_impl_trait.rs:25:42
44+
--> $DIR/nested_impl_trait.rs:27:42
4545
|
4646
LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
4747
| ^^^^^^^^^^^^^^
4848

49-
error: aborting due to 6 previous errors
49+
error[E0277]: `impl Into<u32>` doesn't implement `Debug`
50+
--> $DIR/nested_impl_trait.rs:5:70
51+
|
52+
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
53+
| ^ `impl Into<u32>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
54+
|
55+
help: consider further restricting this bound
56+
|
57+
LL | fn bad_in_ret_position(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
58+
| +++++++++++++++++
59+
60+
error[E0277]: `impl Into<u32>` doesn't implement `Debug`
61+
--> $DIR/nested_impl_trait.rs:18:58
62+
|
63+
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
64+
| ^ `impl Into<u32>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
65+
|
66+
help: consider further restricting this bound
67+
|
68+
LL | fn bad(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
69+
| +++++++++++++++++
70+
71+
error: aborting due to 8 previous errors
5072

51-
Some errors have detailed explanations: E0562, E0666.
52-
For more information about an error, try `rustc --explain E0562`.
73+
Some errors have detailed explanations: E0277, E0562, E0666.
74+
For more information about an error, try `rustc --explain E0277`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ impl<W> Trait<W> for () {}
1515

1616
fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> {
1717
()
18-
//~^ ERROR type annotations needed
18+
//~^ ERROR non-defining opaque type use
1919
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error[E0282]: type annotations needed
1+
error: non-defining opaque type use in defining scope
22
--> $DIR/bound_reduction2.rs:17:5
33
|
44
LL | ()
5-
| ^^ cannot infer type
5+
| ^^
6+
|
7+
note: used non-generic type `<T as TraitWithAssoc>::Assoc` for generic parameter
8+
--> $DIR/bound_reduction2.rs:9:10
9+
|
10+
LL | type Foo<V> = impl Trait<V>;
11+
| ^
612

713
error: could not find defining uses
814
--> $DIR/bound_reduction2.rs:9:15
@@ -12,4 +18,3 @@ LL | type Foo<V> = impl Trait<V>;
1218

1319
error: aborting due to 2 previous errors
1420

15-
For more information about this error, try `rustc --explain E0282`.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#![feature(type_alias_impl_trait)]
22
#![allow(dead_code)]
33

4-
// check-pass
5-
64
use std::fmt::Debug;
75

86
type FooX = impl Debug;
7+
//~^ ERROR could not find defining uses
98

109
trait Foo<A> { }
1110

1211
impl Foo<()> for () { }
1312

1413
fn foo() -> impl Foo<FooX> {
14+
// FIXME(type-alias-impl-trait): We could probably make this work.
1515
()
16+
//~^ ERROR: the trait bound `(): Foo<impl Debug>` is not satisfied
1617
}
1718

1819
fn main() { }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: the trait bound `(): Foo<impl Debug>` is not satisfied
2+
--> $DIR/nested-tait-inference.rs:15:5
3+
|
4+
LL | ()
5+
| ^^ the trait `Foo<impl Debug>` is not implemented for `()`
6+
|
7+
= help: the following implementations were found:
8+
<() as Foo<()>>
9+
10+
error: could not find defining uses
11+
--> $DIR/nested-tait-inference.rs:6:13
12+
|
13+
LL | type FooX = impl Debug;
14+
| ^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
17+
18+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/type-alias-impl-trait/nested-tait-inference2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ impl Foo<u32> for () {}
1313

1414
fn foo() -> impl Foo<FooX> {
1515
()
16-
//~^ ERROR: type annotations needed
17-
//~| ERROR: type annotations needed
16+
//~^ ERROR: the trait bound `(): Foo<impl Debug>` is not satisfied
1817
}
1918

2019
fn main() {}
Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
error[E0282]: type annotations needed
1+
error[E0277]: the trait bound `(): Foo<impl Debug>` is not satisfied
22
--> $DIR/nested-tait-inference2.rs:15:5
33
|
44
LL | ()
5-
| ^^ cannot infer type
6-
7-
error[E0283]: type annotations needed
8-
--> $DIR/nested-tait-inference2.rs:15:5
9-
|
10-
LL | ()
11-
| ^^ cannot infer type
12-
|
13-
note: multiple `impl`s satisfying `(): Foo<_>` found
14-
--> $DIR/nested-tait-inference2.rs:11:1
5+
| ^^ the trait `Foo<impl Debug>` is not implemented for `()`
156
|
16-
LL | impl Foo<()> for () {}
17-
| ^^^^^^^^^^^^^^^^^^^
18-
LL | impl Foo<u32> for () {}
19-
| ^^^^^^^^^^^^^^^^^^^^
7+
= help: the following implementations were found:
8+
<() as Foo<()>>
9+
<() as Foo<u32>>
2010

2111
error: could not find defining uses
2212
--> $DIR/nested-tait-inference2.rs:6:13
2313
|
2414
LL | type FooX = impl Debug;
2515
| ^^^^^^^^^^
2616

27-
error: aborting due to 3 previous errors
17+
error: aborting due to 2 previous errors
2818

29-
Some errors have detailed explanations: E0282, E0283.
30-
For more information about an error, try `rustc --explain E0282`.
19+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)