Skip to content

Commit 3e009ba

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

37 files changed

+330
-179
lines changed

Diff for: compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ hir_analysis_typeof_reserved_keyword_used =
512512
hir_analysis_unconstrained_opaque_type = unconstrained opaque type
513513
.note = `{$name}` must be used in combination with a concrete type within the same {$what}
514514
515+
hir_analysis_undefined_opaque_type = undefined opaque type
516+
515517
hir_analysis_unnamed_fields_repr_field_defined = unnamed field defined here
516518
517519
hir_analysis_unnamed_fields_repr_field_missing_repr_c =

Diff for: compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::hir::nested_filter;
88
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
99
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};
1010

11-
use crate::errors::{TaitForwardCompat, TypeOf, UnconstrainedOpaqueType};
11+
use crate::errors::{TaitForwardCompat, TypeOf, UnconstrainedOpaqueType, UndefinedOpaqueType};
1212

1313
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
1414
let mut res = Ok(());
@@ -389,9 +389,10 @@ 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
392+
// Error if we couldn't define the RPIT during MIR borrowck
393+
if hir_opaque_ty.is_some() {
394+
let err = tcx.dcx().emit_err(UndefinedOpaqueType { span: tcx.def_span(def_id) });
395+
Ty::new_error(tcx, err)
395396
} else {
396397
// We failed to resolve the opaque type or it
397398
// resolves to itself. We interpret this as the

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ pub struct UnconstrainedOpaqueType {
380380
pub what: &'static str,
381381
}
382382

383+
#[derive(Diagnostic)]
384+
#[diag(hir_analysis_undefined_opaque_type)]
385+
pub struct UndefinedOpaqueType {
386+
#[primary_span]
387+
pub span: Span,
388+
}
389+
383390
#[derive(Diagnostic)]
384391
#[diag(hir_analysis_tait_forward_compat)]
385392
#[note]

Diff for: 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: {type error}
28+
//~| ERROR: undefined opaque type
2829
good_bye();
2930
Foo(&(), &())
3031
}

Diff for: tests/ui/borrowck/opaque-types-deadcode.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error: Foo<'{erased}, '{erased}>
1+
error: undefined opaque type
22
--> $DIR/opaque-types-deadcode.rs:26:25
33
|
44
LL | fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to 1 previous error
7+
error: {type error}
8+
--> $DIR/opaque-types-deadcode.rs:26:25
9+
|
10+
LL | fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

Diff for: tests/ui/delegation/not-supported.rs

+2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ mod opaque {
7070

7171
pub fn opaque_arg(_: impl Trait) -> i32 { 0 }
7272
pub fn opaque_ret() -> impl Trait { unimplemented!() }
73+
//~^ ERROR undefined opaque type
7374
}
7475
reuse to_reuse::opaque_arg;
7576
//~^ ERROR delegation with early bound generics is not supported yet
7677

7778
trait ToReuse {
7879
fn opaque_ret() -> impl Trait { unimplemented!() }
80+
//~^ ERROR undefined opaque type
7981
}
8082

8183
// FIXME: Inherited `impl Trait`s create query cycles when used inside trait impls.

Diff for: tests/ui/delegation/not-supported.stderr

+27-15
Original file line numberDiff line numberDiff line change
@@ -107,62 +107,74 @@ LL | reuse Trait::foo2 { &self.0 }
107107
| ^^^^
108108

109109
error: delegation with early bound generics is not supported yet
110-
--> $DIR/not-supported.rs:74:21
110+
--> $DIR/not-supported.rs:75:21
111111
|
112112
LL | pub fn opaque_arg(_: impl Trait) -> i32 { 0 }
113113
| --------------------------------------- callee defined here
114114
...
115115
LL | reuse to_reuse::opaque_arg;
116116
| ^^^^^^^^^^
117117

