From 07a5b212dd38c04a25249c7b97acb5db5969bc03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 18 Mar 2024 23:57:36 +0000 Subject: [PATCH] Accept `T` in `assert!` where `` in <=2021 editions --- compiler/rustc_builtin_macros/src/assert.rs | 76 +++++++++++-------- compiler/rustc_hir_typeck/src/demand.rs | 2 +- .../issue-28308.edition2021.stderr | 11 +++ ....stderr => issue-28308.edition2024.stderr} | 2 +- tests/ui/codemap_tests/issue-28308.rs | 7 +- tests/ui/issues/issue-14091-2.rs | 2 +- tests/ui/issues/issue-14091-2.stderr | 15 +++- tests/ui/issues/issue-14091.stderr | 2 + 8 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 tests/ui/codemap_tests/issue-28308.edition2021.stderr rename tests/ui/codemap_tests/{issue-28308.stderr => issue-28308.edition2024.stderr} (87%) diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 2ffa1d6305afb..3e2108413dcf4 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -11,6 +11,7 @@ use rustc_ast_pretty::pprust; use rustc_errors::PResult; use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult}; use rustc_parse::parser::Parser; +use rustc_span::edition::Edition; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use thin_vec::thin_vec; @@ -172,41 +173,37 @@ fn expand_cond(cx: &ExtCtxt<'_>, parser: Parser<'_>, cond_expr: P) -> P) -> Option { let ts = parser.parse_tokens(); if !ts.is_empty() { Some(ts) } else { None } } + +pub fn use_assert_2024(mut span: Span) -> bool { + // To determine the edition, we check the first span up the expansion + // stack that isn't internal. + loop { + let expn = span.ctxt().outer_expn_data(); + if let Some(_features) = expn.allow_internal_unstable { + span = expn.call_site; + continue; + } + break expn.edition >= Edition::Edition2024; + } +} diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 1927233796b3a..09a5c15c33f0c 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -679,7 +679,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) { match (self.tcx.parent_hir_node(expr.hir_id), error) { (hir::Node::Local(hir::Local { ty: Some(ty), init: Some(init), .. }), _) - if init.hir_id == expr.hir_id && !ty.span.is_dummy() => + if init.hir_id == expr.hir_id && !ty.span.source_equal(init.span) => { // Point at `let` assignment type. err.span_label(ty.span, "expected due to this"); diff --git a/tests/ui/codemap_tests/issue-28308.edition2021.stderr b/tests/ui/codemap_tests/issue-28308.edition2021.stderr new file mode 100644 index 0000000000000..6726c01907a4b --- /dev/null +++ b/tests/ui/codemap_tests/issue-28308.edition2021.stderr @@ -0,0 +1,11 @@ +error[E0600]: cannot apply unary operator `!` to type `&'static str` + --> $DIR/issue-28308.rs:6:13 + | +LL | assert!("foo"); + | ^^^^^ cannot apply unary operator `!` + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0600`. diff --git a/tests/ui/codemap_tests/issue-28308.stderr b/tests/ui/codemap_tests/issue-28308.edition2024.stderr similarity index 87% rename from tests/ui/codemap_tests/issue-28308.stderr rename to tests/ui/codemap_tests/issue-28308.edition2024.stderr index 6d7068199c188..08c1b12afe19a 100644 --- a/tests/ui/codemap_tests/issue-28308.stderr +++ b/tests/ui/codemap_tests/issue-28308.edition2024.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/issue-28308.rs:2:13 + --> $DIR/issue-28308.rs:6:13 | LL | assert!("foo"); | ^^^^^ expected `bool`, found `&str` diff --git a/tests/ui/codemap_tests/issue-28308.rs b/tests/ui/codemap_tests/issue-28308.rs index 3553c66ae16c4..49c330210051e 100644 --- a/tests/ui/codemap_tests/issue-28308.rs +++ b/tests/ui/codemap_tests/issue-28308.rs @@ -1,4 +1,9 @@ +//@revisions: edition2024 edition2021 +//@[edition2024] edition:2024 +//@[edition2024] compile-flags: -Z unstable-options +//@[edition2021] edition:2021 fn main() { assert!("foo"); - //~^ ERROR mismatched types + //[edition2024]~^ ERROR mismatched types + //[edition2021]~^^ ERROR cannot apply unary operator `!` to type `&'static str` } diff --git a/tests/ui/issues/issue-14091-2.rs b/tests/ui/issues/issue-14091-2.rs index 23163b8cf481a..e2f6b18337266 100644 --- a/tests/ui/issues/issue-14091-2.rs +++ b/tests/ui/issues/issue-14091-2.rs @@ -13,5 +13,5 @@ fn main() { let x = BytePos(1); assert!(x, x); - //~^ ERROR mismatched types + //~^ ERROR cannot apply unary operator `!` to type `BytePos` } diff --git a/tests/ui/issues/issue-14091-2.stderr b/tests/ui/issues/issue-14091-2.stderr index a7b6cdb682649..55d50ee5a54a8 100644 --- a/tests/ui/issues/issue-14091-2.stderr +++ b/tests/ui/issues/issue-14091-2.stderr @@ -1,9 +1,18 @@ -error[E0308]: mismatched types +error[E0600]: cannot apply unary operator `!` to type `BytePos` --> $DIR/issue-14091-2.rs:15:13 | LL | assert!(x, x); - | ^ expected `bool`, found `BytePos` + | ^ cannot apply unary operator `!` + | +note: an implementation of `Not` might be missing for `BytePos` + --> $DIR/issue-14091-2.rs:6:1 + | +LL | pub struct BytePos(pub u32); + | ^^^^^^^^^^^^^^^^^^ must implement `Not` +note: the trait `Not` must be implemented + --> $SRC_DIR/core/src/ops/bit.rs:LL:COL + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0308`. +For more information about this error, try `rustc --explain E0600`. diff --git a/tests/ui/issues/issue-14091.stderr b/tests/ui/issues/issue-14091.stderr index b00f651fb320f..53251699eb404 100644 --- a/tests/ui/issues/issue-14091.stderr +++ b/tests/ui/issues/issue-14091.stderr @@ -3,6 +3,8 @@ error[E0308]: mismatched types | LL | assert!(1,1); | ^ expected `bool`, found integer + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error