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.
This commit is contained in:
parent
8b7c17db22
commit
fbe8066ac3
2 changed files with 54 additions and 0 deletions
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
|
26
src/test/compile-fail/associated-const-type-parameters.rs
Normal file
26
src/test/compile-fail/associated-const-type-parameters.rs
Normal file
|
@ -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() {}
|
Loading…
Add table
Add a link
Reference in a new issue