Skip to content

Conversation

@enthropy7
Copy link
Contributor

@enthropy7 enthropy7 commented Jan 21, 2026

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 21, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 8e6d217 to ca0e82b Compare January 21, 2026 15:10
Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

View changes since this review

@Kivooeo Kivooeo added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2026
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from ca0e82b to 4cf78f8 Compare January 21, 2026 17:08
@Kivooeo
Copy link
Member

Kivooeo commented Jan 21, 2026

r? me

@rustbot rustbot assigned Kivooeo and unassigned chenyukang Jan 21, 2026
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 4cf78f8 to 7cac54f Compare January 21, 2026 17:15
Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

View changes since this review

@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Kivooeo
Copy link
Member

Kivooeo commented Jan 21, 2026

also please consider @mu001999 suggestion about other macros like assert_eq , loop suggestion is not relevant with this solution

@Kivooeo
Copy link
Member

Kivooeo commented Jan 21, 2026

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!()
}

@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch 4 times, most recently from 2b55dbd to bb341ca Compare January 21, 2026 19:28
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from bb341ca to cad8133 Compare January 21, 2026 19:35
Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

View changes since this review

@enthropy7
Copy link
Contributor Author

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

@rust-log-analyzer

This comment has been minimized.

@Kivooeo
Copy link
Member

Kivooeo commented Jan 21, 2026

you need to run ./x test ui --bless

@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 63616cb to 7603670 Compare January 21, 2026 20:29
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 21, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jan 21, 2026
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 7603670 to 63616cb Compare January 21, 2026 20:30
@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2026

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.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 21, 2026
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 63616cb to c6f238e Compare January 21, 2026 20:30
@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from c6f238e to 62bc11b Compare January 21, 2026 21:35
@rust-log-analyzer

This comment has been minimized.

@enthropy7 enthropy7 force-pushed the fix-assert-macro-only branch from 62bc11b to 95b58ac Compare January 21, 2026 21:50
@Kivooeo
Copy link
Member

Kivooeo commented Jan 21, 2026

r=me when ci green

@bors delegate+

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 21, 2026

✌️ @enthropy7, you can now approve this pull request!

If @Kivooeo told you to "r=me" after making some further change, then please make that change and post @bors r=Kivooeo.

@enthropy7
Copy link
Contributor Author

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 21, 2026

📌 Commit 95b58ac has been approved by enthropy7

It is now in the queue for this repository.

@rust-bors rust-bors bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 21, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 22, 2026
…=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
rust-bors bot pushed a commit that referenced this pull request Jan 22, 2026
Rollup of 4 pull requests

Successful merges:

 - #151118 (Support slices in type reflection)
 - #151439 (Bump bootstrap compiler to 1.94)
 - #151442 (Port `#![crate_type]` to the attribute parser)
 - #151457 (Improve error message for assert!() macro in functions returning bool)

r? @ghost
@rust-bors rust-bors bot merged commit d43b667 into rust-lang:main Jan 22, 2026
11 checks passed
rust-timer added a commit that referenced this pull request Jan 22, 2026
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
@rustbot rustbot added this to the 1.95.0 milestone Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert!() gives error: "if may be missing an else clause"

7 participants