118-
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/not-supported.rs:82:5: 82:24>::{synthetic#0}`
119-
--> $DIR/not-supported.rs:83:25
118+
error: undefined opaque type
119+
--> $DIR/not-supported.rs:79:28
120+
|
121+
LL | fn opaque_ret() -> impl Trait { unimplemented!() }
122+
| ^^^^^^^^^^
123+
124+
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/not-supported.rs:84:5: 84:24>::{synthetic#0}`
125+
--> $DIR/not-supported.rs:85:25
120126
|
121127
LL | reuse to_reuse::opaque_ret;
122128
| ^^^^^^^^^^
123129
|
124130
note: ...which requires comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process...
125-
--> $DIR/not-supported.rs:83:25
131+
--> $DIR/not-supported.rs:85:25
126132
|
127133
LL | reuse to_reuse::opaque_ret;
128134
| ^^^^^^^^^^
129-
= note: ...which again requires computing type of `opaque::<impl at $DIR/not-supported.rs:82:5: 82:24>::{synthetic#0}`, completing the cycle
130-
note: cycle used when checking that `opaque::<impl at $DIR/not-supported.rs:82:5: 82:24>` is well-formed
131-
--> $DIR/not-supported.rs:82:5
135+
= note: ...which again requires computing type of `opaque::<impl at $DIR/not-supported.rs:84:5: 84:24>::{synthetic#0}`, completing the cycle
136+
note: cycle used when checking that `opaque::<impl at $DIR/not-supported.rs:84:5: 84:24>` is well-formed
137+
--> $DIR/not-supported.rs:84:5
132138
|
133139
LL | impl ToReuse for u8 {
134140
| ^^^^^^^^^^^^^^^^^^^
135141
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
136142

137-
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/not-supported.rs:85:5: 85:25>::{synthetic#0}`
138-
--> $DIR/not-supported.rs:86:24
143+
error: undefined opaque type
144+
--> $DIR/not-supported.rs:72:32
145+
|
146+
LL | pub fn opaque_ret() -> impl Trait { unimplemented!() }
147+
| ^^^^^^^^^^
148+
149+
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/not-supported.rs:87:5: 87:25>::{synthetic#0}`
150+
--> $DIR/not-supported.rs:88:24
139151
|
140152
LL | reuse ToReuse::opaque_ret;
141153
| ^^^^^^^^^^
142154
|
143155
note: ...which requires comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process...
144-
--> $DIR/not-supported.rs:86:24
156+
--> $DIR/not-supported.rs:88:24
145157
|
146158
LL | reuse ToReuse::opaque_ret;
147159
| ^^^^^^^^^^
148-
= note: ...which again requires computing type of `opaque::<impl at $DIR/not-supported.rs:85:5: 85:25>::{synthetic#0}`, completing the cycle
149-
note: cycle used when checking that `opaque::<impl at $DIR/not-supported.rs:85:5: 85:25>` is well-formed
150-
--> $DIR/not-supported.rs:85:5
160+
= note: ...which again requires computing type of `opaque::<impl at $DIR/not-supported.rs:87:5: 87:25>::{synthetic#0}`, completing the cycle
161+
note: cycle used when checking that `opaque::<impl at $DIR/not-supported.rs:87:5: 87:25>` is well-formed
162+
--> $DIR/not-supported.rs:87:5
151163
|
152164
LL | impl ToReuse for u16 {
153165
| ^^^^^^^^^^^^^^^^^^^^
154166
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
155167

156168
error: recursive delegation is not supported yet
157-
--> $DIR/not-supported.rs:99:22
169+
--> $DIR/not-supported.rs:101:22
158170
|
159171
LL | pub reuse to_reuse2::foo;
160172
| --- callee defined here
161173
...
162174
LL | reuse to_reuse1::foo;
163175
| ^^^
164176

165-
error: aborting due to 16 previous errors
177+
error: aborting due to 18 previous errors
166178

167179
Some errors have detailed explanations: E0049, E0195, E0391.
168180
For more information about an error, try `rustc --explain E0049`.

Diff for: 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 undefined opaque type
43
panic!();
54
MyStruct
65
}

Diff for: tests/ui/impl-trait/divergence.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: undefined opaque type
2+
--> $DIR/divergence.rs:1:13
3+
|
4+
LL | fn foo() -> impl MyTrait {
5+
| ^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

Diff for: tests/ui/impl-trait/impl-fn-hrtb-bounds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
1818

1919
fn d() -> impl Fn() -> (impl Debug + '_) {
2020
//~^ ERROR missing lifetime specifier
21+
//~| ERROR undefined opaque type
2122
|| ()
2223
}
2324

Diff for: tests/ui/impl-trait/impl-fn-hrtb-bounds.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ note: lifetime declared here
4646
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
4747
| ^^
4848

49-
error: aborting due to 4 previous errors
49+
error: undefined opaque type
50+
--> $DIR/impl-fn-hrtb-bounds.rs:19:25
51+
|
52+
LL | fn d() -> impl Fn() -> (impl Debug + '_) {
53+
| ^^^^^^^^^^^^^^^
54+
55+
error: aborting due to 5 previous errors
5056

5157
Some errors have detailed explanations: E0106, E0657.
5258
For more information about an error, try `rustc --explain E0106`.

Diff for: tests/ui/impl-trait/impl-fn-parsing-ambiguities.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn a() -> impl Fn(&u8) -> impl Debug + '_ {
99

1010
fn b() -> impl Fn() -> impl Debug + Send {
1111
//~^ ERROR ambiguous `+` in a type
12+
//~| ERROR undefined opaque type
1213
|| ()
1314
}
1415

Diff for: tests/ui/impl-trait/impl-fn-parsing-ambiguities.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ note: lifetime declared here
2222
LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ {
2323
| ^
2424

25-
error: aborting due to 3 previous errors
25+
error: undefined opaque type
26+
--> $DIR/impl-fn-parsing-ambiguities.rs:10:24
27+
|
28+
LL | fn b() -> impl Fn() -> impl Debug + Send {
29+
| ^^^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 4 previous errors
2632

2733
For more information about this error, try `rustc --explain E0657`.

Diff for: tests/ui/impl-trait/impl_fn_associativity.rs

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

54
fn f_debug() -> impl Fn() -> impl Debug {
5+
//~^ ERROR undefined opaque type
66
|| ()
77
}
88

99
fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug {
10+
//~^ ERROR undefined opaque type
11+
//~| ERROR undefined opaque type
1012
|| f_debug()
1113
}
1214

1315
fn multi() -> impl Fn() -> (impl Debug + Send) {
16+
//~^ ERROR undefined opaque type
1417
|| ()
1518
}
1619

Diff for: tests/ui/impl-trait/impl_fn_associativity.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: undefined opaque type
2+
--> $DIR/impl_fn_associativity.rs:4:30
3+
|
4+
LL | fn f_debug() -> impl Fn() -> impl Debug {
5+
| ^^^^^^^^^^
6+
7+
error: undefined opaque type
8+
--> $DIR/impl_fn_associativity.rs:9:31
9+
|
10+
LL | fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: undefined opaque type
14+
--> $DIR/impl_fn_associativity.rs:9:44
15+
|
16+
LL | fn ff_debug() -> impl Fn() -> impl Fn() -> impl Debug {
17+
| ^^^^^^^^^^
18+
19+
error: undefined opaque type
20+
--> $DIR/impl_fn_associativity.rs:15:29
21+
|
22+
LL | fn multi() -> impl Fn() -> (impl Debug + Send) {
23+
| ^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+

Diff for: tests/ui/impl-trait/impl_trait_projections.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ fn path_parametrized_type_is_allowed() -> option::Option<impl Debug> {
1010
}
1111

1212
fn projection_is_disallowed(x: impl Iterator) -> <impl Iterator>::Item {
13-
//~^ ERROR `impl Trait` is not allowed in path parameters
14-
//~| ERROR `impl Trait` is not allowed in path parameters
13+
//~^ ERROR `impl Trait` is not allowed in path parameters
14+
//~| ERROR `impl Trait` is not allowed in path parameters
1515
x.next().unwrap()
1616
}
1717

18-
fn projection_with_named_trait_is_disallowed(mut x: impl Iterator)
19-
-> <impl Iterator as Iterator>::Item
18+
fn projection_with_named_trait_is_disallowed(
19+
mut x: impl Iterator,
20+
) -> <impl Iterator as Iterator>::Item
2021
//~^ ERROR `impl Trait` is not allowed in path parameters
2122
{
2223
x.next().unwrap()
2324
}
2425

2526
fn projection_with_named_trait_inside_path_is_disallowed()
26-
-> <::std::ops::Range<impl Debug> as Iterator>::Item
27+
-> <::std::ops::Range<impl Debug> as Iterator>::Item
2728
//~^ ERROR `impl Trait` is not allowed in path parameters
2829
//~| ERROR `impl Debug: Step` is not satisfied
2930
{
@@ -32,8 +33,9 @@ fn projection_with_named_trait_inside_path_is_disallowed()
3233
}
3334

3435
fn projection_from_impl_trait_inside_dyn_trait_is_disallowed()
35-
-> <dyn Iterator<Item = impl Debug> as Iterator>::Item
36+
-> <dyn Iterator<Item = impl Debug> as Iterator>::Item
3637
//~^ ERROR `impl Trait` is not allowed in path parameters
38+
//~| ERROR undefined opaque type
3739
{
3840
panic!()
3941
}

Diff for: tests/ui/impl-trait/impl_trait_projections.stderr

+20-14
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ LL | fn projection_is_disallowed(x: impl Iterator) -> <impl Iterator>::Item {
55
| ^^^^^^^^^^^^^
66

77
error[E0667]: `impl Trait` is not allowed in path parameters
8-
--> $DIR/impl_trait_projections.rs:19:9
8+
--> $DIR/impl_trait_projections.rs:20:7
99
|
10-
LL | -> <impl Iterator as Iterator>::Item
11-
| ^^^^^^^^^^^^^
10+
LL | ) -> <impl Iterator as Iterator>::Item
11+
| ^^^^^^^^^^^^^
1212

1313
error[E0667]: `impl Trait` is not allowed in path parameters
14-
--> $DIR/impl_trait_projections.rs:26:27
14+
--> $DIR/impl_trait_projections.rs:27:23
1515
|
16-
LL | -> <::std::ops::Range<impl Debug> as Iterator>::Item
17-
| ^^^^^^^^^^
16+
LL | -> <::std::ops::Range<impl Debug> as Iterator>::Item
17+
| ^^^^^^^^^^
1818

1919
error[E0667]: `impl Trait` is not allowed in path parameters
20-
--> $DIR/impl_trait_projections.rs:35:29
20+
--> $DIR/impl_trait_projections.rs:36:25
2121
|
22-
LL | -> <dyn Iterator<Item = impl Debug> as Iterator>::Item
23-
| ^^^^^^^^^^
22+
LL | -> <dyn Iterator<Item = impl Debug> as Iterator>::Item
23+
| ^^^^^^^^^^
2424

2525
error[E0667]: `impl Trait` is not allowed in path parameters
2626
--> $DIR/impl_trait_projections.rs:12:51
@@ -29,10 +29,10 @@ LL | fn projection_is_disallowed(x: impl Iterator) -> <impl Iterator>::Item {
2929
| ^^^^^^^^^^^^^
3030

3131
error[E0277]: the trait bound `impl Debug: Step` is not satisfied
32-
--> $DIR/impl_trait_projections.rs:26:8
32+
--> $DIR/impl_trait_projections.rs:27:4
3333
|
34-
LL | -> <::std::ops::Range<impl Debug> as Iterator>::Item
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Step` is not implemented for `impl Debug`, which is required by `std::ops::Range<impl Debug>: Iterator`
34+
LL | -> <::std::ops::Range<impl Debug> as Iterator>::Item
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Step` is not implemented for `impl Debug`, which is required by `std::ops::Range<impl Debug>: Iterator`
3636
|
3737
= help: the following other types implement trait `Step`:
3838
Char
@@ -47,7 +47,7 @@ LL | -> <::std::ops::Range<impl Debug> as Iterator>::Item
4747
= note: required for `std::ops::Range<impl Debug>` to implement `Iterator`
4848

4949
error[E0277]: the trait bound `impl Debug: Step` is not satisfied
50-
--> $DIR/impl_trait_projections.rs:29:1
50+
--> $DIR/impl_trait_projections.rs:30:1
5151
|
5252
LL | / {
5353
LL | |
@@ -67,7 +67,13 @@ LL | | }
6767
and 8 others
6868
= note: required for `std::ops::Range<impl Debug>` to implement `Iterator`
6969

70-
error: aborting due to 7 previous errors
70+
error: undefined opaque type
71+
--> $DIR/impl_trait_projections.rs:36:25
72+
|
73+
LL | -> <dyn Iterator<Item = impl Debug> as Iterator>::Item
74+
| ^^^^^^^^^^
75+
76+
error: aborting due to 8 previous errors
7177

7278
Some errors have detailed explanations: E0277, E0667.
7379
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)