Skip to content

Commit 512ae20

Browse files
Check unnormalized signature on pointer cast
1 parent 4fe1e2b commit 512ae20

7 files changed

+126
-13
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+63-7
Original file line numberDiff line numberDiff line change
@@ -1978,19 +1978,75 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19781978

19791979
match cast_kind {
19801980
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
1981-
let fn_sig = op.ty(body, tcx).fn_sig(tcx);
1981+
let src_sig = op.ty(body, tcx).fn_sig(tcx);
1982+
1983+
// HACK: This shouldn't be necessary... We can remove this when we actually
1984+
// get binders with where clauses, then elaborate implied bounds into that
1985+
// binder, and implement a higher-ranked subtyping algorithm that actually
1986+
// respects these implied bounds.
1987+
//
1988+
// This protects against the case where we are casting from a higher-ranked
1989+
// fn item to a non-higher-ranked fn pointer, where the cast throws away
1990+
// implied bounds that would've needed to be checked at the call site. This
1991+
// only works when we're casting to a non-higher-ranked fn ptr, since we
1992+
// need to instantiate the higher-ranked signature with *infer* vars, not
1993+
// placeholders.
1994+
//
1995+
// We check that this signature is WF before subtyping the signature with
1996+
// the target fn sig.
1997+
if src_sig.has_bound_regions()
1998+
&& let ty::FnPtr(target_sig) = *ty.kind()
1999+
&& let Some(target_sig) = target_sig.no_bound_vars()
2000+
{
2001+
let src_sig = self.infcx.instantiate_binder_with_fresh_vars(
2002+
span,
2003+
BoundRegionConversionTime::HigherRankedType,
2004+
src_sig,
2005+
);
2006+
let src_ty = Ty::new_fn_ptr(self.tcx(), ty::Binder::dummy(src_sig));
2007+
self.prove_predicate(
2008+
ty::ClauseKind::WellFormed(src_ty.into()),
2009+
location.to_locations(),
2010+
ConstraintCategory::Cast { unsize_to: None },
2011+
);
2012+
2013+
let src_ty = self.normalize(src_ty, location);
2014+
if let Err(terr) = self.sub_types(
2015+
src_ty,
2016+
*ty,
2017+
location.to_locations(),
2018+
ConstraintCategory::Cast { unsize_to: None },
2019+
) {
2020+
span_mirbug!(
2021+
self,
2022+
rvalue,
2023+
"equating {:?} with {:?} yields {:?}",
2024+
target_sig,
2025+
src_sig,
2026+
terr
2027+
);
2028+
};
2029+
}
2030+
2031+
let src_ty = Ty::new_fn_ptr(tcx, src_sig);
2032+
// HACK: We want to assert that the signature of the source fn is
2033+
// well-formed, because we don't enforce that via the WF of FnDef
2034+
// types normally. This should be removed when we improve the tracking
2035+
// of implied bounds of fn signatures.
2036+
self.prove_predicate(
2037+
ty::ClauseKind::WellFormed(src_ty.into()),
2038+
location.to_locations(),
2039+
ConstraintCategory::Cast { unsize_to: None },
2040+
);
19822041

19832042
// The type that we see in the fcx is like
19842043
// `foo::<'a, 'b>`, where `foo` is the path to a
19852044
// function definition. When we extract the
19862045
// signature, it comes from the `fn_sig` query,
19872046
// and hence may contain unnormalized results.
1988-
let fn_sig = self.normalize(fn_sig, location);
1989-
1990-
let ty_fn_ptr_from = Ty::new_fn_ptr(tcx, fn_sig);
1991-
2047+
let src_ty = self.normalize(src_ty, location);
19922048
if let Err(terr) = self.sub_types(
1993-
ty_fn_ptr_from,
2049+
src_ty,
19942050
*ty,
19952051
location.to_locations(),
19962052
ConstraintCategory::Cast { unsize_to: None },
@@ -1999,7 +2055,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19992055
self,
20002056
rvalue,
20012057
"equating {:?} with {:?} yields {:?}",
2002-
ty_fn_ptr_from,
2058+
src_ty,
20032059
ty,
20042060
terr
20052061
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
static UNIT: &'static &'static () = &&();
2+
3+
fn foo<'a: 'a, 'b: 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }
4+
5+
fn bad<'a, T>(x: &'a T) -> &'static T {
6+
let f: fn(_, &'a T) -> &'static T = foo;
7+
//~^ ERROR lifetime may not live long enough
8+
f(UNIT, x)
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/implied-bounds-on-nested-references-plus-variance-early-bound.rs:6:12
3+
|
4+
LL | fn bad<'a, T>(x: &'a T) -> &'static T {
5+
| -- lifetime `'a` defined here
6+
LL | let f: fn(_, &'a T) -> &'static T = foo;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
8+
9+
error: aborting due to 1 previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait ToArg<T> {
2+
type Arg;
3+
}
4+
impl<T, U> ToArg<T> for U {
5+
type Arg = T;
6+
}
7+
8+
fn extend_inner<'a, 'b>(x: &'a str) -> <&'b &'a () as ToArg<&'b str>>::Arg { x }
9+
fn extend<'a, 'b>(x: &'a str) -> &'b str {
10+
(extend_inner as fn(_) -> _)(x)
11+
//~^ ERROR lifetime may not live long enough
12+
}
13+
14+
fn main() {
15+
let y = extend(&String::from("Hello World"));
16+
println!("{}", y);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/implied-bounds-on-nested-references-plus-variance-unnormalized.rs:10:5
3+
|
4+
LL | fn extend<'a, 'b>(x: &'a str) -> &'b str {
5+
| -- -- lifetime `'b` defined here
6+
| |
7+
| lifetime `'a` defined here
8+
LL | (extend_inner as fn(_) -> _)(x)
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
10+
|
11+
= help: consider adding the following bound: `'a: 'b`
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/implied-bounds/implied-bounds-on-nested-references-plus-variance.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
//@ check-pass
2-
//@ known-bug: #25860
3-
4-
// Should fail. The combination of variance and implied bounds for nested
5-
// references allows us to infer a longer lifetime than we can prove.
6-
71
static UNIT: &'static &'static () = &&();
82

93
fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }
104

115
fn bad<'a, T>(x: &'a T) -> &'static T {
126
let f: fn(_, &'a T) -> &'static T = foo;
7+
//~^ ERROR lifetime may not live long enough
138
f(UNIT, x)
149
}
1510

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/implied-bounds-on-nested-references-plus-variance.rs:6:12
3+
|
4+
LL | fn bad<'a, T>(x: &'a T) -> &'static T {
5+
| -- lifetime `'a` defined here
6+
LL | let f: fn(_, &'a T) -> &'static T = foo;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)