]> git.proxmox.com Git - rustc.git/blame - src/test/ui/nll/drop-may-dangle.rs
New upstream version 1.26.2+dfsg1
[rustc.git] / src / test / ui / nll / drop-may-dangle.rs
CommitLineData
abe05a73
XL
1// Copyright 2012-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// Basic test for liveness constraints: the region (`R1`) that appears
12// in the type of `p` includes the points after `&v[0]` up to (but not
13// including) the call to `use_x`. The `else` branch is not included.
14
ff7c6d11
XL
15// compile-flags:-Znll -Zborrowck=mir
16// must-compile-successfully
abe05a73
XL
17
18#![allow(warnings)]
19#![feature(dropck_eyepatch)]
20#![feature(generic_param_attrs)]
21
22fn use_x(_: usize) -> bool { true }
23
24fn main() {
25 let mut v = [1, 2, 3];
ff7c6d11 26 let p: WrapMayDangle<& /* R4 */ usize> = WrapMayDangle { value: &v[0] };
abe05a73 27 if true {
ff7c6d11
XL
28 // `p` will get dropped at end of this block. However, because of
29 // the `#[may_dangle]` attribute, we do not need to consider R4
30 // live after this point.
abe05a73
XL
31 use_x(*p.value);
32 } else {
ff7c6d11 33 v[0] += 1;
abe05a73
XL
34 use_x(22);
35 }
36
ff7c6d11 37 v[0] += 1;
abe05a73
XL
38}
39
ff7c6d11 40struct WrapMayDangle<T> {
abe05a73
XL
41 value: T
42}
43
ff7c6d11 44unsafe impl<#[may_dangle] T> Drop for WrapMayDangle<T> {
abe05a73
XL
45 fn drop(&mut self) { }
46}