Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,27 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
}
}


impl<'a, 'tcx> PatternContext<'a, 'tcx> {
fn report_inlining_errors(&self, pat_span: Span) {
for error in &self.errors {
match *error {
PatternError::StaticInPattern(span) => {
span_err!(self.tcx.sess, span, E0158,
"statics cannot be referenced in patterns");
self.span_e0158(span, "statics cannot be referenced in patterns")
}
PatternError::AssociatedConstInPattern(span) => {
self.span_e0158(span, "associated consts cannot be referenced in patterns")
}
PatternError::ConstEval(ref err) => {
err.report(self.tcx, pat_span, "pattern");
}
}
}
}

fn span_e0158(&self, span: Span, text: &str) {
span_err!(self.tcx.sess, span, E0158, "{}", text)
}
}

impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
Expand Down
11 changes: 10 additions & 1 deletion src/librustc_const_eval/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use syntax_pos::Span;

#[derive(Clone, Debug)]
pub enum PatternError<'tcx> {
AssociatedConstInPattern(Span),
StaticInPattern(Span),
ConstEval(ConstEvalErr<'tcx>),
}
Expand Down Expand Up @@ -632,6 +633,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
-> Pattern<'tcx> {
let ty = self.tables.node_id_to_type(id);
let def = self.tables.qpath_def(qpath, id);
let is_associated_const = match def {
Def::AssociatedConst(_) => true,
_ => false,
};
let kind = match def {
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
let substs = self.tables.node_substs(id);
Expand All @@ -653,7 +658,11 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
return pat;
}
None => {
self.errors.push(PatternError::StaticInPattern(span));
self.errors.push(if is_associated_const {
PatternError::AssociatedConstInPattern(span)
} else {
PatternError::StaticInPattern(span)
});
PatternKind::Wild
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/compile-fail/associated-const-type-parameter-arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait Foo {
}

struct Abc;

impl Foo for Abc {
const X: EFoo = EFoo::B;
}
Expand All @@ -27,8 +28,10 @@ impl Foo for Def {

pub fn test<A: Foo, B: Foo>(arg: EFoo) {
match arg {
A::X => println!("A::X"), //~ error: statics cannot be referenced in patterns [E0158]
B::X => println!("B::X"), //~ error: statics cannot be referenced in patterns [E0158]
A::X => println!("A::X"),
//~^ error: associated consts cannot be referenced in patterns [E0158]
B::X => println!("B::X"),
//~^ error: associated consts cannot be referenced in patterns [E0158]
_ => (),
}
}
Expand Down