Skip to content

Commit fbe8066

Browse files
committed
Catch associated consts that depend on type parameters in type checking.
Constants with values that depend on generic parameters or `Self` cause ICEs in `check_const`, and are not yet accepted via RFC, so we need to throw a proper error in these cases.
1 parent 8b7c17d commit fbe8066

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/librustc_typeck/check/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3765,6 +3765,21 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37653765
{
37663766
// If fully resolved already, we don't have to do anything.
37673767
if path_res.depth == 0 {
3768+
// Associated constants can't depend on generic types.
3769+
if let Some(ty) = opt_self_ty {
3770+
match path_res.full_def() {
3771+
def::DefAssociatedConst(..) => {
3772+
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3773+
fcx.sess().span_err(span,
3774+
"Associated consts cannot depend \
3775+
on type parameters or Self.");
3776+
fcx.write_error(node_id);
3777+
return None;
3778+
}
3779+
}
3780+
_ => {}
3781+
}
3782+
}
37683783
Some((opt_self_ty, &path.segments, path_res.base_def))
37693784
} else {
37703785
let mut def = path_res.base_def;
@@ -3780,6 +3795,19 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37803795
let item_name = item_segment.identifier.name;
37813796
match method::resolve_ufcs(fcx, span, item_name, ty, node_id) {
37823797
Ok((def, lp)) => {
3798+
// Associated constants can't depend on generic types.
3799+
match def {
3800+
def::DefAssociatedConst(..) => {
3801+
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3802+
fcx.sess().span_err(span,
3803+
"Associated consts cannot depend \
3804+
on type parameters or Self.");
3805+
fcx.write_error(node_id);
3806+
return None;
3807+
}
3808+
}
3809+
_ => {}
3810+
}
37833811
// Write back the new resolution.
37843812
fcx.ccx.tcx.def_map.borrow_mut()
37853813
.insert(node_id, def::PathResolution {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#![feature(associated_consts)]
12+
13+
pub trait Foo {
14+
const MIN: i32;
15+
16+
fn get_min() -> i32 {
17+
Self::MIN //~ Associated consts cannot depend on type parameters or Self.
18+
}
19+
}
20+
21+
fn get_min<T: Foo>() -> i32 {
22+
T::MIN; //~ Associated consts cannot depend on type parameters or Self.
23+
<T as Foo>::MIN //~ Associated consts cannot depend on type parameters or Self.
24+
}
25+
26+
fn main() {}

0 commit comments

Comments
 (0)