Skip to content

Commit 6a7ad4c

Browse files
committed
If MIR doesn't define the opaque also fail for RPITs
1 parent 1e7db2c commit 6a7ad4c

11 files changed

+105
-51
lines changed

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,13 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
389389
// the `concrete_opaque_types` table.
390390
Ty::new_error(tcx, guar)
391391
} else {
392-
// Fall back to the RPIT we inferred during HIR typeck
393-
if let Some(hir_opaque_ty) = hir_opaque_ty {
394-
hir_opaque_ty.ty
395-
} else {
396-
// We failed to resolve the opaque type or it
397-
// resolves to itself. We interpret this as the
398-
// no values of the hidden type ever being constructed,
399-
// so we can just make the hidden type be `!`.
400-
// For backwards compatibility reasons, we fall back to
401-
// `()` until we the diverging default is changed.
402-
Ty::new_diverging_default(tcx)
403-
}
392+
// We failed to resolve the opaque type or it
393+
// resolves to itself. We interpret this as the
394+
// no values of the hidden type ever being constructed,
395+
// so we can just make the hidden type be `!`.
396+
// For backwards compatibility reasons, we fall back to
397+
// `()` until we the diverging default is changed.
398+
Ty::new_diverging_default(tcx)
404399
}
405400
}
406401
}

tests/ui/borrowck/opaque-types-deadcode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ fn good_bye() -> ! {
2424
}
2525

2626
fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
27-
//~^ ERROR: Foo<'{erased}, '{erased}>
27+
//~^ ERROR: (): CallMeMaybe<DefId(0:23 ~ opaque_types_deadcode[3cb8]::foo::'a)_'a/#0, DefId(0:24 ~ opaque_types_deadcode[3cb8]::foo::'b)_'b/#1>` is not satisfied [E0277]
28+
//~| ERROR: ()
2829
good_bye();
2930
Foo(&(), &())
3031
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
error: Foo<'{erased}, '{erased}>
1+
error[E0277]: the trait bound `(): CallMeMaybe<DefId(0:23 ~ opaque_types_deadcode[3cb8]::foo::'a)_'a/#0, DefId(0:24 ~ opaque_types_deadcode[3cb8]::foo::'b)_'b/#1>` is not satisfied
2+
--> $DIR/opaque-types-deadcode.rs:26:25
3+
|
4+
LL | fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `CallMeMaybe<DefId(0:23 ~ opaque_types_deadcode[3cb8]::foo::'a)_'a/#0, DefId(0:24 ~ opaque_types_deadcode[3cb8]::foo::'b)_'b/#1>` is not implemented for `()`
6+
|
7+
= help: the trait `CallMeMaybe<DefId(0:16 ~ opaque_types_deadcode[3cb8]::{impl#0}::'a)_'a/#0, DefId(0:17 ~ opaque_types_deadcode[3cb8]::{impl#0}::'b)_'b/#1>` is implemented for `Foo<DefId(0:16 ~ opaque_types_deadcode[3cb8]::{impl#0}::'a)_'a/#0, DefId(0:17 ~ opaque_types_deadcode[3cb8]::{impl#0}::'b)_'b/#1>`
8+
9+
error: ()
210
--> $DIR/opaque-types-deadcode.rs:26:25
311
|
412
LL | fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
513
| ^^^^^^^^^^^^^^^^^^^^^^^^
614

7-
error: aborting due to 1 previous error
15+
error: aborting due to 2 previous errors
816

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

tests/ui/impl-trait/divergence.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//@ check-pass
2-
31
fn foo() -> impl MyTrait {
2+
//~^ ERROR: the trait bound `(): MyTrait` is not satisfied [E0277]
43
panic!();
54
MyStruct
65
}

