Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Beta] Backports #68189

Merged
merged 8 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: Interne
// not for MSVC or macOS
if builder.config.llvm_static_stdcpp &&
!target.contains("freebsd") &&
!target.contains("windows") &&
!target.contains("msvc") &&
!target.contains("apple") {
let file = compiler_file(builder,
builder.cxx(target).unwrap(),
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,6 @@ impl<'tcx> TyCtxt<'tcx> {

if self.is_mutable_static(def_id) {
self.mk_mut_ptr(static_ty)
} else if self.is_foreign_item(def_id) {
self.mk_imm_ptr(static_ty)
} else {
self.mk_imm_ref(self.lifetimes.re_erased, static_ty)
}
Expand Down
22 changes: 20 additions & 2 deletions src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the function `{}` returns",
"...but `{}` will be dropped here, when the {} returns",
name,
self.infcx.tcx.hir().name(fn_hir_id),
self.infcx
.tcx
.hir()
.opt_name(fn_hir_id)
.map(|name| format!("function `{}`", name))
.unwrap_or_else(|| {
match &self
.infcx
.tcx
.typeck_tables_of(self.mir_def_id)
.node_type(fn_hir_id)
.kind
{
ty::Closure(..) => "enclosing closure",
ty::Generator(..) => "enclosing generator",
kind => bug!("expected closure or generator, found {:?}", kind),
}
.to_string()
})
),
);

Expand Down
31 changes: 22 additions & 9 deletions src/librustc_mir/dataflow/move_paths/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
}
};

// The move path index of the first union that we find. Once this is
// some we stop creating child move paths, since moves from unions
// move the whole thing.
// We continue looking for other move errors though so that moving
// from `*(u.f: &_)` isn't allowed.
let mut union_path = None;

for (i, elem) in place.projection.iter().enumerate() {
let proj_base = &place.projection[..i];
let body = self.builder.body;
Expand All @@ -127,9 +134,8 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
InteriorOfTypeWithDestructor { container_ty: place_ty },
));
}
// move out of union - always move the entire union
ty::Adt(adt, _) if adt.is_union() => {
return Err(MoveError::UnionMove { path: base });
union_path.get_or_insert(base);
}
ty::Slice(_) => {
return Err(MoveError::cannot_move_out_of(
Expand All @@ -155,15 +161,22 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
_ => {}
};

base = self.add_move_path(base, elem, |tcx| {
Place {
base: place.base.clone(),
projection: tcx.intern_place_elems(&place.projection[..i+1]),
}
});
if union_path.is_none() {
base = self.add_move_path(base, elem, |tcx| {
Place {
base: place.base.clone(),
projection: tcx.intern_place_elems(&place.projection[..i+1]),
}
});
}
}

Ok(base)
if let Some(base) = union_path {
// Move out of union - always move the entire union.
Err(MoveError::UnionMove { path: base })
} else {
Ok(base)
}
}

