Improve error message for assert!() macro in functions returning bool#151457
Improve error message for assert!() macro in functions returning bool#151457rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
This comment has been minimized.
This comment has been minimized.
8e6d217 to
ca0e82b
Compare
There was a problem hiding this comment.
thanks @mu001999 for this review!
but i think that this solving very different problem than original issue was about
i had a quick talk with issue author about it and we agreed that the problem lies in the error message itself
ca0e82b to
4cf78f8
Compare
|
r? me |
4cf78f8 to
7cac54f
Compare
There was a problem hiding this comment.
i'm pretty sure we could simplify this by a lot like this, what do you think about this
diff --git a/compiler/rustc_hir_typeck/src/_match.rs b/compiler/rustc_hir_typeck/src/_match.rs
index 09f780c6c9e..48f36d4277b 100644
--- a/compiler/rustc_hir_typeck/src/_match.rs
+++ b/compiler/rustc_hir_typeck/src/_match.rs
@@ -291,6 +291,13 @@ pub(super) fn if_fallback_coercion(
error
}
+ fn is_from_assert_macro(&self, span: Span) -> bool {
+ span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
+ self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
+ || self.tcx.is_diagnostic_item(sym::debug_assert_macro, def_id)
+ })
+ }
+
/// Explain why `if` expressions without `else` evaluate to `()` and detect likely irrefutable
/// `if let PAT = EXPR {}` expressions that could be turned into `let PAT = EXPR;`.
fn explain_if_expr(
@@ -302,26 +309,6 @@ fn explain_if_expr(
then_expr: &'tcx hir::Expr<'tcx>,
error: &mut bool,
) {
- // Check if this `if` expression comes from an `assert!()` or `debug_assert!()` macro expansion
- // Walk the macro expansion chain to find the root assert macro, similar to how panic_call does it
- let mut is_assert_macro = false;
- let mut expn = if_span.ctxt().outer_expn_data();
- loop {
- if let Some(def_id) = expn.macro_def_id {
- if self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
- || self.tcx.is_diagnostic_item(sym::debug_assert_macro, def_id)
- {
- is_assert_macro = true;
- break;
- }
- }
- let parent = expn.call_site.ctxt().outer_expn_data();
- if parent.macro_def_id.is_none() {
- break;
- }
- expn = parent;
- }
-
if let Some((if_span, msg)) = ret_reason {
err.span_label(if_span, msg);
} else if let ExprKind::Block(block, _) = then_expr.kind
@@ -329,6 +316,7 @@ fn explain_if_expr(
{
err.span_label(expr.span, "found here");
}
+ let is_assert_macro = self.is_from_assert_macro(if_span);`
if is_assert_macro {
err.primary_message("mismatched types");|
Reminder, once the PR becomes ready for a review, use |
|
also please consider @mu001999 suggestion about other macros like |
|
and also please add test like this to make sure it works fine with nested macros macro_rules! g {
() => {
f!()
};
}
macro_rules! f {
() => {
assert!(1 < 2)
};
}
fn f() -> bool {
g!()
} |
2b55dbd to
bb341ca
Compare
bb341ca to
cad8133
Compare
There was a problem hiding this comment.
I appreciate you implementing the suggestions quickly, but I'd like to have more of a discussion. When I ask "what do you think about this", I'm genuinely interested in your thoughts - whether you see any issues with the approach, have alternative ideas, or questions about why we're doing it this way
Silent force-pushes make it hard to know if you understand the changes or just applying them mechanically. Could we have a bit more back-and-forth? It helps both of us and makes the review process more valuable
Code review is as much about collaboration and learning as it is about the code itself - that's what makes open source work well. I'd love to see more discussion from you in reviews in future PRs
Took a note. In this case - all corrections were completely right at to the place. We would solve a totally different problem without your help. Also i'm a little newbie in compile coding, so, my decisions may be not the most effective and idiomatic one, also i trust more to experienced people. That's why i'm a little shy to discuss some changes :3 |
This comment has been minimized.
This comment has been minimized.
|
you need to run |
63616cb to
7603670
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
7603670 to
63616cb
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
63616cb to
c6f238e
Compare
c6f238e to
62bc11b
Compare
This comment has been minimized.
This comment has been minimized.
62bc11b to
95b58ac
Compare
|
r=me when ci green @bors delegate+ |
|
✌️ @enthropy7, you can now approve this pull request! If @Kivooeo told you to " |
|
@bors r+ |
…=enthropy7 Improve error message for assert!() macro in functions returning bool Detect `assert!()` macro in error messages and provide clearer diagnostics When `assert!()` is used in a function returning a value, show a message explaining that `assert!()` doesn't return a value, instead of the generic "if may be missing an else clause" error. Fixes rust-lang#151446
Rollup merge of #151457 - enthropy7:fix-assert-macro-only, r=enthropy7 Improve error message for assert!() macro in functions returning bool Detect `assert!()` macro in error messages and provide clearer diagnostics When `assert!()` is used in a function returning a value, show a message explaining that `assert!()` doesn't return a value, instead of the generic "if may be missing an else clause" error. Fixes #151446
Detect
assert!()macro in error messages and provide clearer diagnosticsWhen
assert!()is used in a function returning a value, show a message explaining thatassert!()doesn't return a value, instead of the generic "if may be missing an else clause" error.Fixes #151446