Skip to content

Commit bbc0238

Browse files
committed
Auto merge of #31962 - sanxiyn:const-eval-map, r=arielb1
Fix #31910.
2 parents 0a52494 + 33bd144 commit bbc0238

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/librustc/middle/const_eval.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10191019
}
10201020
Some(Def::AssociatedConst(def_id)) => {
10211021
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
1022-
match tcx.impl_or_trait_item(def_id).container() {
1022+
match impl_or_trait_container(tcx, def_id) {
10231023
ty::TraitContainer(trait_id) => match tcx.map.find(node_id) {
10241024
Some(ast_map::NodeTraitItem(ti)) => match ti.node {
10251025
hir::ConstTraitItem(ref ty, _) => {
@@ -1222,6 +1222,23 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
12221222
Ok(result)
12231223
}
12241224

1225+
fn impl_or_trait_container(tcx: &ty::ctxt, def_id: DefId) -> ty::ImplOrTraitItemContainer {
1226+
// This is intended to be equivalent to tcx.impl_or_trait_item(def_id).container()
1227+
// for local def_id, but it can be called before tcx.impl_or_trait_items is complete.
1228+
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
1229+
if let Some(ast_map::NodeItem(item)) = tcx.map.find(tcx.map.get_parent_node(node_id)) {
1230+
let container_id = tcx.map.local_def_id(item.id);
1231+
match item.node {
1232+
hir::ItemImpl(..) => return ty::ImplContainer(container_id),
1233+
hir::ItemTrait(..) => return ty::TraitContainer(container_id),
1234+
_ => ()
1235+
}
1236+
}
1237+
panic!("No impl or trait container for {:?}", def_id);
1238+
}
1239+
panic!("{:?} is not local", def_id);
1240+
}
1241+
12251242
fn resolve_trait_associated_const<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
12261243
ti: &'tcx hir::TraitItem,
12271244
trait_id: DefId,

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 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+
#![feature(associated_consts)]
12+
13+
enum Enum<T: Trait> {
14+
X = Trait::Number, //~ ERROR constant evaluation error
15+
}
16+
17+
trait Trait {
18+
const Number: i32 = 1;
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)