fn add_move_path(
Expand Down
17 changes: 8 additions & 9 deletions src/librustc_mir/transform/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use rustc::mir::*;
use rustc::ty::{self, Ty};
use rustc::hir::def_id::DefId;
use syntax_pos::DUMMY_SP;

use super::Item as ConstCx;
Expand Down Expand Up @@ -33,12 +32,6 @@ pub trait Qualif {
/// of the type.
fn in_any_value_of_ty(_cx: &ConstCx<'_, 'tcx>, _ty: Ty<'tcx>) -> bool;

fn in_static(cx: &ConstCx<'_, 'tcx>, def_id: DefId) -> bool {
// `mir_const_qualif` does return the qualifs in the final value of a `static`, so we could
// use value-based qualification here, but we shouldn't do this without a good reason.
Self::in_any_value_of_ty(cx, cx.tcx.type_of(def_id))
}

fn in_projection_structurally(
cx: &ConstCx<'_, 'tcx>,
per_local: &impl Fn(Local) -> bool,
Expand Down Expand Up @@ -108,8 +101,14 @@ pub trait Qualif {
Operand::Move(ref place) => Self::in_place(cx, per_local, place.as_ref()),

Operand::Constant(ref constant) => {
if let Some(static_) = constant.check_static_ptr(cx.tcx) {
Self::in_static(cx, static_)
if constant.check_static_ptr(cx.tcx).is_some() {
// `mir_const_qualif` does return the qualifs in the final value of a `static`,
// so we could use value-based qualification here, but we shouldn't do this
// without a good reason.
//
// Note: this uses `constant.literal.ty` which is a reference or pointer to the
// type of the actual `static` item.
Self::in_any_value_of_ty(cx, constant.literal.ty)
} else if let ty::ConstKind::Unevaluated(def_id, _) = constant.literal.val {
// Don't peek inside trait associated constants.
if cx.tcx.trait_of_item(def_id).is_some() {
Expand Down
8 changes: 6 additions & 2 deletions src/libstd/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,12 @@ impl ChildStdio {

impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.program)?;
for arg in &self.args {
if self.program != self.args[0] {
write!(f, "[{:?}] ", self.program)?;
}
write!(f, "{:?}", self.args[0])?;

for arg in &self.args[1..] {
write!(f, " {:?}", arg)?;
}
Ok(())
Expand Down
71 changes: 71 additions & 0 deletions src/test/mir-opt/const-promotion-extern-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
extern "C" {
static X: i32;
}

static Y: i32 = 42;

static mut BAR: *const &'static i32 = [&Y].as_ptr();

static mut FOO: *const &'static i32 = [unsafe { &X }].as_ptr();

fn main() {}

// END RUST SOURCE
// START rustc.FOO.PromoteTemps.before.mir
// bb0: {
// ...
// _5 = const Scalar(AllocId(1).0x0) : &i32;
// _4 = &(*_5);
// _3 = [move _4];
// _2 = &_3;
// _1 = move _2 as &[&'static i32] (Pointer(Unsize));
// _0 = const core::slice::<impl [&'static i32]>::as_ptr(move _1) -> [return: bb2, unwind: bb1];
// }
// ...
// bb2: {
// StorageDead(_5);
// StorageDead(_3);
// return;
// }
// END rustc.FOO.PromoteTemps.before.mir
// START rustc.BAR.PromoteTemps.before.mir
// bb0: {
// ...
// _5 = const Scalar(AllocId(0).0x0) : &i32;
// _4 = &(*_5);
// _3 = [move _4];
// _2 = &_3;
// _1 = move _2 as &[&'static i32] (Pointer(Unsize));
// _0 = const core::slice::<impl [&'static i32]>::as_ptr(move _1) -> [return: bb2, unwind: bb1];
// }
// ...
// bb2: {
// StorageDead(_5);
// StorageDead(_3);
// return;
// }
// END rustc.BAR.PromoteTemps.before.mir
// START rustc.BAR.PromoteTemps.after.mir
// bb0: {
// ...
// _2 = &(promoted[0]: [&'static i32; 1]);
// _1 = move _2 as &[&'static i32] (Pointer(Unsize));
// _0 = const core::slice::<impl [&'static i32]>::as_ptr(move _1) -> [return: bb2, unwind: bb1];
// }
// ...
// bb2: {
// return;
// }
// END rustc.BAR.PromoteTemps.after.mir
// START rustc.FOO.PromoteTemps.after.mir
// bb0: {
// ...
// _2 = &(promoted[0]: [&'static i32; 1]);
// _1 = move _2 as &[&'static i32] (Pointer(Unsize));
// _0 = const core::slice::<impl [&'static i32]>::as_ptr(move _1) -> [return: bb2, unwind: bb1];
// }
// ...
// bb2: {
// return;
// }
// END rustc.FOO.PromoteTemps.after.mir
30 changes: 30 additions & 0 deletions src/test/ui/borrowck/move-from-union-field-issue-66500.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Moving from a reference/raw pointer should be an error, even when they're
// the field of a union.

#![feature(untagged_unions)]

union Pointers {
a: &'static String,
b: &'static mut String,
c: *const String,
d: *mut String,
}

unsafe fn move_ref(u: Pointers) -> String {
*u.a
//~^ ERROR cannot move out of `*u.a`
}
unsafe fn move_ref_mut(u: Pointers) -> String {
*u.b
//~^ ERROR cannot move out of `*u.b`
}
unsafe fn move_ptr(u: Pointers) -> String {
*u.c
//~^ ERROR cannot move out of `*u.c`
}
unsafe fn move_ptr_mut(u: Pointers) -> String {
*u.d
//~^ ERROR cannot move out of `*u.d`
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/borrowck/move-from-union-field-issue-66500.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0507]: cannot move out of `*u.a` which is behind a shared reference
--> $DIR/move-from-union-field-issue-66500.rs:14:5
|
LL | *u.a
| ^^^^ move occurs because `*u.a` has type `std::string::String`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `*u.b` which is behind a mutable reference
--> $DIR/move-from-union-field-issue-66500.rs:18:5
|
LL | *u.b
| ^^^^ move occurs because `*u.b` has type `std::string::String`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `*u.c` which is behind a raw pointer
--> $DIR/move-from-union-field-issue-66500.rs:22:5
|
LL | *u.c
| ^^^^ move occurs because `*u.c` has type `std::string::String`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `*u.d` which is behind a raw pointer
--> $DIR/move-from-union-field-issue-66500.rs:26:5
|
LL | *u.d
| ^^^^ move occurs because `*u.d` has type `std::string::String`, which does not implement the `Copy` trait

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0507`.
24 changes: 24 additions & 0 deletions src/test/ui/command/command-argv0-debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// run-pass

// ignore-windows - this is a unix-specific test
// ignore-cloudabi no processes
// ignore-emscripten no processes
// ignore-sgx no processes
#![feature(process_set_argv0)]

use std::os::unix::process::CommandExt;
use std::process::Command;

fn main() {
let mut command = Command::new("some-boring-name");

assert_eq!(format!("{:?}", command), r#""some-boring-name""#);

command.args(&["1", "2", "3"]);

assert_eq!(format!("{:?}", command), r#""some-boring-name" "1" "2" "3""#);

command.arg0("exciting-name");

assert_eq!(format!("{:?}", command), r#"["some-boring-name"] "exciting-name" "1" "2" "3""#);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-eval/promote-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// regression test for #67609.

// check-pass

static NONE: Option<String> = None;

static NONE_REF_REF: &&Option<String> = {
let x = &&NONE;
x
};

fn main() {
println!("{:?}", NONE_REF_REF);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
[0].iter().flat_map(|a| [0].iter().map(|_| &a)); //~ ERROR `a` does not live long enough
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0597]: `a` does not live long enough
--> $DIR/unnamed-closure-doesnt-life-long-enough-issue-67634.rs:2:49
|
LL | [0].iter().flat_map(|a| [0].iter().map(|_| &a));
| - ^- ...but `a` will be dropped here, when the enclosing closure returns
| | |
| | `a` would have to be valid for `'_`...
| has type `&i32`
|
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
= note: to learn more, visit <https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#dangling-references>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.