Skip to content

Commit 5ff4501

Browse files
committed
Auto merge of rust-lang#125285 - spastorino:unchk-region-opaque-deadcode, r=<try>
Error for RPIT if they are not defined during MIR borrowck r? `@lcnr` Fixes rust-lang#112417 There are some changes in tests that we would need to properly review. I've left some comments on each of them.
2 parents 474bee7 + c130bfb commit 5ff4501

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

+392
-190
lines changed

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 =

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

+4-13
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

+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]
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ compile-flags:-Zverbose-internals
2+
3+
#![feature(rustc_attrs)]
4+
#![rustc_hidden_type_of_opaques]
5+
6+
trait CallMeMaybe<'a, 'b> {
7+
fn mk() -> Self;
8+
fn subtype<T>(self, x: &'b T) -> &'a T;
9+
}
10+
11+
struct Foo<'a, 'b: 'a>(&'a (), &'b ());
12+
impl<'a, 'b> CallMeMaybe<'a, 'b> for Foo<'a, 'b> {
13+
fn mk() -> Self {
14+
Foo(&(), &())
15+
}
16+
17+
fn subtype<T>(self, x: &'b T) -> &'a T {
18+
x
19+
}
20+
}
21+
22+
fn good_bye() -> ! {
23+
panic!();
24+
}
25+
26+
fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
27+
//~^ ERROR: {type error}
28+
//~| ERROR: undefined opaque type
29+
good_bye();
30+
Foo(&(), &())
31+
}
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: undefined opaque type
2+
--> $DIR/opaque-types-deadcode.rs:26:25
3+
|
4+
LL | fn foo<'a, 'b: 'a>() -> impl CallMeMaybe<'a, 'b> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
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
14+

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.

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`.

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
}

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+

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

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`.

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

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`.

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

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+

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
}

0 commit comments

Comments
 (0)