Rollup merge of #89126 - FabianWolff:issue-89088, r=petrochenkov

Fix ICE when `indirect_structural_match` is allowed

Fixes #89088. The ICE is caused by `delay_good_path_bug()`, which is called (indirectly) from a `format!()` macro invocation. I have moved the macro invocation into the `decorate` closure of `struct_span_lint_hir()`, so that the macro is only invoked if the lint is not allowed (i.e., causes at least a warning, and thus prevents `delay_good_path_bug()` from firing).
This commit is contained in:
the8472 2021-09-21 22:54:06 +02:00 committed by GitHub
commit 8d95bb2146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -322,16 +322,18 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
&& !self.saw_const_match_lint.get()
{
self.saw_const_match_lint.set(true);
let msg = format!(
"to use a constant of type `{}` in a pattern, \
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
cv.ty, cv.ty,
);
tcx.struct_span_lint_hir(
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
id,
span,
|lint| lint.build(&msg).emit(),
|lint| {
let msg = format!(
"to use a constant of type `{}` in a pattern, \
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
cv.ty, cv.ty,
);
lint.build(&msg).emit()
},
);
}
// Since we are behind a reference, we can just bubble the error up so we get a

View File

@ -0,0 +1,22 @@
// Regression test for the ICE described in #89088.
// check-pass
#![allow(indirect_structural_match)]
use std::borrow::Cow;
const FOO: &A = &A::Field(Cow::Borrowed("foo"));
#[derive(PartialEq, Eq)]
enum A {
Field(Cow<'static, str>)
}
fn main() {
let var = A::Field(Cow::Borrowed("bar"));
match &var {
FOO => todo!(),
_ => todo!()
}
}