Skip to content

Commit c130bfb

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

Some content is hidden

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

43 files changed

+355
-193
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 2 additions & 0 deletions
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 =

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

Lines changed: 4 additions & 13 deletions
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,18 +389,9 @@ 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+
// Error if we couldn't define the RPIT during MIR borrowck
393+
let err = tcx.dcx().emit_err(UndefinedOpaqueType { span: tcx.def_span(def_id) });
394+
Ty::new_error(tcx, err)
404395
}
405396
}
406397
}

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 7 additions & 0 deletions
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]

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

Lines changed: 2 additions & 1 deletion
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
}
Lines changed: 8 additions & 2 deletions
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

tests/ui/delegation/not-supported.rs

Lines changed: 2 additions & 0 deletions
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.

tests/ui/delegation/not-supported.stderr

Lines changed: 27 additions & 15 deletions
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`.

tests/ui/impl-trait/divergence.rs

Lines changed: 1 addition & 2 deletions
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
}
Lines changed: 8 additions & 0 deletions
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+

tests/ui/impl-trait/impl-fn-hrtb-bounds.rs

Lines changed: 1 addition & 0 deletions
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

0 commit comments

Comments
 (0)