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

Don't promote to 'static the result of dereferences. #47408

Merged
merged 1 commit into from
Feb 17, 2018
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
36 changes: 34 additions & 2 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
this.add(Qualif::STATIC);
}

this.add(Qualif::NOT_CONST);

let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
if let ty::TyRawPtr(_) = base_ty.sty {
this.add(Qualif::NOT_CONST);
if this.mode != Mode::Fn {
struct_span_err!(this.tcx.sess,
this.span, E0396,
Expand Down Expand Up @@ -570,7 +571,38 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {

fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
// Recurse through operands and places.
self.super_rvalue(rvalue, location);
if let Rvalue::Ref(region, kind, ref place) = *rvalue {
let mut is_reborrow = false;
if let Place::Projection(ref proj) = *place {
if let ProjectionElem::Deref = proj.elem {
let base_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);
if let ty::TyRef(..) = base_ty.sty {
is_reborrow = true;
}
}
}

if is_reborrow {
self.nest(|this| {
this.super_place(place, PlaceContext::Borrow {
region,
kind
}, location);
if !this.try_consume() {
return;
}

if this.qualif.intersects(Qualif::STATIC_REF) {
this.qualif = this.qualif - Qualif::STATIC_REF;
this.add(Qualif::STATIC);
}
});
} else {
self.super_rvalue(rvalue, location);
}
} else {
self.super_rvalue(rvalue, location);
}

match *rvalue {
Rvalue::Use(_) |
Expand Down
25 changes: 12 additions & 13 deletions src/librustc_passes/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,9 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
hir::ExprBox(_) => {
v.promotable = false;
}
hir::ExprUnary(op, ref inner) => {
match v.tables.node_id_to_type(inner.hir_id).sty {
ty::TyRawPtr(_) => {
assert!(op == hir::UnDeref);

v.promotable = false;
}
_ => {}
hir::ExprUnary(op, _) => {
if op == hir::UnDeref {
v.promotable = false;
}
}
hir::ExprBinary(op, ref lhs, _) => {
Expand Down Expand Up @@ -548,7 +543,8 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr) {
use rustc::ty::adjustment::*;

for adjustment in v.tables.expr_adjustments(e) {
let mut adjustments = v.tables.expr_adjustments(e).iter().peekable();
while let Some(adjustment) = adjustments.next() {
match adjustment.kind {
Adjust::NeverToAny |
Adjust::ReifyFnPointer |
Expand All @@ -558,11 +554,14 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp
Adjust::Borrow(_) |
Adjust::Unsize => {}

Adjust::Deref(ref overloaded) => {
if overloaded.is_some() {
v.promotable = false;
break;
Adjust::Deref(_) => {
if let Some(next_adjustment) = adjustments.peek() {
if let Adjust::Borrow(_) = next_adjustment.kind {
continue;
}
}
v.promotable = false;
break;
}
}
}
Expand Down