From b26a472ccb32a520eb809505a7dc465f8050268f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 25 May 2021 11:15:02 +0200 Subject: Fix type mismatch caused by macros MacroStmts should be completely transparent, but it prevented coercion. (I should maybe give `infer_expr` and `infer_expr_inner` better names.) --- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/tests/coercion.rs | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index eab8fac91..79a732106 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -805,7 +805,7 @@ impl<'a> InferenceContext<'a> { None => self.table.new_float_var(), }, }, - Expr::MacroStmts { tail } => self.infer_expr(*tail, expected), + Expr::MacroStmts { tail } => self.infer_expr_inner(*tail, expected), }; // use a new type variable if we got unknown here let ty = self.insert_type_vars_shallow(ty); diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index bb568ea37..7e9fc8735 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -912,3 +912,47 @@ fn test() -> i32 { "#, ) } + +#[test] +fn panic_macro() { + check_infer_with_mismatches( + r#" +mod panic { + #[macro_export] + pub macro panic_2015 { + () => ( + $crate::panicking::panic("explicit panic") + ), + } +} + +mod panicking { + pub fn panic() -> ! { loop {} } +} + +#[rustc_builtin_macro = "core_panic"] +macro_rules! panic { + // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021` + // depending on the edition of the caller. + ($($arg:tt)*) => { + /* compiler built-in */ + }; +} + +fn main() { + panic!("internal error: entered unreachable code") +} + "#, + expect![[r#" + 190..201 '{ loop {} }': ! + 192..199 'loop {}': ! + 197..199 '{}': () + !0..24 '$crate...:panic': fn panic() -> ! + !0..42 '$crate...anic")': ! + !0..42 '$crate...anic")': ! + !0..70 '$crate...code")': ! + !25..41 '"expli...panic"': &str + 470..528 '{ ...de") }': () + "#]], + ); +} -- cgit v1.2.3