Skip to content

Commit fd8e175

Browse files
committed
Auto merge of #26859 - arielb1:const-deref-again, r=eddyb
Fixes #25901 r? @eddyb
2 parents e6a9be1 + 45fd296 commit fd8e175

File tree

7 files changed

+58
-18
lines changed

7 files changed

+58
-18
lines changed

src/librustc/diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1237,5 +1237,6 @@ register_diagnostics! {
12371237
E0314, // closure outlives stack frame
12381238
E0315, // cannot invoke closure outside of its lifetime
12391239
E0316, // nested quantification of lifetimes
1240-
E0370 // discriminant overflow
1240+
E0370, // discriminant overflow
1241+
E0400 // overloaded derefs are not allowed in constants
12411242
}

src/librustc/middle/cfg/construct.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
332332
}
333333

334334
ast::ExprIndex(ref l, ref r) |
335-
ast::ExprBinary(_, ref l, ref r) if self.is_method_call(expr) => {
335+
ast::ExprBinary(_, ref l, ref r) if self.tcx.is_method_call(expr.id) => {
336336
self.call(expr, pred, &**l, Some(&**r).into_iter())
337337
}
338338

@@ -342,7 +342,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
342342
self.straightline(expr, pred, fields)
343343
}
344344

345-
ast::ExprUnary(_, ref e) if self.is_method_call(expr) => {
345+
ast::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
346346
self.call(expr, pred, &**e, None::<ast::Expr>.iter())
347347
}
348348

@@ -631,9 +631,4 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
631631
}
632632
}
633633
}
634-
635-
fn is_method_call(&self, expr: &ast::Expr) -> bool {
636-
let method_call = ty::MethodCall::expr(expr.id);
637-
self.tcx.tables.borrow().method_map.contains_key(&method_call)
638-
}
639634
}

src/librustc/middle/check_const.rs

+20
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
405405

406406
let node_ty = self.tcx.node_id_to_type(ex.id);
407407
check_expr(self, ex, node_ty);
408+
check_adjustments(self, ex);
408409

409410
// Special-case some expressions to avoid certain flags bubbling up.
410411
match ex.node {
@@ -777,6 +778,25 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
777778
}
778779
}
779780

781+
/// Check the adjustments of an expression
782+
fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &ast::Expr) {
783+
match v.tcx.tables.borrow().adjustments.get(&e.id) {
784+
None | Some(&ty::AdjustReifyFnPointer) | Some(&ty::AdjustUnsafeFnPointer) => {}
785+
Some(&ty::AdjustDerefRef(ty::AutoDerefRef { autoderefs, .. })) => {
786+
if (0..autoderefs as u32).any(|autoderef| {
787+
v.tcx.is_overloaded_autoderef(e.id, autoderef)
788+
}) {
789+
v.add_qualif(ConstQualif::NOT_CONST);
790+
if v.mode != Mode::Var {
791+
span_err!(v.tcx.sess, e.span, E0400,
792+
"user-defined dereference operators are not allowed in {}s",
793+
v.msg());
794+
}
795+
}
796+
}
797+
}
798+
}
799+
780800
pub fn check_crate(tcx: &ty::ctxt) {
781801
visit::walk_crate(&mut CheckCrateVisitor {
782802
tcx: tcx,

src/librustc/middle/ty.rs

+5
Original file line numberDiff line numberDiff line change
@@ -6631,6 +6631,11 @@ impl<'tcx> ctxt<'tcx> {
66316631
self.tables.borrow().method_map.contains_key(&MethodCall::expr(expr_id))
66326632
}
66336633

6634+
pub fn is_overloaded_autoderef(&self, expr_id: ast::NodeId, autoderefs: u32) -> bool {
6635+
self.tables.borrow().method_map.contains_key(&MethodCall::autoderef(expr_id,
6636+
autoderefs))
6637+
}
6638+
66346639
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture> {
66356640
Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
66366641
}

src/librustc_trans/trans/expr.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
367367
match datum.ty.sty {
368368
// Don't skip a conversion from Box<T> to &T, etc.
369369
ty::TyRef(..) => {
370-
let method_call = MethodCall::autoderef(expr.id, 0);
371-
if bcx.tcx().tables.borrow().method_map.contains_key(&method_call) {
370+
if bcx.tcx().is_overloaded_autoderef(expr.id, 0) {
372371
// Don't skip an overloaded deref.
373372
0
374373
} else {
@@ -1612,9 +1611,7 @@ fn trans_unary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
16121611
// The only overloaded operator that is translated to a datum
16131612
// is an overloaded deref, since it is always yields a `&T`.
16141613
// Otherwise, we should be in the RvalueDpsExpr path.
1615-
assert!(
1616-
op == ast::UnDeref ||
1617-
!ccx.tcx().tables.borrow().method_map.contains_key(&method_call));
1614+
assert!(op == ast::UnDeref || !ccx.tcx().is_method_call(expr.id));
16181615

16191616
let un_ty = expr_ty(bcx, expr);
16201617

@@ -1907,7 +1904,7 @@ fn trans_binary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
19071904
let ccx = bcx.ccx();
19081905

19091906
// if overloaded, would be RvalueDpsExpr
1910-
assert!(!ccx.tcx().tables.borrow().method_map.contains_key(&MethodCall::expr(expr.id)));
1907+
assert!(!ccx.tcx().is_method_call(expr.id));
19111908

19121909
match op.node {
19131910
ast::BiAnd => {
@@ -2141,7 +2138,7 @@ fn trans_assign_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
21412138
debug!("trans_assign_op(expr={:?})", expr);
21422139

21432140
// User-defined operator methods cannot be used with `+=` etc right now
2144-
assert!(!bcx.tcx().tables.borrow().method_map.contains_key(&MethodCall::expr(expr.id)));
2141+
assert!(!bcx.tcx().is_method_call(expr.id));
21452142

21462143
// Evaluate LHS (destination), which should be an lvalue
21472144
let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, dst, "assign_op"));
@@ -2606,7 +2603,7 @@ enum ExprKind {
26062603
}
26072604

26082605
fn expr_kind(tcx: &ty::ctxt, expr: &ast::Expr) -> ExprKind {
2609-
if tcx.tables.borrow().method_map.contains_key(&MethodCall::expr(expr.id)) {
2606+
if tcx.is_method_call(expr.id) {
26102607
// Overloaded operations are generally calls, and hence they are
26112608
// generated via DPS, but there are a few exceptions:
26122609
return match expr.node {

src/librustc_typeck/check/regionck.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
516516
type_must_outlive(rcx, infer::ExprTypeIsNotInScope(expr_ty, expr.span),
517517
expr_ty, ty::ReScope(CodeExtent::from_node_id(expr.id)));
518518

519-
let method_call = MethodCall::expr(expr.id);
520-
let has_method_map = rcx.fcx.inh.tables.borrow().method_map.contains_key(&method_call);
519+
let has_method_map = rcx.fcx.infcx().is_method_call(expr.id);
521520

522521
// Check any autoderefs or autorefs that appear.
523522
let adjustment = rcx.fcx.inh.tables.borrow().adjustments.get(&expr.id).map(|a| a.clone());

src/test/compile-fail/issue-25901.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2015 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+
struct A;
12+
struct B;
13+
14+
static S: &'static B = &A; //~ ERROR user-defined dereference operators
15+
16+
use std::ops::Deref;
17+
18+
impl Deref for A {
19+
type Target = B;
20+
fn deref(&self)->&B { static B_: B = B; &B_ }
21+
}
22+
23+
fn main(){}

0 commit comments

Comments
 (0)