Skip to content

Commit dbed79a

Browse files
authored
Unrolled build for rust-lang#133428
Rollup merge of rust-lang#133428 - compiler-errors:rpitit-unsound, r=lcnr Actually use placeholder regions for trait method late bound regions in `collect_return_position_impl_trait_in_trait_tys` So in rust-lang#113182, I introduced a "diagnostics improvement" in the form of 473c88d, which changes which signature we end up instantiating with placeholder regions and which signature we end up instantiating with fresh region vars so that we have placeholders corresponding to the names of the late-bound regions coming from the *impl*. However, this is not sound, since now we're essentially no longer proving that *all* instantiations of the trait method are compatible with an instantiation of the impl method, but vice versa (which is weaker). Let's look at the example `tests/ui/impl-trait/in-trait/do-not-imply-from-trait-impl.rs`: ```rust trait MkStatic { fn mk_static(self) -> &'static str; } impl MkStatic for &'static str { fn mk_static(self) -> &'static str { self } } trait Foo { fn foo<'a: 'static, 'late>(&'late self) -> impl MkStatic; } impl Foo for str { fn foo<'a: 'static>(&'a self) -> impl MkStatic + 'static { self } } fn call_foo<T: Foo + ?Sized>(t: &T) -> &'static str { t.foo().mk_static() } fn main() { let s = call_foo(String::from("hello, world").as_str()); println!("> {s}"); } ``` To collect RPITITs, we were previously instantiating the trait signature with infer vars (`fn(&'?0 str) -> ?1t` where `?1t` is the variable we use to infer the RPITIT) and the impl signature with placeholders (there are no late-bound regions in that signature, so we just have `fn(&'a str) -> Opaque`). Equating the signatures works, since all we do is unify `?1t` with `Opaque` and `'?0` with `'a`. However, conceptually it *shouldn't* hold, since this definition is not valid for *all* instantiations of the trait method but just the one where `'0` (i.e. `'late`) is equal to `'a` :( ## So what This PR effectively reverts 473c88d to fix the unsoundness. Fixes rust-lang#133427 Also fixes rust-lang#133425, which is actually coincidentally another instance of this bug (but not one that is weaponized into UB, just one that causes an ICE in refinement checking).
2 parents eddb717 + 871cfc9 commit dbed79a

8 files changed

+92
-44
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+21-27
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,9 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
523523
let impl_sig = ocx.normalize(
524524
&misc_cause,
525525
param_env,
526-
tcx.liberate_late_bound_regions(
527-
impl_m.def_id,
526+
infcx.instantiate_binder_with_fresh_vars(
527+
return_span,
528+
infer::HigherRankedType,
528529
tcx.fn_sig(impl_m.def_id).instantiate_identity(),
529530
),
530531
);
@@ -536,10 +537,9 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
536537
// them with inference variables.
537538
// We will use these inference variables to collect the hidden types of RPITITs.
538539
let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_def_id);
539-
let unnormalized_trait_sig = infcx
540-
.instantiate_binder_with_fresh_vars(
541-
return_span,
542-
infer::HigherRankedType,
540+
let unnormalized_trait_sig = tcx
541+
.liberate_late_bound_regions(
542+
impl_m.def_id,
543543
tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_to_impl_args),
544544
)
545545
.fold_with(&mut collector);
@@ -702,8 +702,8 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
702702

