Skip to content

Commit af45f12

Browse files
committed
Rollup merge of #28686 - eefriedman:unresolved-path-error, r=nikomatsakis
The behavior here isn't really ideal, but we can't really do much better given the current state of constant evaluation. The changes to ExprUseVisitor are to avoid a compile error; apparently that bit of code is extremely sensitive to changes in other areas of the compiler. Fixes #28670, and probably a bunch of duplicates.
2 parents eb90af8 + 1763fcb commit af45f12

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/librustc/middle/const_eval.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ pub enum ErrKind {
367367
ShiftRightWithOverflow,
368368
MissingStructField,
369369
NonConstPath,
370+
UnresolvedPath,
370371
ExpectedConstTuple,
371372
ExpectedConstStruct,
372373
TupleIndexOutOfBounds,
@@ -403,7 +404,8 @@ impl ConstEvalErr {
403404
ShiftLeftWithOverflow => "attempted left shift with overflow".into_cow(),
404405
ShiftRightWithOverflow => "attempted right shift with overflow".into_cow(),
405406
MissingStructField => "nonexistent struct field".into_cow(),
406-
NonConstPath => "non-constant path in constant expr".into_cow(),
407+
NonConstPath => "non-constant path in constant expression".into_cow(),
408+
UnresolvedPath => "unresolved path in constant expression".into_cow(),
407409
ExpectedConstTuple => "expected constant tuple".into_cow(),
408410
ExpectedConstStruct => "expected constant struct".into_cow(),
409411
TupleIndexOutOfBounds => "tuple index out of bounds".into_cow(),
@@ -895,7 +897,20 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
895897
}
896898
}
897899
hir::ExprPath(..) => {
898-
let opt_def = tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
900+
let opt_def = if let Some(def) = tcx.def_map.borrow().get(&e.id) {
901+
// After type-checking, def_map contains definition of the
902+
// item referred to by the path. During type-checking, it
903+
// can contain the raw output of path resolution, which
904+
// might be a partially resolved path.
905+
// FIXME: There's probably a better way to make sure we don't
906+
// panic here.
907+
if def.depth != 0 {
908+
signal!(e, UnresolvedPath);
909+
}
910+
Some(def.full_def())
911+
} else {
912+
None
913+
};
899914
let (const_expr, const_ty) = match opt_def {
900915
Some(def::DefConst(def_id)) => {
901916
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
fn main() {
12+
fn f(a: [u8; u32::DOESNOTEXIST]) {}
13+
//~^ ERROR unresolved path in constant expression
14+
}

0 commit comments

Comments
 (0)