Skip to content

Commit 3a7b5aa

Browse files
Rollup merge of #163102 - lcnr:opaque-type-modulo-regions, r=oli-obk
remove unnecessary restriction with next-solver We previously FCP'd to forbid uses of opaque types which only differ in their lifetime arguments during MIR borrowck in #116935 (comment). This actually did not end up being necessary after all. Will explain this a bit more in the stabilization documentation for the new solver r? types
2 parents 923f22a + 919170b commit 3a7b5aa

5 files changed

Lines changed: 42 additions & 37 deletions

File tree

‎compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -362,44 +362,43 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
362362
DefinitionSiteHiddenType::new_error(tcx, guar)
363363
});
364364

365-
// Sometimes, when the hidden type is an inference variable, it can happen that
366-
// the hidden type becomes the opaque type itself. In this case, this was an opaque
367-
// usage of the opaque type and we can ignore it. This check is mirrored in typeck's
368-
// writeback.
369365
if !rcx.infcx.tcx.use_typing_mode_post_typeck_until_borrowck() {
366+
// Sometimes, when the hidden type is an inference variable, it can happen that
367+
// the hidden type becomes the opaque type itself. In this case, this was an opaque
368+
// usage of the opaque type and we can ignore it. This check is mirrored in typeck's
369+
// writeback.
370370
if let &ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
371371
hidden_type.ty.skip_binder().kind()
372372
&& def_id == opaque_type_key.def_id.to_def_id()
373373
&& args == opaque_type_key.args
374374
{
375375
continue;
376376
}
377-
}
378377

379-
// Check that all opaque types have the same region parameters if they have the same
380-
// non-region parameters. This is necessary because within the new solver we perform
381-
// various query operations modulo regions, and thus could unsoundly select some impls
382-
// that don't hold.
383-
//
384-
// FIXME(-Znext-solver): This isn't necessary after all. We can remove this check again.
385-
if let Some((prev_decl_key, prev_span)) = decls_modulo_regions.insert(
386-
rcx.infcx.tcx.erase_and_anonymize_regions(opaque_type_key),
387-
(opaque_type_key, hidden_type.span),
388-
) && let Some((arg1, arg2)) = std::iter::zip(
389-
prev_decl_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg),
390-
opaque_type_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg),
391-
)
392-
.find(|(arg1, arg2)| arg1 != arg2)
393-
{
394-
errors.push(DeferredOpaqueTypeError::LifetimeMismatchOpaqueParam(
395-
LifetimeMismatchOpaqueParam {
396-
arg: arg1,
397-
prev: arg2,
398-
span: prev_span,
399-
prev_span: hidden_type.span,
400-
},
401-
));
378+
// Check that all opaque types have the same region parameters if they have the same
379+
// non-region parameters. This is necessary because within the new solver we perform
380+
// various query operations modulo regions, and thus could unsoundly select some impls
381+
// that don't hold.
382+
if let Some((prev_decl_key, prev_span)) = decls_modulo_regions.insert(
383+
rcx.infcx.tcx.erase_and_anonymize_regions(opaque_type_key),
384+
(opaque_type_key, hidden_type.span),
385+
) && let Some((arg1, arg2)) = std::iter::zip(
386+
prev_decl_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg),
387+
opaque_type_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg),
388+
)
389+
.find(|(arg1, arg2)| arg1 != arg2)
390+
{
391+
errors.push(DeferredOpaqueTypeError::LifetimeMismatchOpaqueParam(
392+
LifetimeMismatchOpaqueParam {
393+
arg: arg1,
394+
prev: arg2,
395+
span: prev_span,
396+
prev_span: hidden_type.span,
397+
},
398+
));
399+
}
402400
}
401+
403402
add_hidden_type(tcx, hidden_types, opaque_type_key.def_id, hidden_type);
404403
}
405404
}

tests/ui/type-alias-impl-trait/lifetime_mismatch.stderr renamed to tests/ui/type-alias-impl-trait/lifetime_mismatch.current.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: opaque type used twice with different lifetimes
2-
--> $DIR/lifetime_mismatch.rs:7:5
2+
--> $DIR/lifetime_mismatch.rs:10:5
33
|
44
LL | (x, y)
55
| ^^^^^^
@@ -8,13 +8,13 @@ LL | (x, y)
88
| lifetime `'b` previously used here
99
|
1010
note: if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types
11-
--> $DIR/lifetime_mismatch.rs:7:5
11+
--> $DIR/lifetime_mismatch.rs:10:5
1212
|
1313
LL | (x, y)
1414
| ^^^^^^
1515

1616
error: opaque type used twice with different lifetimes
17-
--> $DIR/lifetime_mismatch.rs:15:5
17+
--> $DIR/lifetime_mismatch.rs:18:5
1818
|
1919
LL | (i, j)
2020
| ^^^^^^
@@ -23,7 +23,7 @@ LL | (i, j)
2323
| lifetime `'y` previously used here
2424
|
2525
note: if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types
26-
--> $DIR/lifetime_mismatch.rs:15:5
26+
--> $DIR/lifetime_mismatch.rs:18:5
2727
|
2828
LL | (i, j)
2929
| ^^^^^^

‎tests/ui/type-alias-impl-trait/lifetime_mismatch.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
//@ revisions: current next
2+
//@[next] compile-flags: -Znext-solver=globally
3+
//@[next] check-pass
14
#![feature(type_alias_impl_trait)]
25

36
type Foo<'a> = impl Sized;
47

58
#[define_opaque(Foo)]
69
fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> (Foo<'a>, Foo<'b>) {
710
(x, y)
8-
//~^ ERROR opaque type used twice with different lifetimes
11+
//[current]~^ ERROR opaque type used twice with different lifetimes
912
}
1013

1114
type Bar<'a, 'b> = impl std::fmt::Debug;
1215

1316
#[define_opaque(Bar)]
1417
fn bar<'x, 'y>(i: &'x i32, j: &'y i32) -> (Bar<'x, 'y>, Bar<'y, 'x>) {
1518
(i, j)
16-
//~^ ERROR opaque type used twice with different lifetimes
19+
//[current]~^ ERROR opaque type used twice with different lifetimes
1720
}
1821

1922
fn main() {

tests/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn-lifetimes.stderr renamed to tests/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn-lifetimes.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: opaque type used twice with different lifetimes
2-
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:7:5
2+
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:10:5
33
|
44
LL | (i, j)
55
| ^^^^^^
@@ -8,7 +8,7 @@ LL | (i, j)
88
| lifetime `'y` previously used here
99
|
1010
note: if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types
11-
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:7:5
11+
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:10:5
1212
|
1313
LL | (i, j)
1414
| ^^^^^^
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
//@ revisions: current next
2+
//@[next] compile-flags: -Znext-solver=globally
3+
//@[next] check-pass
14
#![feature(type_alias_impl_trait)]
25

36
type Foo<'a, 'b> = impl std::fmt::Debug;
47

58
#[define_opaque(Foo)]
69
fn foo<'x, 'y>(i: &'x i32, j: &'y i32) -> (Foo<'x, 'y>, Foo<'y, 'x>) {
710
(i, j)
8-
//~^ ERROR opaque type used twice with different lifetimes
11+
//[current]~^ ERROR opaque type used twice with different lifetimes
912
}
1013

1114
fn main() {}

0 commit comments

Comments
 (0)