tests/ui/impl-trait/divergence.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: the trait bound `(): MyTrait` is not satisfied
2+
--> $DIR/divergence.rs:1:13
3+
|
4+
LL | fn foo() -> impl MyTrait {
5+
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `()`
6+
|
7+
= help: the trait `MyTrait` is implemented for `MyStruct`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/impl_fn_associativity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@ run-pass
21
#![feature(impl_trait_in_fn_trait_return)]
32
use std::fmt::Debug;
43

@@ -7,6 +6,7 @@ fn f_debug() -> impl Fn() -> impl Debug {
76
}
87

98
fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug {
9+
//~^ ERROR: expected a `Fn()` closure, found `()` [E0277]
1010
|| f_debug()
1111
}
1212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: expected a `Fn()` closure, found `()`
2+
--> $DIR/impl_fn_associativity.rs:8:31
3+
|
4+
LL | fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn()` closure, found `()`
6+
|
7+
= help: the trait `Fn<()>` is not implemented for `()`
8+
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/nested_impl_trait.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
#![feature(impl_trait_in_fn_trait_return)]
22
use std::fmt::Debug;
33

4-
fn fine(x: impl Into<u32>) -> impl Into<u32> { x }
4+
fn fine(x: impl Into<u32>) -> impl Into<u32> {
5+
x
6+
}
57

6-
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
7-
//~^ ERROR nested `impl Trait` is not allowed
8-
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
8+
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> {
9+
//~^ ERROR nested `impl Trait` is not allowed
10+
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
11+
x
12+
}
913

1014
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1115
//~^ ERROR nested `impl Trait` is not allowed
1216
//~| `impl Trait` is not allowed in `fn` pointer
1317

14-
fn bad_in_arg_position(_: impl Into<impl Debug>) { }
18+
fn bad_in_arg_position(_: impl Into<impl Debug>) {}
1519
//~^ ERROR nested `impl Trait` is not allowed
1620

1721
struct X;
1822
impl X {
19-
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
20-
//~^ ERROR nested `impl Trait` is not allowed
21-
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
23+
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> {
24+
//~^ ERROR nested `impl Trait` is not allowed
25+
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
26+
x
27+
}
2228
}
2329

24-
fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {
30+
fn allowed_in_assoc_type() -> impl Iterator<Item = impl Fn()> {
2531
vec![|| println!("woot")].into_iter()
2632
}
2733

2834
fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
35+
//~^ ERROR: the trait bound `u32: From<()>` is not satisfied [E0277]
2936
|| 5u8
3037
}
3138

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error[E0666]: nested `impl Trait` is not allowed
2-
--> $DIR/nested_impl_trait.rs:6:56
2+
--> $DIR/nested_impl_trait.rs:8:56
33
|
4-
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
4+
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> {
55
| ----------^^^^^^^^^^-
66
| | |
77
| | nested `impl Trait` here
88
| outer `impl Trait`
99

1010
error[E0666]: nested `impl Trait` is not allowed
11-
--> $DIR/nested_impl_trait.rs:10:42
11+
--> $DIR/nested_impl_trait.rs:14:42
1212
|
1313
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1414
| ----------^^^^^^^^^^-
@@ -17,50 +17,65 @@ 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:14:37
20+
--> $DIR/nested_impl_trait.rs:18:37
2121
|
22-
LL | fn bad_in_arg_position(_: impl Into<impl Debug>) { }
22+
LL | fn bad_in_arg_position(_: impl Into<impl Debug>) {}
2323
| ----------^^^^^^^^^^-
2424
| | |
2525
| | nested `impl Trait` here
2626
| outer `impl Trait`
2727

2828
error[E0666]: nested `impl Trait` is not allowed
29-
--> $DIR/nested_impl_trait.rs:19:44
29+
--> $DIR/nested_impl_trait.rs:23:44
3030
|
31-
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
31+
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> {
3232
| ----------^^^^^^^^^^-
3333
| | |
3434
| | nested `impl Trait` here
3535
| outer `impl Trait`
3636

3737
error[E0562]: `impl Trait` is not allowed in `fn` pointer return types
38-
--> $DIR/nested_impl_trait.rs:10:32
38+
--> $DIR/nested_impl_trait.rs:14:32
3939
|
4040
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
4141
| ^^^^^^^^^^^^^^^^^^^^^
4242
|
4343
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
4444

4545
error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
46-
--> $DIR/nested_impl_trait.rs:6:46
46+
--> $DIR/nested_impl_trait.rs:8:46
4747
|
48-
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
48+
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> {
4949
| ^^^^^^^^^^^^^^^^^^^^^ the trait `From<impl Into<u32>>` is not implemented for `impl Into<u32>`, which is required by `impl Into<u32>: Into<impl Debug>`
5050
|
5151
= help: the trait `Into<U>` is implemented for `T`
5252
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`
5353

5454
error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
55-
--> $DIR/nested_impl_trait.rs:19:34
55+
--> $DIR/nested_impl_trait.rs:23:34
5656
|
57-
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
57+
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> {
5858
| ^^^^^^^^^^^^^^^^^^^^^ the trait `From<impl Into<u32>>` is not implemented for `impl Into<u32>`, which is required by `impl Into<u32>: Into<impl Debug>`
5959
|
6060
= help: the trait `Into<U>` is implemented for `T`
6161
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`
6262

63-
error: aborting due to 7 previous errors
63+
error[E0277]: the trait bound `u32: From<()>` is not satisfied
64+
--> $DIR/nested_impl_trait.rs:34:42
65+
|
66+
LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
67+
| ^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `u32`, which is required by `(): Into<u32>`
68+
|
69+
= help: the following other types implement trait `From<T>`:
70+
<u32 as From<Char>>
71+
<u32 as From<Ipv4Addr>>
72+
<u32 as From<bool>>
73+
<u32 as From<char>>
74+
<u32 as From<u16>>
75+
<u32 as From<u8>>
76+
= note: required for `()` to implement `Into<u32>`
77+
78+
error: aborting due to 8 previous errors
6479

6580
Some errors have detailed explanations: E0277, E0562, E0666.
6681
For more information about an error, try `rustc --explain E0277`.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![deny(unused_must_use)]
22

33
fn it() -> impl ExactSizeIterator<Item = ()> {
4+
//~^ ERROR the trait bound `(): ExactSizeIterator` is not satisfied [E0277]
45
let x: Box<dyn ExactSizeIterator<Item = ()>> = todo!();
56
x
67
}
78

89
fn main() {
910
it();
10-
//~^ ERROR unused implementer of `Iterator` that must be used
1111
}
+15-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
error: unused implementer of `Iterator` that must be used
2-
--> $DIR/unused-supertrait.rs:9:5
1+
error[E0277]: the trait bound `(): ExactSizeIterator` is not satisfied
2+
--> $DIR/unused-supertrait.rs:3:12
33
|
4-
LL | it();
5-
| ^^^^
4+
LL | fn it() -> impl ExactSizeIterator<Item = ()> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ExactSizeIterator` is not implemented for `()`
66
|
7-
= note: iterators are lazy and do nothing unless consumed
8-
note: the lint level is defined here
9-
--> $DIR/unused-supertrait.rs:1:9
10-
|
11-
LL | #![deny(unused_must_use)]
12-
| ^^^^^^^^^^^^^^^
7+
= help: the following other types implement trait `ExactSizeIterator`:
8+
&mut I
9+
Args
10+
ArgsOs
11+
ArrayChunksMut<'_, T, N>
12+
ArrayWindows<'_, T, N>
13+
Box<I, A>
14+
Chunks<'_, T>
15+
ChunksExact<'_, T>
16+
and 114 others
1317

1418
error: aborting due to 1 previous error
1519

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

0 commit comments

Comments
 (0)