提交 38587a7b 编写于 作者: K kennytm 提交者: GitHub

Rollup merge of #47876 - GuillaumeGomez:associated-const-error, r=nikomatsakis

Update associated constants error message

Fixes #47570.
......@@ -127,13 +127,16 @@ fn visit_body(&mut self, body: &'tcx hir::Body) {
}
}
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");
......@@ -141,6 +144,10 @@ fn report_inlining_errors(&self, pat_span: Span) {
}
}
}
fn span_e0158(&self, span: Span, text: &str) {
span_err!(self.tcx.sess, span, E0158, "{}", text)
}
}
impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
......
......@@ -27,6 +27,7 @@
#[derive(Clone, Debug)]
pub enum PatternError<'tcx> {
AssociatedConstInPattern(Span),
StaticInPattern(Span),
ConstEval(ConstEvalErr<'tcx>),
}
......@@ -635,6 +636,10 @@ fn lower_path(&mut self,
-> 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);
......@@ -656,7 +661,11 @@ fn lower_path(&mut self,
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
}
}
......
......@@ -16,6 +16,7 @@ pub trait Foo {
}
struct Abc;
impl Foo for Abc {
const X: EFoo = EFoo::B;
}
......@@ -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]
_ => (),
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册