Skip to content

Commit 57efa9c

Browse files
committed
borrowck: wf-check fn item substs
1 parent e30fb6a commit 57efa9c

File tree

10 files changed

+175
-5
lines changed

10 files changed

+175
-5
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,24 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
427427
// const_trait_impl: use a non-const param env when checking that a FnDef type is well formed.
428428
// this is because the well-formedness of the function does not need to be proved to have `const`
429429
// impls for trait bounds.
430-
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
431430
let prev = self.cx.param_env;
432431
self.cx.param_env = prev.without_const();
432+
433+
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
433434
self.cx.normalize_and_prove_instantiated_predicates(
434435
def_id,
435436
instantiated_predicates,
436437
locations,
437438
);
439+
440+
self.cx.prove_predicates(
441+
substs
442+
.types()
443+
.map(|ty| ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))),
444+
locations,
445+
ConstraintCategory::Boring,
446+
);
447+
438448
self.cx.param_env = prev;
439449
}
440450
}

library/std/src/collections/hash/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3051,8 +3051,8 @@ where
30513051
#[stable(feature = "hash_extend_copy", since = "1.4.0")]
30523052
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
30533053
where
3054-
K: Eq + Hash + Copy,
3055-
V: Copy,
3054+
K: Eq + Hash + Copy + 'a,
3055+
V: Copy + 'a,
30563056
S: BuildHasher,
30573057
{
30583058
#[inline]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// The method `assert_static` should be callable only for static values,
2+
// because the impl has an implied bound `where T: 'static`.
3+
4+
// check-fail
5+
6+
trait AnyStatic<Witness>: Sized {
7+
fn assert_static(self) {}
8+
}
9+
10+
impl<T> AnyStatic<&'static T> for T {}
11+
12+
fn main() {
13+
(&String::new()).assert_static();
14+
//~^ ERROR temporary value dropped while borrowed
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/fn-item-check-trait-ref.rs:13:7
3+
|
4+
LL | (&String::new()).assert_static();
5+
| --^^^^^^^^^^^^^------------------ temporary value is freed at the end of this statement
6+
| | |
7+
| | creates a temporary which is freed while still in use
8+
| argument requires that borrow lasts for `'static`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0716`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Regression test for #104005.
2+
//
3+
// Previously, different borrowck implementations used to disagree here.
4+
// The status of each is documented on `fn test_*`.
5+
6+
// check-fail
7+
8+
use std::fmt::Display;
9+
10+
trait Displayable {
11+
fn display(self) -> Box<dyn Display>;
12+
}
13+
14+
impl<T: Display> Displayable for (T, Option<&'static T>) {
15+
fn display(self) -> Box<dyn Display> {
16+
Box::new(self.0)
17+
}
18+
}
19+
20+
fn extend_lt<T, U>(val: T) -> Box<dyn Display>
21+
where
22+
(T, Option<U>): Displayable,
23+
{
24+
Displayable::display((val, None))
25+
}
26+
27+
// AST: fail
28+
// HIR: pass
29+
// MIR: pass
30+
pub fn test_call<'a>(val: &'a str) {
31+
extend_lt(val);
32+
//~^ ERROR borrowed data escapes outside of function
33+
}
34+
35+
// AST: fail
36+
// HIR: fail
37+
// MIR: pass
38+
pub fn test_coercion<'a>() {
39+
let _: fn(&'a str) -> _ = extend_lt;
40+
//~^ ERROR lifetime may not live long enough
41+
}
42+
43+
// AST: fail
44+
// HIR: fail
45+
// MIR: fail
46+
pub fn test_arg() {
47+
fn want<I, O>(_: I, _: impl Fn(I) -> O) {}
48+
want(&String::new(), extend_lt);
49+
//~^ ERROR temporary value dropped while borrowed
50+
}
51+
52+
// An exploit of the unsoundness.
53+
fn main() {
54+
let val = extend_lt(&String::from("blah blah blah"));
55+
//~^ ERROR temporary value dropped while borrowed
56+
println!("{}", val);
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0521]: borrowed data escapes outside of function
2+
--> $DIR/fn-item-check-type-params.rs:31:5
3+
|
4+
LL | pub fn test_call<'a>(val: &'a str) {
5+
| -- --- `val` is a reference that is only valid in the function body
6+
| |
7+
| lifetime `'a` defined here
8+
LL | extend_lt(val);
9+
| ^^^^^^^^^^^^^^
10+
| |
11+
| `val` escapes the function body here
12+
| argument requires that `'a` must outlive `'static`
13+
14+
error: lifetime may not live long enough
15+
--> $DIR/fn-item-check-type-params.rs:39:12
16+
|
17+
LL | pub fn test_coercion<'a>() {
18+
| -- lifetime `'a` defined here
19+
LL | let _: fn(&'a str) -> _ = extend_lt;
20+
| ^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
21+
22+
error[E0716]: temporary value dropped while borrowed
23+
--> $DIR/fn-item-check-type-params.rs:48:11
24+
|
25+
LL | want(&String::new(), extend_lt);
26+
| ------^^^^^^^^^^^^^------------- temporary value is freed at the end of this statement
27+
| | |
28+
| | creates a temporary which is freed while still in use
29+
| argument requires that borrow lasts for `'static`
30+
31+
error[E0716]: temporary value dropped while borrowed
32+
--> $DIR/fn-item-check-type-params.rs:54:26
33+
|
34+
LL | let val = extend_lt(&String::from("blah blah blah"));
35+
| -----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
36+
| | |
37+
| | creates a temporary which is freed while still in use
38+
| argument requires that borrow lasts for `'static`
39+
40+
error: aborting due to 4 previous errors
41+
42+
Some errors have detailed explanations: E0521, E0716.
43+
For more information about an error, try `rustc --explain E0521`.

src/test/ui/higher-rank-trait-bounds/issue-59311.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ where
1717
v.t(|| {});
1818
//~^ ERROR: higher-ranked lifetime error
1919
//~| ERROR: higher-ranked lifetime error
20+
//~| ERROR: higher-ranked lifetime error
2021
}
2122

2223
fn main() {}

src/test/ui/higher-rank-trait-bounds/issue-59311.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | v.t(|| {});
66
|
77
= note: could not prove `[closure@$DIR/issue-59311.rs:17:9: 17:11] well-formed`
88

9+
error: higher-ranked lifetime error
10+
--> $DIR/issue-59311.rs:17:5
11+
|
12+
LL | v.t(|| {});
13+
| ^^^^^^^^^^
14+
|
15+
= note: could not prove `[closure@$DIR/issue-59311.rs:17:9: 17:11] well-formed`
16+
917
error: higher-ranked lifetime error
1018
--> $DIR/issue-59311.rs:17:9
1119
|
@@ -14,5 +22,5 @@ LL | v.t(|| {});
1422
|
1523
= note: could not prove `for<'a> &'a V: 'static`
1624

17-
error: aborting due to 2 previous errors
25+
error: aborting due to 3 previous errors
1826

src/test/ui/lifetimes/lifetime-errors/issue_74400.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ fn f<T, S>(data: &[T], key: impl Fn(&T) -> S) {
1111
fn g<T>(data: &[T]) {
1212
f(data, identity)
1313
//~^ ERROR the parameter type
14+
//~| ERROR the parameter type
15+
//~| ERROR the parameter type
1416
//~| ERROR mismatched types
1517
//~| ERROR implementation of `FnOnce` is not general
1618
}

src/test/ui/lifetimes/lifetime-errors/issue_74400.stderr

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ help: consider adding an explicit lifetime bound...
99
LL | fn g<T: 'static>(data: &[T]) {
1010
| +++++++++
1111

12+
error[E0310]: the parameter type `T` may not live long enough
13+
--> $DIR/issue_74400.rs:12:5
14+
|
15+
LL | f(data, identity)
16+
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
17+
|
18+
help: consider adding an explicit lifetime bound...
19+
|
20+
LL | fn g<T: 'static>(data: &[T]) {
21+
| +++++++++
22+
23+
error[E0310]: the parameter type `T` may not live long enough
24+
--> $DIR/issue_74400.rs:12:5
25+
|
26+
LL | f(data, identity)
27+
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
28+
|
29+
help: consider adding an explicit lifetime bound...
30+
|
31+
LL | fn g<T: 'static>(data: &[T]) {
32+
| +++++++++
33+
1234
error[E0308]: mismatched types
1335
--> $DIR/issue_74400.rs:12:5
1436
|
@@ -32,7 +54,7 @@ LL | f(data, identity)
3254
= note: `fn(&'2 T) -> &'2 T {identity::<&'2 T>}` must implement `FnOnce<(&'1 T,)>`, for any lifetime `'1`...
3355
= note: ...but it actually implements `FnOnce<(&'2 T,)>`, for some specific lifetime `'2`
3456

35-
error: aborting due to 3 previous errors
57+
error: aborting due to 5 previous errors
3658

3759
Some errors have detailed explanations: E0308, E0310.
3860
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)