Skip to content

Commit 81cfaad

Browse files
committed
Auto merge of #53212 - sunjay:nll-raw-cast, r=nikomatsakis
NLL - Prevent where clauses from extending the lifetime of bindings Fixes #53123 r? @nikomatsakis
2 parents afd0a2f + 6447651 commit 81cfaad

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/librustc/traits/select.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ pub struct SelectionCache<'tcx> {
226226
/// parameter environment.
227227
#[derive(PartialEq,Eq,Debug,Clone)]
228228
enum SelectionCandidate<'tcx> {
229+
/// If has_nested is false, there are no *further* obligations
229230
BuiltinCandidate { has_nested: bool },
230231
ParamCandidate(ty::PolyTraitRef<'tcx>),
231232
ImplCandidate(DefId),
@@ -2039,12 +2040,20 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20392040
}
20402041

20412042
match other.candidate {
2043+
// Prefer BuiltinCandidate { has_nested: false } to anything else.
2044+
// This is a fix for #53123 and prevents winnowing from accidentally extending the
2045+
// lifetime of a variable.
2046+
BuiltinCandidate { has_nested: false } => true,
20422047
ParamCandidate(ref cand) => match victim.candidate {
20432048
AutoImplCandidate(..) => {
20442049
bug!(
20452050
"default implementations shouldn't be recorded \
20462051
when there are other valid candidates");
20472052
}
2053+
// Prefer BuiltinCandidate { has_nested: false } to anything else.
2054+
// This is a fix for #53123 and prevents winnowing from accidentally extending the
2055+
// lifetime of a variable.
2056+
BuiltinCandidate { has_nested: false } => false,
20482057
ImplCandidate(..) |
20492058
ClosureCandidate |
20502059
GeneratorCandidate |
@@ -2072,6 +2081,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20722081
"default implementations shouldn't be recorded \
20732082
when there are other valid candidates");
20742083
}
2084+
// Prefer BuiltinCandidate { has_nested: false } to anything else.
2085+
// This is a fix for #53123 and prevents winnowing from accidentally extending the
2086+
// lifetime of a variable.
2087+
BuiltinCandidate { has_nested: false } => false,
20752088
ImplCandidate(..) |
20762089
ClosureCandidate |
20772090
GeneratorCandidate |
@@ -2115,7 +2128,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21152128
FnPointerCandidate |
21162129
BuiltinObjectCandidate |
21172130
BuiltinUnsizeCandidate |
2118-
BuiltinCandidate { .. } => {
2131+
BuiltinCandidate { has_nested: true } => {
21192132
match victim.candidate {
21202133
ParamCandidate(ref cand) => {
21212134
// Prefer these to a global where-clause bound
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(nll)]
12+
#![allow(unused_variables)]
13+
14+
pub trait TryTransform {
15+
fn try_transform<F>(self, f: F)
16+
where
17+
Self: Sized,
18+
F: FnOnce(Self);
19+
}
20+
21+
impl<'a, T> TryTransform for &'a mut T {
22+
fn try_transform<F>(self, f: F)
23+
where
24+
// The bug was that `Self: Sized` caused the lifetime of `this` to "extend" for all
25+
// of 'a instead of only lasting as long as the binding is used (for just that line).
26+
Self: Sized,
27+
F: FnOnce(Self),
28+
{
29+
let this: *mut T = self as *mut T;
30+
f(self);
31+
}
32+
}
33+
34+
fn main() {
35+
}

0 commit comments

Comments
 (0)