From fbe8066ac39546073b4d76bcb9928cf83886e8b2 Mon Sep 17 00:00:00 2001 From: Sean Patrick Santos Date: Mon, 4 May 2015 01:39:10 -0600 Subject: [PATCH] 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. --- src/librustc_typeck/check/mod.rs | 28 +++++++++++++++++++ .../associated-const-type-parameters.rs | 26 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/test/compile-fail/associated-const-type-parameters.rs diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index f13305bba343e..7c270bf4a3003 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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 { diff --git a/src/test/compile-fail/associated-const-type-parameters.rs b/src/test/compile-fail/associated-const-type-parameters.rs new file mode 100644 index 0000000000000..bbe252fc15188 --- /dev/null +++ b/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 or the MIT license +// , 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() -> i32 { + T::MIN; //~ Associated consts cannot depend on type parameters or Self. + ::MIN //~ Associated consts cannot depend on type parameters or Self. +} + +fn main() {}