Skip to content

Commit

Permalink
Do not consider using a semicolon inside of a different-crate macro
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Feb 14, 2021
1 parent 7fafa4d commit 506a0df
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
expected.is_unit(),
pointing_at_return_type,
) {
if cond_expr.span.desugaring_kind().is_none() {
// If the block is from a macro, then do not suggest
// adding a semicolon, because there's nowhere to put it.
// See issue #81943.
let hir = fcx.tcx.hir();
let body_owner =
hir.body_owner(hir.body_owned_by(hir.enclosing_body_owner(fcx.body_id)));
let from_same_crate = cond_expr.span.ctxt().dollar_crate_name()
== hir.span(body_owner).ctxt().dollar_crate_name();
if cond_expr.span.desugaring_kind().is_none() && from_same_crate {
err.span_label(cond_expr.span, "expected this to be `()`");
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/typeck/issue-81943.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn f<F: Fn(i32)>(f: F) { f(0); }
fn main() {
f(|x| dbg!(x)); //~ERROR
f(|x| match x { tmp => { tmp } }); //~ERROR
}
20 changes: 20 additions & 0 deletions src/test/ui/typeck/issue-81943.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0308]: mismatched types
--> $DIR/issue-81943.rs:3:9
|
LL | f(|x| dbg!(x));
| ^^^^^^^ expected `()`, found `i32`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/issue-81943.rs:4:28
|
LL | f(|x| match x { tmp => { tmp } });
| -------------------^^^----- help: consider using a semicolon here
| | |
| | expected `()`, found `i32`
| expected this to be `()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 506a0df

Please sign in to comment.