Skip to content

Commit 45fd296

Browse files
author
Ariel Ben-Yehuda
committed
mark user-defined derefs as non-constant
Fixes #25901
1 parent 445824b commit 45fd296

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/librustc/diagnostics.rs

Lines changed: 2 additions & 1 deletion
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/check_const.rs

Lines changed: 20 additions & 0 deletions
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/test/compile-fail/issue-25901.rs

Lines changed: 23 additions & 0 deletions
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)