Skip to content

Commit 15dff27

Browse files
Actually use placeholder regions for trait method late bound regions in collect_return_position_impl_trait_in_trait_tys
1 parent 15b663e commit 15dff27

8 files changed

+77
-23
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+6-6
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);
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)