Skip to content

Commit d6d2c68

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 6b3d66b commit d6d2c68

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,6 +3715,21 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37153715
{
37163716
// If fully resolved already, we don't have to do anything.
37173717
if path_res.depth == 0 {
3718+
// Associated constants can't depend on generic types.
3719+
if let Some(ty) = opt_self_ty {
3720+
match path_res.full_def() {
3721+
def::DefAssociatedConst(..) => {
3722+
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3723+
fcx.sess().span_err(span,
3724+
"Associated consts cannot depend \
3725+
on type parameters or Self.");
3726+
fcx.write_error(node_id);
3727+
return None;
3728+
}
3729+
}
3730+
_ => {}
3731+
}
3732+
}
37183733
Some((opt_self_ty, &path.segments, path_res.base_def))
37193734
} else {
37203735
let mut def = path_res.base_def;
@@ -3730,6 +3745,19 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
37303745
let item_name = item_segment.identifier.name;
37313746
match method::resolve_ufcs(fcx, span, item_name, ty, node_id) {
37323747
Ok((def, lp)) => {
3748+
// Associated constants can't depend on generic types.
3749+
match def {
3750+
def::DefAssociatedConst(..) => {
3751+
if ty::type_has_params(ty) || ty::type_has_self(ty) {
3752+
fcx.sess().span_err(span,
3753+
"Associated consts cannot depend \
3754+
on type parameters or Self.");
3755+
fcx.write_error(node_id);
3756+
return None;
3757+
}
3758+
}
3759+
_ => {}
3760+
}
37333761
// Write back the new resolution.
37343762
fcx.ccx.tcx.def_map.borrow_mut()
37353763
.insert(node_id, def::PathResolution {
Lines changed: 26 additions & 0 deletions
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)