Skip to content

Commit f6a6d84

Browse files
Update associated constants error message
1 parent 1b193de commit f6a6d84

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/librustc_const_eval/check_match.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,27 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
127127
}
128128
}
129129

130+
130131
impl<'a, 'tcx> PatternContext<'a, 'tcx> {
131132
fn report_inlining_errors(&self, pat_span: Span) {
132133
for error in &self.errors {
133134
match *error {
134135
PatternError::StaticInPattern(span) => {
135-
span_err!(self.tcx.sess, span, E0158,
136-
"statics cannot be referenced in patterns");
136+
self.span_e0158(span, "statics cannot be referenced in patterns")
137+
}
138+
PatternError::AssociatedConstInPattern(span) => {
139+
self.span_e0158(span, "associated consts cannot be referenced in patterns")
137140
}
138141
PatternError::ConstEval(ref err) => {
139142
err.report(self.tcx, pat_span, "pattern");
140143
}
141144
}
142145
}
143146
}
147+
148+
fn span_e0158(&self, span: Span, text: &str) {
149+
span_err!(self.tcx.sess, span, E0158, "{}", text)
150+
}
144151
}
145152

146153
impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {

src/librustc_const_eval/pattern.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use syntax_pos::Span;
2727

2828
#[derive(Clone, Debug)]
2929
pub enum PatternError<'tcx> {
30+
AssociatedConstInPattern(Span),
3031
StaticInPattern(Span),
3132
ConstEval(ConstEvalErr<'tcx>),
3233
}
@@ -632,6 +633,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
632633
-> Pattern<'tcx> {
633634
let ty = self.tables.node_id_to_type(id);
634635
let def = self.tables.qpath_def(qpath, id);
636+
let is_associated_const = match def {
637+
Def::AssociatedConst(_) => true,
638+
_ => false,
639+
};
635640
let kind = match def {
636641
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
637642
let substs = self.tables.node_substs(id);
@@ -653,7 +658,11 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
653658
return pat;
654659
}
655660
None => {
656-
self.errors.push(PatternError::StaticInPattern(span));
661+
self.errors.push(if is_associated_const {
662+
PatternError::AssociatedConstInPattern(span)
663+
} else {
664+
PatternError::StaticInPattern(span)
665+
});
657666
PatternKind::Wild
658667
}
659668
}

src/test/compile-fail/associated-const-type-parameter-arms.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub trait Foo {
1616
}
1717

1818
struct Abc;
19+
1920
impl Foo for Abc {
2021
const X: EFoo = EFoo::B;
2122
}
@@ -27,8 +28,10 @@ impl Foo for Def {
2728

2829
pub fn test<A: Foo, B: Foo>(arg: EFoo) {
2930
match arg {
30-
A::X => println!("A::X"), //~ error: statics cannot be referenced in patterns [E0158]
31-
B::X => println!("B::X"), //~ error: statics cannot be referenced in patterns [E0158]
31+
A::X => println!("A::X"),
32+
//~^ error: associated consts cannot be referenced in patterns [E0158]
33+
B::X => println!("B::X"),
34+
//~^ error: associated consts cannot be referenced in patterns [E0158]
3235
_ => (),
3336
}
3437
}

0 commit comments

Comments
 (0)