703703
let mut remapped_types = DefIdMap::default();
704704
for (def_id, (ty, args)) in collected_types {
705-
match infcx.fully_resolve((ty, args)) {
706-
Ok((ty, args)) => {
705+
match infcx.fully_resolve(ty) {
706+
Ok(ty) => {
707707
// `ty` contains free regions that we created earlier while liberating the
708708
// trait fn signature. However, projection normalization expects `ty` to
709709
// contains `def_id`'s early-bound regions.
@@ -883,33 +883,27 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
883883
self.tcx
884884
}
885885

886-
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
887-
if let ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = *t.kind() {
888-
let mut mapped_args = Vec::with_capacity(args.len());
889-
for (arg, v) in std::iter::zip(args, self.tcx.variances_of(def_id)) {
890-
mapped_args.push(match (arg.unpack(), v) {
891-
// Skip uncaptured opaque args
892-
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => arg,
893-
_ => arg.try_fold_with(self)?,
894-
});
895-
}
896-
Ok(Ty::new_opaque(self.tcx, def_id, self.tcx.mk_args(&mapped_args)))
897-
} else {
898-
t.try_super_fold_with(self)
899-
}
900-
}
901-
902886
fn try_fold_region(
903887
&mut self,
904888
region: ty::Region<'tcx>,
905889
) -> Result<ty::Region<'tcx>, Self::Error> {
906890
match region.kind() {
907-
// Remap late-bound regions from the function.
891+
// Never remap bound regions or `'static`
892+
ty::ReBound(..) | ty::ReStatic | ty::ReError(_) => return Ok(region),
893+
// We always remap liberated late-bound regions from the function.
908894
ty::ReLateParam(_) => {}
909895
// Remap early-bound regions as long as they don't come from the `impl` itself,
910896
// in which case we don't really need to renumber them.
911-
ty::ReEarlyParam(ebr) if ebr.index as usize >= self.num_impl_args => {}
912-
_ => return Ok(region),
897+
ty::ReEarlyParam(ebr) => {
898+
if ebr.index as usize >= self.num_impl_args {
899+
// Remap
900+
} else {
901+
return Ok(region);
902+
}
903+
}
904+
ty::ReVar(_) | ty::RePlaceholder(_) | ty::ReErased => unreachable!(
905+
"should not have leaked vars or placeholders into hidden type of RPITIT"
906+
),
913907
}
914908

915909
let e = if let Some(id_region) = self.map.get(&region) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Make sure that we don't accidentally collect an RPITIT hidden type that does not
2+
// hold for all instantiations of the trait signature.
3+
4+
trait MkStatic {
5+
fn mk_static(self) -> &'static str;
6+
}
7+
8+
impl MkStatic for &'static str {
9+
fn mk_static(self) -> &'static str { self }
10+
}
11+
12+
trait Foo {
13+
fn foo<'a: 'static, 'late>(&'late self) -> impl MkStatic;
14+
}
15+
16+
impl Foo for str {
17+
fn foo<'a: 'static>(&'a self) -> impl MkStatic + 'static {
18+
//~^ ERROR method not compatible with trait
19+
self
20+
}
21+
}
22+
23+
fn call_foo<T: Foo + ?Sized>(t: &T) -> &'static str {
24+
t.foo().mk_static()
25+
}
26+
27+
fn main() {
28+
let s = call_foo(String::from("hello, world").as_str());
29+
println!("> {s}");
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: method not compatible with trait
2+
--> $DIR/do-not-imply-from-trait-impl.rs:17:38
3+
|
4+
LL | fn foo<'a: 'static>(&'a self) -> impl MkStatic + 'static {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
6+
|
7+
= note: expected signature `fn(&'late _) -> _`
8+
found signature `fn(&'a _) -> _`
9+
note: the lifetime `'late` as defined here...
10+
--> $DIR/do-not-imply-from-trait-impl.rs:13:25
11+
|
12+
LL | fn foo<'a: 'static, 'late>(&'late self) -> impl MkStatic;
13+
| ^^^^^
14+
note: ...does not necessarily outlive the lifetime `'a` as defined here
15+
--> $DIR/do-not-imply-from-trait-impl.rs:17:12
16+
|
17+
LL | fn foo<'a: 'static>(&'a self) -> impl MkStatic + 'static {
18+
| ^^
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0308`.

tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ note: type in trait
1111
|
1212
LL | fn early<'early, T>(x: &'early T) -> impl Sized;
1313
| ^^^^^^^^^
14-
= note: expected signature `fn(&T)`
15-
found signature `fn(&'late ())`
14+
= note: expected signature `fn(&'early T)`
15+
found signature `fn(&())`
1616
help: change the parameter type to match the trait
1717
|
18-
LL | fn early<'late, T>(_: &T) {}
19-
| ~~
18+
LL | fn early<'late, T>(_: &'early T) {}
19+
| ~~~~~~~~~
2020

2121
error: aborting due to 1 previous error
2222

tests/ui/impl-trait/in-trait/rpitit-hidden-types-self-implied-wf.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ LL | fn extend(s: &str) -> (Option<&'static &'_ ()>, &'static str) {
66
|
77
= note: the pointer is valid for the static lifetime
88
note: but the referenced data is only valid for the anonymous lifetime defined here
9-
--> $DIR/rpitit-hidden-types-self-implied-wf.rs:6:18
9+
--> $DIR/rpitit-hidden-types-self-implied-wf.rs:2:18
1010
|
11-
LL | fn extend(s: &str) -> (Option<&'static &'_ ()>, &'static str) {
11+
LL | fn extend(_: &str) -> (impl Sized + '_, &'static str);
1212
| ^^^^
1313

1414
error: aborting due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
error[E0623]: lifetime mismatch
1+
error[E0477]: the type `impl Future<Output = Vec<u8>>` does not fulfill the required lifetime
22
--> $DIR/signature-mismatch.rs:77:10
33
|
4-
LL | &'a self,
5-
| -------- this parameter and the return type are declared with different lifetimes...
6-
...
74
LL | ) -> impl Future<Output = Vec<u8>> {
85
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9-
| |
10-
| ...but data from `buff` is returned here
6+
|
7+
note: type must outlive the lifetime `'a` as defined here as required by this binding
8+
--> $DIR/signature-mismatch.rs:73:32
9+
|
10+
LL | fn async_fn_reduce_outlive<'a, 'b, T>(
11+
| ^^
1112

1213
error: aborting due to 1 previous error
1314

14-
For more information about this error, try `rustc --explain E0623`.
15+
For more information about this error, try `rustc --explain E0477`.

tests/ui/impl-trait/in-trait/signature-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl AsyncTrait for Struct {
7575
buff: &'b [u8],
7676
t: T,
7777
) -> impl Future<Output = Vec<u8>> {
78-
//[failure]~^ ERROR lifetime mismatch
78+
//[failure]~^ ERROR the type `impl Future<Output = Vec<u8>>` does not fulfill the required lifetime
7979
async move {
8080
let _t = t;
8181
vec![]

tests/ui/impl-trait/precise-capturing/rpitit-impl-captures-too-much.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error: return type captures more lifetimes than trait definition
22
--> $DIR/rpitit-impl-captures-too-much.rs:10:39
33
|
4+
LL | fn hello(self_: Invariant<'_>) -> impl Sized + use<Self>;
5+
| -- this lifetime was captured
6+
...
47
LL | fn hello(self_: Invariant<'_>) -> impl Sized + use<'_> {}
5-
| -- ^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| this lifetime was captured
8+
| ^^^^^^^^^^^^^^^^^^^^
89
|
910
note: hidden type must only reference lifetimes captured by this impl trait
1011
--> $DIR/rpitit-impl-captures-too-much.rs:6:39

0 commit comments

Comments
 (0)