Could someone please explain the reason behind this compile error:
pub trait Test {
const ASSOC_CONST: u32;
fn example(&self, some_var: u32) {
match some_var {
Self::ASSOC_CONST => {
println!("matched assoc constant with value defined by concrete type")
}
_ => println!("didn't match"),
}
}
}
I can't find a reason for the associated consts part of this error, E0158 (E0158 - Error codes index), either on the linked page or on github etc. The linked page merely states that statics are bad in patterns while constants are fine (obviously not so in this case). While I have very limited knowledge of the Rust compiler/ complex language stuff, I don't really understand the difference between normal constants and constants on traits, surely just a superficial difference.
Relevant compiler code:
compiler/rustc_mir_build/src/thir/pattern/mod.rs:492
:
let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, substs) {
Ok(Some(i)) => i,
Ok(None) => {
// It should be assoc consts if there's no error but we cannot resolve it.
debug_assert!(is_associated_const);
self.errors.push(PatternError::AssocConstInPattern(span));
return pat_from_kind(PatKind::Wild);
}
Err(_) => {
self.tcx.sess.span_err(span, "could not evaluate constant pattern");
return pat_from_kind(PatKind::Wild);
}
};
which also doesn't contain rationale.