Navigation Menu

Skip to content

Commit

Permalink
Catch associated consts that depend on type parameters in type checking.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
quantheory committed May 17, 2015
1 parent 8b7c17d commit fbe8066
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -3765,6 +3765,21 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
{
// If fully resolved already, we don't have to do anything.
if path_res.depth == 0 {
// Associated constants can't depend on generic types.
if let Some(ty) = opt_self_ty {
match path_res.full_def() {
def::DefAssociatedConst(..) => {
if ty::type_has_params(ty) || ty::type_has_self(ty) {
fcx.sess().span_err(span,
"Associated consts cannot depend \
on type parameters or Self.");
fcx.write_error(node_id);
return None;
}
}
_ => {}
}
}
Some((opt_self_ty, &path.segments, path_res.base_def))
} else {
let mut def = path_res.base_def;
Expand All @@ -3780,6 +3795,19 @@ pub fn resolve_ty_and_def_ufcs<'a, 'b, 'tcx>(fcx: &FnCtxt<'b, 'tcx>,
let item_name = item_segment.identifier.name;
match method::resolve_ufcs(fcx, span, item_name, ty, node_id) {
Ok((def, lp)) => {
// Associated constants can't depend on generic types.
match def {
def::DefAssociatedConst(..) => {
if ty::type_has_params(ty) || ty::type_has_self(ty) {
fcx.sess().span_err(span,
"Associated consts cannot depend \
on type parameters or Self.");
fcx.write_error(node_id);
return None;
}
}
_ => {}
}
// Write back the new resolution.
fcx.ccx.tcx.def_map.borrow_mut()
.insert(node_id, def::PathResolution {
Expand Down
26 changes: 26 additions & 0 deletions src/test/compile-fail/associated-const-type-parameters.rs
@@ -0,0 +1,26 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

pub trait Foo {
const MIN: i32;

fn get_min() -> i32 {
Self::MIN //~ Associated consts cannot depend on type parameters or Self.
}
}

fn get_min<T: Foo>() -> i32 {
T::MIN; //~ Associated consts cannot depend on type parameters or Self.
<T as Foo>::MIN //~ Associated consts cannot depend on type parameters or Self.
}

fn main() {}

0 comments on commit fbe8066

Please sign in to comment.