Skip to content

Commit df1c1af

Browse files
Check that RPITs are compatible with the opaques inferred during HIR typeck too
1 parent 3d09b99 commit df1c1af

File tree

7 files changed

+45
-154
lines changed

7 files changed

+45
-154
lines changed

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

+36-29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::StashKey;
12
use rustc_hir::def_id::LocalDefId;
23
use rustc_hir::intravisit::{self, Visitor};
34
use rustc_hir::{self as hir, Expr, ImplItem, Item, Node, TraitItem};
@@ -210,12 +211,40 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
210211
def_id: LocalDefId,
211212
owner_def_id: LocalDefId,
212213
) -> Ty<'_> {
213-
let concrete = tcx.mir_borrowck(owner_def_id).concrete_opaque_types.get(&def_id).copied();
214+
let tables = tcx.typeck(owner_def_id);
214215

215-
if let Some(concrete) = concrete {
216+
// Check that all of the opaques we inferred during HIR are compatible.
217+
// FIXME: We explicitly don't check that the types inferred during HIR
218+
// typeck are compatible with the one that we infer during borrowck,
219+
// because that one actually sometimes has consts evaluated eagerly so
220+
// using strict type equality will fail.
221+
let mut hir_opaque_ty: Option<ty::OpaqueHiddenType<'tcx>> = None;
222+
if tables.tainted_by_errors.is_none() {
223+
for (&opaque_type_key, &hidden_type) in &tables.concrete_opaque_types {
224+
if opaque_type_key.def_id != def_id {
225+
continue;
226+
}
227+
let concrete_type = tcx.erase_regions(
228+
hidden_type.remap_generic_params_to_declaration_params(opaque_type_key, tcx, true),
229+
);
230+
if let Some(prev) = &mut hir_opaque_ty {
231+
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
232+
prev.report_mismatch(&concrete_type, def_id, tcx).stash(
233+
tcx.def_span(opaque_type_key.def_id),
234+
StashKey::OpaqueHiddenTypeMismatch,
235+
);
236+
}
237+
} else {
238+
hir_opaque_ty = Some(concrete_type);
239+
}
240+
}
241+
}
242+
243+
let mir_opaque_ty = tcx.mir_borrowck(owner_def_id).concrete_opaque_types.get(&def_id).copied();
244+
if let Some(mir_opaque_ty) = mir_opaque_ty {
216245
let scope = tcx.hir().local_def_id_to_hir_id(owner_def_id);
217246
debug!(?scope);
218-
let mut locator = RpitConstraintChecker { def_id, tcx, found: concrete };
247+
let mut locator = RpitConstraintChecker { def_id, tcx, found: mir_opaque_ty };
219248

220249
match tcx.hir().get(scope) {
221250
Node::Item(it) => intravisit::walk_item(&mut locator, it),
@@ -224,38 +253,16 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
224253
other => bug!("{:?} is not a valid scope for an opaque type item", other),
225254
}
226255

227-
concrete.ty
256+
mir_opaque_ty.ty
228257
} else {
229-
let tables = tcx.typeck(owner_def_id);
230258
if let Some(guar) = tables.tainted_by_errors {
231-
// Some error in the
232-
// owner fn prevented us from populating
259+
// Some error in the owner fn prevented us from populating
233260
// the `concrete_opaque_types` table.
234261
tcx.ty_error(guar)
235262
} else {
236263
// Fall back to the RPIT we inferred during HIR typeck
237-
let mut opaque_ty: Option<ty::OpaqueHiddenType<'tcx>> = None;
238-
for (&opaque_type_key, &hidden_type) in &tables.concrete_opaque_types {
239-
if opaque_type_key.def_id != def_id {
240-
continue;
241-
}
242-
let concrete_type =
243-
tcx.erase_regions(hidden_type.remap_generic_params_to_declaration_params(
244-
opaque_type_key,
245-
tcx,
246-
true,
247-
));
248-
if let Some(prev) = &mut opaque_ty {
249-
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
250-
prev.report_mismatch(&concrete_type, def_id, tcx).emit();
251-
}
252-
} else {
253-
opaque_ty = Some(concrete_type);
254-
}
255-
}
256-
257-
if let Some(opaque_ty) = opaque_ty {
258-
opaque_ty.ty
264+
if let Some(hir_opaque_ty) = hir_opaque_ty {
265+
hir_opaque_ty.ty
259266
} else {
260267
// We failed to resolve the opaque type or it
261268
// resolves to itself. We interpret this as the

compiler/rustc_middle/src/ty/typeck_results.rs

-4
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ pub struct TypeckResults<'tcx> {
155155
/// We also store the type here, so that the compiler can use it as a hint
156156
/// for figuring out hidden types, even if they are only set in dead code
157157
/// (which doesn't show up in MIR).
158-
///
159-
/// These types are mapped back to the opaque's identity substitutions
160-
/// (with erased regions), which is why we don't associated substs with any
161-
/// of these usages.
162158
pub concrete_opaque_types: FxIndexMap<ty::OpaqueTypeKey<'tcx>, ty::OpaqueHiddenType<'tcx>>,
163159

164160
/// Tracks the minimum captures required for a closure;

tests/ui/chalkify/bugs/async.stderr

+3-10
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ error: internal compiler error: projection clauses should be implied from elsewh
1313
LL | async fn foo(x: u32) -> u32 {
1414
| ^^^query stack during panic:
1515
#0 [typeck] type-checking `foo`
16-
#1 [thir_body] building THIR for `foo`
17-
#2 [check_match] match-checking `foo`
18-
#3 [mir_built] building MIR for `foo`
19-
#4 [unsafety_check_result] unsafety-checking `foo`
20-
#5 [mir_const] preparing `foo` for borrow checking
21-
#6 [mir_promoted] promoting constants in MIR for `foo`
22-
#7 [mir_borrowck] borrow-checking `foo`
23-
#8 [type_of] computing type of `foo::{opaque#0}`
24-
#9 [check_mod_item_types] checking item types in top-level module
25-
#10 [analysis] running analysis passes on this crate
16+
#1 [type_of] computing type of `foo::{opaque#0}`
17+
#2 [check_mod_item_types] checking item types in top-level module
18+
#3 [analysis] running analysis passes on this crate
2619
end of query stack
2720
error: aborting due to 2 previous errors
2821

tests/ui/dyn-star/param-env-infer.next.stderr

-35
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,6 @@ error[E0391]: cycle detected when computing type of `make_dyn_star::{opaque#0}`
1313
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
16-
note: ...which requires borrow-checking `make_dyn_star`...
17-
--> $DIR/param-env-infer.rs:11:1
18-
|
19-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21-
note: ...which requires promoting constants in MIR for `make_dyn_star`...
22-
--> $DIR/param-env-infer.rs:11:1
23-
|
24-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26-
note: ...which requires preparing `make_dyn_star` for borrow checking...
27-
--> $DIR/param-env-infer.rs:11:1
28-
|
29-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31-
note: ...which requires unsafety-checking `make_dyn_star`...
32-
--> $DIR/param-env-infer.rs:11:1
33-
|
34-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36-
note: ...which requires building MIR for `make_dyn_star`...
37-
--> $DIR/param-env-infer.rs:11:1
38-
|
39-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41-
note: ...which requires match-checking `make_dyn_star`...
42-
--> $DIR/param-env-infer.rs:11:1
43-
|
44-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46-
note: ...which requires building THIR for `make_dyn_star`...
47-
--> $DIR/param-env-infer.rs:11:1
48-
|
49-
LL | fn make_dyn_star<'a, T: PointerLike + Debug + 'a>(t: T) -> impl PointerLike + Debug + 'a {
50-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5116
note: ...which requires type-checking `make_dyn_star`...
5217
--> $DIR/param-env-infer.rs:11:1
5318
|

tests/ui/impl-trait/auto-trait-leak.stderr

-70
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,6 @@ error[E0391]: cycle detected when computing type of `cycle1::{opaque#0}`
44
LL | fn cycle1() -> impl Clone {
55
| ^^^^^^^^^^
66
|
7-
note: ...which requires borrow-checking `cycle1`...
8-
--> $DIR/auto-trait-leak.rs:12:1
9-
|
10-
LL | fn cycle1() -> impl Clone {
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires promoting constants in MIR for `cycle1`...
13-
--> $DIR/auto-trait-leak.rs:12:1
14-
|
15-
LL | fn cycle1() -> impl Clone {
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
17-
note: ...which requires preparing `cycle1` for borrow checking...
18-
--> $DIR/auto-trait-leak.rs:12:1
19-
|
20-
LL | fn cycle1() -> impl Clone {
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
22-
note: ...which requires unsafety-checking `cycle1`...
23-
--> $DIR/auto-trait-leak.rs:12:1
24-
|
25-
LL | fn cycle1() -> impl Clone {
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
27-
note: ...which requires building MIR for `cycle1`...
28-
--> $DIR/auto-trait-leak.rs:12:1
29-
|
30-
LL | fn cycle1() -> impl Clone {
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
32-
note: ...which requires match-checking `cycle1`...
33-
--> $DIR/auto-trait-leak.rs:12:1
34-
|
35-
LL | fn cycle1() -> impl Clone {
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
37-
note: ...which requires building THIR for `cycle1`...
38-
--> $DIR/auto-trait-leak.rs:12:1
39-
|
40-
LL | fn cycle1() -> impl Clone {
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
427
note: ...which requires type-checking `cycle1`...
438
--> $DIR/auto-trait-leak.rs:14:5
449
|
@@ -50,41 +15,6 @@ note: ...which requires computing type of `cycle2::{opaque#0}`...
5015
|
5116
LL | fn cycle2() -> impl Clone {
5217
| ^^^^^^^^^^
53-
note: ...which requires borrow-checking `cycle2`...
54-
--> $DIR/auto-trait-leak.rs:19:1
55-
|
56-
LL | fn cycle2() -> impl Clone {
57-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
58-
note: ...which requires promoting constants in MIR for `cycle2`...
59-
--> $DIR/auto-trait-leak.rs:19:1
60-
|
61-
LL | fn cycle2() -> impl Clone {
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
63-
note: ...which requires preparing `cycle2` for borrow checking...
64-
--> $DIR/auto-trait-leak.rs:19:1
65-
|
66-
LL | fn cycle2() -> impl Clone {
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
68-
note: ...which requires unsafety-checking `cycle2`...
69-
--> $DIR/auto-trait-leak.rs:19:1
70-
|
71-
LL | fn cycle2() -> impl Clone {
72-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
73-
note: ...which requires building MIR for `cycle2`...
74-
--> $DIR/auto-trait-leak.rs:19:1
75-
|
76-
LL | fn cycle2() -> impl Clone {
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
78-
note: ...which requires match-checking `cycle2`...
79-
--> $DIR/auto-trait-leak.rs:19:1
80-
|
81-
LL | fn cycle2() -> impl Clone {
82-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
83-
note: ...which requires building THIR for `cycle2`...
84-
--> $DIR/auto-trait-leak.rs:19:1
85-
|
86-
LL | fn cycle2() -> impl Clone {
87-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8818
note: ...which requires type-checking `cycle2`...
8919
--> $DIR/auto-trait-leak.rs:20:5
9020
|

tests/ui/impl-trait/multiple-defining-usages-in-body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ impl Trait for () {}
44
fn foo<T: Trait, U: Trait>() -> impl Trait {
55
//~^ WARN function cannot return without recursing [unconditional_recursion]
66
let a: T = foo::<T, U>();
7-
//~^ ERROR concrete type differs from previous defining opaque type use
87
loop {}
98
let _: T = foo::<U, T>();
9+
//~^ ERROR concrete type differs from previous defining opaque type use
1010
}
1111

1212
fn main() {}

tests/ui/impl-trait/multiple-defining-usages-in-body.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ LL | let a: T = foo::<T, U>();
1111
= note: `#[warn(unconditional_recursion)]` on by default
1212

1313
error: concrete type differs from previous defining opaque type use
14-
--> $DIR/multiple-defining-usages-in-body.rs:6:16
14+
--> $DIR/multiple-defining-usages-in-body.rs:8:16
1515
|
16-
LL | let a: T = foo::<T, U>();
17-
| ^^^^^^^^^^^^^ expected `U`, got `T`
16+
LL | let _: T = foo::<U, T>();
17+
| ^^^^^^^^^^^^^ expected `T`, got `U`
1818
|
1919
note: previous use here
20-
--> $DIR/multiple-defining-usages-in-body.rs:9:16
20+
--> $DIR/multiple-defining-usages-in-body.rs:6:16
2121
|
22-
LL | let _: T = foo::<U, T>();
22+
LL | let a: T = foo::<T, U>();
2323
| ^^^^^^^^^^^^^
2424

2525
error: aborting due to previous error; 1 warning emitted

0 commit comments

Comments
 (0)