Update associated constants error message

This commit is contained in:
Guillaume Gomez 2018-01-30 14:20:20 +01:00
parent 1b193de98a
commit f6a6d84031
3 changed files with 24 additions and 5 deletions

View file

@ -127,13 +127,16 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
} }
} }
impl<'a, 'tcx> PatternContext<'a, 'tcx> { impl<'a, 'tcx> PatternContext<'a, 'tcx> {
fn report_inlining_errors(&self, pat_span: Span) { fn report_inlining_errors(&self, pat_span: Span) {
for error in &self.errors { for error in &self.errors {
match *error { match *error {
PatternError::StaticInPattern(span) => { PatternError::StaticInPattern(span) => {
span_err!(self.tcx.sess, span, E0158, self.span_e0158(span, "statics cannot be referenced in patterns")
"statics cannot be referenced in patterns"); }
PatternError::AssociatedConstInPattern(span) => {
self.span_e0158(span, "associated consts cannot be referenced in patterns")
} }
PatternError::ConstEval(ref err) => { PatternError::ConstEval(ref err) => {
err.report(self.tcx, pat_span, "pattern"); err.report(self.tcx, pat_span, "pattern");
@ -141,6 +144,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
} }
} }
} }
fn span_e0158(&self, span: Span, text: &str) {
span_err!(self.tcx.sess, span, E0158, "{}", text)
}
} }
impl<'a, 'tcx> MatchVisitor<'a, 'tcx> { impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {

View file

@ -27,6 +27,7 @@ use syntax_pos::Span;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum PatternError<'tcx> { pub enum PatternError<'tcx> {
AssociatedConstInPattern(Span),
StaticInPattern(Span), StaticInPattern(Span),
ConstEval(ConstEvalErr<'tcx>), ConstEval(ConstEvalErr<'tcx>),
} }
@ -632,6 +633,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
-> Pattern<'tcx> { -> Pattern<'tcx> {
let ty = self.tables.node_id_to_type(id); let ty = self.tables.node_id_to_type(id);
let def = self.tables.qpath_def(qpath, id); let def = self.tables.qpath_def(qpath, id);
let is_associated_const = match def {
Def::AssociatedConst(_) => true,
_ => false,
};
let kind = match def { let kind = match def {
Def::Const(def_id) | Def::AssociatedConst(def_id) => { Def::Const(def_id) | Def::AssociatedConst(def_id) => {
let substs = self.tables.node_substs(id); let substs = self.tables.node_substs(id);
@ -653,7 +658,11 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
return pat; return pat;
} }
None => { None => {
self.errors.push(PatternError::StaticInPattern(span)); self.errors.push(if is_associated_const {
PatternError::AssociatedConstInPattern(span)
} else {
PatternError::StaticInPattern(span)
});
PatternKind::Wild PatternKind::Wild
} }
} }

View file

@ -16,6 +16,7 @@ pub trait Foo {
} }
struct Abc; struct Abc;
impl Foo for Abc { impl Foo for Abc {
const X: EFoo = EFoo::B; const X: EFoo = EFoo::B;
} }
@ -27,8 +28,10 @@ impl Foo for Def {
pub fn test<A: Foo, B: Foo>(arg: EFoo) { pub fn test<A: Foo, B: Foo>(arg: EFoo) {
match arg { match arg {
A::X => println!("A::X"), //~ error: statics cannot be referenced in patterns [E0158] A::X => println!("A::X"),
B::X => println!("B::X"), //~ error: statics cannot be referenced in patterns [E0158] //~^ error: associated consts cannot be referenced in patterns [E0158]
B::X => println!("B::X"),
//~^ error: associated consts cannot be referenced in patterns [E0158]
_ => (), _ => (),
} }
} }