preliminary support for may-dangle attribute and drop constraints

This commit is contained in:
Niko Matsakis 2017-10-24 17:14:39 -04:00
parent e02937848b
commit af09f720d6
3 changed files with 164 additions and 14 deletions

View file

@ -0,0 +1,50 @@
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Basic test for liveness constraints: the region (`R1`) that appears
// in the type of `p` includes the points after `&v[0]` up to (but not
// including) the call to `use_x`. The `else` branch is not included.
// ignore-tidy-linelength
// compile-flags:-Znll -Zverbose
// ^^^^^^^^^ force compiler to dump more region information
#![allow(warnings)]
fn use_x(_: usize) -> bool { true }
fn main() {
let mut v = [1, 2, 3];
let p: Wrap<& /* R1 */ usize> = Wrap { value: &v[0] };
if true {
use_x(*p.value);
} else {
use_x(22);
}
// `p` will get dropped here. Because the `#[may_dangle]`
// attribute is not present on `Wrap`, we must conservatively
// assume that the dtor may access the `value` field, and hence we
// must consider R1 to be live.
}
struct Wrap<T> {
value: T
}
// Look ma, no `#[may_dangle]` attribute here.
impl<T> Drop for Wrap<T> {
fn drop(&mut self) { }
}
// END RUST SOURCE
// START rustc.node12.nll.0.mir
// | R4: {bb1[3], bb1[4], bb1[5], bb2[0], bb2[1], bb2[2], bb3[0], bb4[0], bb4[1], bb4[2], bb6[0], bb7[0], bb7[1], bb8[0]}
// END rustc.node12.nll.0.mir