From bdfc68855861ecea26844a66c38c53f496386b7d Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 9 Jun 2022 16:07:36 -0700 Subject: [PATCH 01/23] Add drop tracking version of must_not_suspend ref test --- .../must_not_suspend/ref-drop-tracking.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/ui/lint/must_not_suspend/ref-drop-tracking.rs diff --git a/src/test/ui/lint/must_not_suspend/ref-drop-tracking.rs b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.rs new file mode 100644 index 0000000000000..1bc4a38125753 --- /dev/null +++ b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.rs @@ -0,0 +1,30 @@ +// edition:2018 +// compile-flags: -Zdrop-tracking +#![feature(must_not_suspend)] +#![deny(must_not_suspend)] + +#[must_not_suspend = "You gotta use Umm's, ya know?"] +struct Umm { + i: i64 +} + +struct Bar { + u: Umm, +} + +async fn other() {} + +impl Bar { + async fn uhoh(&mut self) { + let guard = &mut self.u; //~ ERROR `Umm` held across + + other().await; + + *guard = Umm { + i: 2 + } + } +} + +fn main() { +} From 89d35060b900f0aa441ca16b1bde2cdebcb46263 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 23 May 2022 12:50:55 -0700 Subject: [PATCH 02/23] Update must_not_suspend lint to traverse references --- .../rustc_typeck/src/check/generator_interior.rs | 6 ++++++ src/test/ui/lint/must_not_suspend/ref.stderr | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs index d4f8001493c8b..b897d137f74ae 100644 --- a/compiler/rustc_typeck/src/check/generator_interior.rs +++ b/compiler/rustc_typeck/src/check/generator_interior.rs @@ -489,6 +489,8 @@ pub fn check_must_not_suspend_ty<'tcx>( let plural_suffix = pluralize!(data.plural_len); + debug!("Checking must_not_suspend for {}", ty); + match *ty.kind() { ty::Adt(..) if ty.is_box() => { let boxed_ty = ty.boxed_ty(); @@ -580,6 +582,10 @@ pub fn check_must_not_suspend_ty<'tcx>( }, ) } + ty::Ref(_region, ty, _mutability) => { + let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix); + check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data }) + } _ => false, } } diff --git a/src/test/ui/lint/must_not_suspend/ref.stderr b/src/test/ui/lint/must_not_suspend/ref.stderr index 5f000014c7dba..8fbf3e71649ef 100644 --- a/src/test/ui/lint/must_not_suspend/ref.stderr +++ b/src/test/ui/lint/must_not_suspend/ref.stderr @@ -1,5 +1,5 @@ -error: `Umm` held across a suspend point, but should not be - --> $DIR/ref.rs:18:26 +error: reference to `Umm` held across a suspend point, but should not be + --> $DIR/ref.rs:18:13 | LL | let guard = &mut self.u; | ^^^^^^ @@ -13,15 +13,15 @@ note: the lint level is defined here LL | #![deny(must_not_suspend)] | ^^^^^^^^^^^^^^^^ note: You gotta use Umm's, ya know? - --> $DIR/ref.rs:18:26 + --> $DIR/ref.rs:18:13 | LL | let guard = &mut self.u; - | ^^^^^^ + | ^^^^^ help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point - --> $DIR/ref.rs:18:26 + --> $DIR/ref.rs:18:13 | LL | let guard = &mut self.u; - | ^^^^^^ + | ^^^^^ error: aborting due to previous error From c42c77bc7b64cf9015c5b8e7f11f192e499509b3 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Fri, 10 Jun 2022 11:44:53 -0700 Subject: [PATCH 03/23] Update to still be correct without drop tracking --- .../src/check/generator_interior.rs | 8 +++--- .../must_not_suspend/ref-drop-tracking.stderr | 27 +++++++++++++++++++ src/test/ui/lint/must_not_suspend/ref.stderr | 12 ++++----- 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs index b897d137f74ae..bfe0200101c93 100644 --- a/compiler/rustc_typeck/src/check/generator_interior.rs +++ b/compiler/rustc_typeck/src/check/generator_interior.rs @@ -457,7 +457,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> { } #[derive(Default)] -pub struct SuspendCheckData<'a, 'tcx> { +struct SuspendCheckData<'a, 'tcx> { expr: Option<&'tcx Expr<'tcx>>, source_span: Span, yield_span: Span, @@ -472,7 +472,7 @@ pub struct SuspendCheckData<'a, 'tcx> { // // Note that this technique was chosen over things like a `Suspend` marker trait // as it is simpler and has precedent in the compiler -pub fn check_must_not_suspend_ty<'tcx>( +fn check_must_not_suspend_ty<'tcx>( fcx: &FnCtxt<'_, 'tcx>, ty: Ty<'tcx>, hir_id: HirId, @@ -582,7 +582,9 @@ pub fn check_must_not_suspend_ty<'tcx>( }, ) } - ty::Ref(_region, ty, _mutability) => { + // If drop tracking is enabled, we want to look through references, since the referrent + // may not be considered live across the await point. + ty::Ref(_region, ty, _mutability) if fcx.sess().opts.debugging_opts.drop_tracking => { let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix); check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data }) } diff --git a/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr new file mode 100644 index 0000000000000..33c7ff2cb3375 --- /dev/null +++ b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr @@ -0,0 +1,27 @@ +error: reference to `Umm` held across a suspend point, but should not be + --> $DIR/ref-drop-tracking.rs:19:13 + | +LL | let guard = &mut self.u; + | ^^^^^ +LL | +LL | other().await; + | ------ the value is held across this suspend point + | +note: the lint level is defined here + --> $DIR/ref-drop-tracking.rs:4:9 + | +LL | #![deny(must_not_suspend)] + | ^^^^^^^^^^^^^^^^ +note: You gotta use Umm's, ya know? + --> $DIR/ref-drop-tracking.rs:19:13 + | +LL | let guard = &mut self.u; + | ^^^^^ +help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point + --> $DIR/ref-drop-tracking.rs:19:13 + | +LL | let guard = &mut self.u; + | ^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/lint/must_not_suspend/ref.stderr b/src/test/ui/lint/must_not_suspend/ref.stderr index 8fbf3e71649ef..5f000014c7dba 100644 --- a/src/test/ui/lint/must_not_suspend/ref.stderr +++ b/src/test/ui/lint/must_not_suspend/ref.stderr @@ -1,5 +1,5 @@ -error: reference to `Umm` held across a suspend point, but should not be - --> $DIR/ref.rs:18:13 +error: `Umm` held across a suspend point, but should not be + --> $DIR/ref.rs:18:26 | LL | let guard = &mut self.u; | ^^^^^^ @@ -13,15 +13,15 @@ note: the lint level is defined here LL | #![deny(must_not_suspend)] | ^^^^^^^^^^^^^^^^ note: You gotta use Umm's, ya know? - --> $DIR/ref.rs:18:13 + --> $DIR/ref.rs:18:26 | LL | let guard = &mut self.u; - | ^^^^^ + | ^^^^^^ help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point - --> $DIR/ref.rs:18:13 + --> $DIR/ref.rs:18:26 | LL | let guard = &mut self.u; - | ^^^^^ + | ^^^^^^ error: aborting due to previous error From 0da81997a2652b8a46506cf0a5e4d382ea1f3308 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Fri, 29 Jul 2022 15:13:44 -0700 Subject: [PATCH 04/23] Fix after rebasing --- compiler/rustc_typeck/src/check/generator_interior.rs | 2 +- src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs index bfe0200101c93..85a0d4e449906 100644 --- a/compiler/rustc_typeck/src/check/generator_interior.rs +++ b/compiler/rustc_typeck/src/check/generator_interior.rs @@ -584,7 +584,7 @@ fn check_must_not_suspend_ty<'tcx>( } // If drop tracking is enabled, we want to look through references, since the referrent // may not be considered live across the await point. - ty::Ref(_region, ty, _mutability) if fcx.sess().opts.debugging_opts.drop_tracking => { + ty::Ref(_region, ty, _mutability) if fcx.sess().opts.unstable_opts.drop_tracking => { let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix); check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data }) } diff --git a/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr index 33c7ff2cb3375..c49d271285371 100644 --- a/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr +++ b/src/test/ui/lint/must_not_suspend/ref-drop-tracking.stderr @@ -3,7 +3,7 @@ error: reference to `Umm` held across a suspend point, but should not be | LL | let guard = &mut self.u; | ^^^^^ -LL | +LL | LL | other().await; | ------ the value is held across this suspend point | From 848f301782f1dc2e40241e503711fd6e11aefebe Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 30 Jul 2022 17:17:21 -0400 Subject: [PATCH 05/23] avoid assertion failures in try_to_scalar_int --- compiler/rustc_middle/src/mir/interpret/value.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 834c114ee1c58..a1c111ae372a8 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -79,7 +79,7 @@ impl<'tcx> ConstValue<'tcx> { } pub fn try_to_scalar_int(&self) -> Option { - Some(self.try_to_scalar()?.assert_int()) + self.try_to_scalar()?.try_to_int().ok() } pub fn try_to_bits(&self, size: Size) -> Option { @@ -368,6 +368,7 @@ impl<'tcx, Prov: Provenance> Scalar { } #[inline(always)] + #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980) pub fn assert_int(self) -> ScalarInt { self.try_to_int().unwrap() } @@ -389,6 +390,7 @@ impl<'tcx, Prov: Provenance> Scalar { } #[inline(always)] + #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980) pub fn assert_bits(self, target_size: Size) -> u128 { self.to_bits(target_size).unwrap() } From a73afe3b7ec292366d116c4449377020c5066e6a Mon Sep 17 00:00:00 2001 From: Andrew Pollack Date: Tue, 16 Aug 2022 15:56:38 +0000 Subject: [PATCH 06/23] Improving Fuchsia rustc support documentation Improving wording --- src/doc/rustc/src/platform-support/fuchsia.md | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/doc/rustc/src/platform-support/fuchsia.md b/src/doc/rustc/src/platform-support/fuchsia.md index 61bd1b425bc35..f51ccfdafeeb4 100644 --- a/src/doc/rustc/src/platform-support/fuchsia.md +++ b/src/doc/rustc/src/platform-support/fuchsia.md @@ -127,7 +127,10 @@ following files inside: **`package/meta/package`** ```json -{"name":"hello_fuchsia","version":0} +{ + "name": "hello_fuchsia", + "version": "0" +} ``` The `package` file describes our package's name and version number. Every @@ -238,10 +241,17 @@ ${SDK_PATH}/tools/${ARCH}/ffx product-bundle get workstation_eng.qemu-${ARCH} ${SDK_PATH}/tools/${ARCH}/ffx emu start workstation_eng.qemu-${ARCH} --headless ``` -Then, once the emulator has been started: +Once the emulator is running, start a package repository server to serve our +package to the emulator: ```sh -${SDK_PATH}/tools/${ARCH}/ffx target repository register +${SDK_PATH}/tools/${ARCH}/ffx repository server start +``` + +Once the repository server is up and running, register our repository: + +```sh +${SDK_PATH}/tools/${ARCH}/ffx target repository register --repository hello-fuchsia ``` And watch the logs from the emulator in a separate terminal: @@ -259,6 +269,10 @@ ${SDK_PATH}/tools/${ARCH}/ffx component run fuchsia-pkg://hello-fuchsia/hello_fu On reruns of the component, the `--recreate` argument may also need to be passed. +```sh +${SDK_PATH}/tools/${ARCH}/ffx component run --recreate fuchsia-pkg://hello-fuchsia/hello_fuchsia#meta/hello_fuchsia.cm +``` + ## Testing ### Running unit tests From 1886aef0355cbb4666368a0b74609006b2221045 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 17 Aug 2022 04:53:06 +0900 Subject: [PATCH 07/23] point at a type parameter shadowing another type --- .../rustc_resolve/src/late/diagnostics.rs | 12 +++++++++++ .../generic_const_exprs/issue-69654.stderr | 4 +++- src/test/ui/lexical-scopes.stderr | 2 ++ ...t-type-parameter-shadowing-another-type.rs | 21 +++++++++++++++++++ ...pe-parameter-shadowing-another-type.stderr | 12 +++++++++++ src/test/ui/span/issue-35987.stderr | 4 +++- 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs create mode 100644 src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index cb133841bca5a..e1f2f8ae09797 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -161,6 +161,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { msg: String, fallback_label: String, span: Span, + span_label: Option<(Span, &'a str)>, could_be_expr: bool, suggestion: Option<(Span, &'a str, String)>, } @@ -172,6 +173,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { msg: format!("expected {}, found {} `{}`", expected, res.descr(), path_str), fallback_label: format!("not a {expected}"), span, + span_label: match res { + Res::Def(kind, def_id) if kind == DefKind::TyParam => { + self.def_span(def_id).map(|span| (span, "found this type pararmeter")) + } + _ => None, + }, could_be_expr: match res { Res::Def(DefKind::Fn, _) => { // Verify whether this is a fn call or an Fn used as a type. @@ -251,6 +258,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { format!("not found in {mod_str}") }, span: item_span, + span_label: None, could_be_expr: false, suggestion, } @@ -262,6 +270,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span); + if let Some((span, label)) = base_error.span_label { + err.span_label(span, label); + } + if let Some(sugg) = base_error.suggestion { err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect); } diff --git a/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr b/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr index 7a083733a2cd1..5ad457d547a69 100644 --- a/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr +++ b/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr @@ -2,7 +2,9 @@ error[E0423]: expected value, found type parameter `T` --> $DIR/issue-69654.rs:5:25 | LL | impl Bar for [u8; T] {} - | ^ not a value + | - ^ not a value + | | + | found this type pararmeter error[E0599]: the function or associated item `foo` exists for struct `Foo<_>`, but its trait bounds were not satisfied --> $DIR/issue-69654.rs:17:10 diff --git a/src/test/ui/lexical-scopes.stderr b/src/test/ui/lexical-scopes.stderr index 3b2a062c1c254..ad11f72a31d37 100644 --- a/src/test/ui/lexical-scopes.stderr +++ b/src/test/ui/lexical-scopes.stderr @@ -1,6 +1,8 @@ error[E0574]: expected struct, variant or union type, found type parameter `T` --> $DIR/lexical-scopes.rs:3:13 | +LL | fn f() { + | - found this type pararmeter LL | let t = T { i: 0 }; | ^ not a struct, variant or union type diff --git a/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs new file mode 100644 index 0000000000000..bd496875e80b9 --- /dev/null +++ b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs @@ -0,0 +1,21 @@ +trait Foo { + fn foo(&self, name: T) -> usize; +} + +struct Bar { + baz: Baz, +} + +struct Baz { + num: usize, +} + +impl Foo for Bar { + fn foo(&self, _name: Baz) -> usize { + match self.baz { + Baz { num } => num, //~ ERROR expected struct, variant or union type, found type parameter `Baz` + } + } +} + +fn main() {} diff --git a/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr new file mode 100644 index 0000000000000..d9c404e94acb4 --- /dev/null +++ b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr @@ -0,0 +1,12 @@ +error[E0574]: expected struct, variant or union type, found type parameter `Baz` + --> $DIR/point-at-type-parameter-shadowing-another-type.rs:16:13 + | +LL | impl Foo for Bar { + | --- found this type pararmeter +... +LL | Baz { num } => num, + | ^^^ not a struct, variant or union type + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0574`. diff --git a/src/test/ui/span/issue-35987.stderr b/src/test/ui/span/issue-35987.stderr index 2bc3ff4c3b619..ea9c4c82c3610 100644 --- a/src/test/ui/span/issue-35987.stderr +++ b/src/test/ui/span/issue-35987.stderr @@ -2,7 +2,9 @@ error[E0404]: expected trait, found type parameter `Add` --> $DIR/issue-35987.rs:5:21 | LL | impl Add for Foo { - | ^^^ not a trait + | --- ^^^ not a trait + | | + | found this type pararmeter | help: consider importing this trait instead | From 5a848c701b98bf99b7dd480dc95bf1227b134e55 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 17 Aug 2022 04:58:26 +0900 Subject: [PATCH 08/23] avoid a `&str` to `String` conversion --- src/tools/rust-analyzer/bench_data/glorious_old_parser | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/bench_data/glorious_old_parser b/src/tools/rust-analyzer/bench_data/glorious_old_parser index 7e900dfeb1eeb..764893daa12ad 100644 --- a/src/tools/rust-analyzer/bench_data/glorious_old_parser +++ b/src/tools/rust-analyzer/bench_data/glorious_old_parser @@ -1988,7 +1988,7 @@ impl<'a> Parser<'a> { err.span_suggestion( span, "declare the type after the parameter binding", - String::from(": "), + ": ", Applicability::HasPlaceholders, ); } else if require_name && is_trait_item { From 7e15fbab75ab919238ec86011fd54cba7ad78e68 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 18:34:13 -0300 Subject: [PATCH 09/23] Added first migration for repeated expressions without syntax vars --- .../rustc_error_messages/locales/en-US/expand.ftl | 3 +++ compiler/rustc_expand/src/mbe/transcribe.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index bdfa22e77eb2f..42519fd22ce63 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -3,3 +3,6 @@ expand_explain_doc_comment_outer = expand_explain_doc_comment_inner = inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match + +expand_expr_repeat_no_syntax_vars = + attempted to repeat an expression containing no syntax variables matched as repeating at this depth diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index e47ea83ac3809..69090cb457e75 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -9,6 +9,7 @@ use rustc_errors::{pluralize, PResult}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; +use rustc_macros::SessionDiagnostic; use rustc_span::Span; use smallvec::{smallvec, SmallVec}; @@ -53,6 +54,13 @@ impl<'a> Iterator for Frame<'a> { } } +#[derive(SessionDiagnostic)] +#[error(expand::expr_repeat_no_syntax_vars)] +struct NoSyntaxVarsExprRepeat { + #[primary_span] + span: Span, +} + /// This can do Macro-By-Example transcription. /// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the /// invocation. We are assuming we already know there is a match. @@ -165,11 +173,7 @@ pub(super) fn transcribe<'a>( seq @ mbe::TokenTree::Sequence(_, delimited) => { match lockstep_iter_size(&seq, interp, &repeats) { LockstepIterSize::Unconstrained => { - return Err(cx.struct_span_err( - seq.span(), /* blame macro writer */ - "attempted to repeat an expression containing no syntax variables \ - matched as repeating at this depth", - )); + return Err(cx.create_err(NoSyntaxVarsExprRepeat { span: seq.span() })); } LockstepIterSize::Contradiction(msg) => { From be18a9bf75f6c92ee734838fd3eca9257556cf40 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 19:02:51 -0300 Subject: [PATCH 10/23] Migrated more diagnostics under transcribe.rs --- .../locales/en-US/expand.ftl | 6 +++++ compiler/rustc_expand/src/mbe/transcribe.rs | 26 ++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index 42519fd22ce63..b25aaaa0e5175 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -6,3 +6,9 @@ expand_explain_doc_comment_inner = expand_expr_repeat_no_syntax_vars = attempted to repeat an expression containing no syntax variables matched as repeating at this depth + +expand_must_repeat_once = + this must repeat at least once + +count_repetition_misplaced = + `count` can not be placed inside the inner-most repetition \ No newline at end of file diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 69090cb457e75..4c8f7a59bbac2 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -7,9 +7,9 @@ use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{pluralize, PResult}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; +use rustc_macros::SessionDiagnostic; use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; -use rustc_macros::SessionDiagnostic; use rustc_span::Span; use smallvec::{smallvec, SmallVec}; @@ -61,6 +61,13 @@ struct NoSyntaxVarsExprRepeat { span: Span, } +#[derive(SessionDiagnostic)] +#[error(expand::must_repeat_once)] +struct MustRepeatOnce { + #[primary_span] + span: Span, +} + /// This can do Macro-By-Example transcription. /// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the /// invocation. We are assuming we already know there is a match. @@ -197,10 +204,7 @@ pub(super) fn transcribe<'a>( // FIXME: this really ought to be caught at macro definition // time... It happens when the Kleene operator in the matcher and // the body for the same meta-variable do not match. - return Err(cx.struct_span_err( - sp.entire(), - "this must repeat at least once", - )); + return Err(cx.create_err(MustRepeatOnce { span: sp.entire() })); } } else { // 0 is the initial counter (we have done 0 repetitions so far). `len` @@ -424,6 +428,13 @@ fn lockstep_iter_size( } } +#[derive(SessionDiagnostic)] +#[error(expand::count_repetition_misplaced)] +struct CountRepetitionMisplaced { + #[primary_span] + span: Span, +} + /// Used solely by the `count` meta-variable expression, counts the outer-most repetitions at a /// given optional nested depth. /// @@ -452,10 +463,7 @@ fn count_repetitions<'a>( match matched { MatchedTokenTree(_) | MatchedNonterminal(_) => { if declared_lhs_depth == 0 { - return Err(cx.struct_span_err( - sp.entire(), - "`count` can not be placed inside the inner-most repetition", - )); + return Err(cx.create_err( CountRepetitionMisplaced { span: sp.entire()} )); } match depth_opt { None => Ok(1), From 72ce216def236055f5bee03d06085d0ec9c270a9 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 19:19:59 -0300 Subject: [PATCH 11/23] Previous commit under x.py fmt --- compiler/rustc_expand/src/mbe/transcribe.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 4c8f7a59bbac2..af5489761920f 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -463,7 +463,7 @@ fn count_repetitions<'a>( match matched { MatchedTokenTree(_) | MatchedNonterminal(_) => { if declared_lhs_depth == 0 { - return Err(cx.create_err( CountRepetitionMisplaced { span: sp.entire()} )); + return Err(cx.create_err(CountRepetitionMisplaced { span: sp.entire() })); } match depth_opt { None => Ok(1), From 2c24958cfd15b5b5f683faf241af5a4e47b2e34b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 17 Aug 2022 12:20:25 +1000 Subject: [PATCH 12/23] Remove `TraitDef::attributes`. Because it's always empty. --- compiler/rustc_builtin_macros/src/deriving/bounds.rs | 1 - compiler/rustc_builtin_macros/src/deriving/clone.rs | 1 - compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs | 1 - compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs | 1 - .../rustc_builtin_macros/src/deriving/cmp/partial_eq.rs | 1 - .../rustc_builtin_macros/src/deriving/cmp/partial_ord.rs | 1 - compiler/rustc_builtin_macros/src/deriving/debug.rs | 1 - compiler/rustc_builtin_macros/src/deriving/decodable.rs | 1 - compiler/rustc_builtin_macros/src/deriving/default.rs | 1 - compiler/rustc_builtin_macros/src/deriving/encodable.rs | 1 - compiler/rustc_builtin_macros/src/deriving/generic/mod.rs | 8 ++------ compiler/rustc_builtin_macros/src/deriving/hash.rs | 1 - 12 files changed, 2 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/bounds.rs b/compiler/rustc_builtin_macros/src/deriving/bounds.rs index 5ef68c6aeaa59..77e0b6c55a80e 100644 --- a/compiler/rustc_builtin_macros/src/deriving/bounds.rs +++ b/compiler/rustc_builtin_macros/src/deriving/bounds.rs @@ -15,7 +15,6 @@ pub fn expand_deriving_copy( ) { let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(marker::Copy), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 7755ff779c4d9..ee4c5aea1a352 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -71,7 +71,6 @@ pub fn expand_deriving_clone( let attrs = vec![cx.attribute(inline)]; let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(clone::Clone), additional_bounds: bounds, generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index 4e798bf6acb10..f99ee8cb2d53c 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -23,7 +23,6 @@ pub fn expand_deriving_eq( let attrs = vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)]; let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(cmp::Eq), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs index 1612be862377b..8aa16dfeb0f1f 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs @@ -18,7 +18,6 @@ pub fn expand_deriving_ord( let attrs = vec![cx.attribute(inline)]; let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(cmp::Ord), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs index 0141b33772621..73be9362e4eb4 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -98,7 +98,6 @@ pub fn expand_deriving_partial_eq( let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(cmp::PartialEq), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index 2ebb01cc8a035..137c779f81b84 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -36,7 +36,6 @@ pub fn expand_deriving_partial_ord( let trait_def = TraitDef { span, - attributes: vec![], path: path_std!(cmp::PartialOrd), additional_bounds: vec![], generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index ceef893e862eb..af370a7751da5 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -19,7 +19,6 @@ pub fn expand_deriving_debug( let trait_def = TraitDef { span, - attributes: Vec::new(), path: path_std!(fmt::Debug), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs index d688143a2a5c6..47da0862b52fc 100644 --- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs @@ -22,7 +22,6 @@ pub fn expand_deriving_rustc_decodable( let trait_def = TraitDef { span, - attributes: Vec::new(), path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 5177690917f21..a431832080c0c 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -25,7 +25,6 @@ pub fn expand_deriving_default( let attrs = vec![cx.attribute(inline)]; let trait_def = TraitDef { span, - attributes: Vec::new(), path: Path::new(vec![kw::Default, sym::Default]), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs index 70167cac68a7e..d43c66a5fa644 100644 --- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs @@ -106,7 +106,6 @@ pub fn expand_deriving_rustc_encodable( let trait_def = TraitDef { span, - attributes: Vec::new(), path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global), additional_bounds: Vec::new(), generics: Bounds::empty(), diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 9f3a80ea7cbdc..e09553be830d3 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -184,8 +184,6 @@ pub struct TraitDef<'a> { /// The span for the current #[derive(Foo)] header. pub span: Span, - pub attributes: Vec, - /// Path of the trait, including any type parameters pub path: Path, @@ -718,15 +716,13 @@ impl<'a> TraitDef<'a> { let self_type = cx.ty_path(path); let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived)); + let attrs = vec![attr]; let opt_trait_ref = Some(trait_ref); - let mut a = vec![attr]; - a.extend(self.attributes.iter().cloned()); - cx.item( self.span, Ident::empty(), - a, + attrs, ast::ItemKind::Impl(Box::new(ast::Impl { unsafety: ast::Unsafe::No, polarity: ast::ImplPolarity::Positive, diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs index 32ae3d3447896..9aa170bec14d8 100644 --- a/compiler/rustc_builtin_macros/src/deriving/hash.rs +++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs @@ -21,7 +21,6 @@ pub fn expand_deriving_hash( let arg = Path::new_local(typaram); let hash_trait_def = TraitDef { span, - attributes: Vec::new(), path, additional_bounds: Vec::new(), generics: Bounds::empty(), From 6cd40d0e518b1bd2477932e411aaf6502807edea Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 17 Aug 2022 12:33:42 +1000 Subject: [PATCH 13/23] Remove `attrs` arg from `typaram` and `mk_ty_param`. Because it's always empty. --- compiler/rustc_builtin_macros/src/deriving/generic/mod.rs | 2 +- compiler/rustc_builtin_macros/src/deriving/generic/ty.rs | 5 ++--- compiler/rustc_expand/src/build.rs | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index e09553be830d3..9882f7958aaa5 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -603,7 +603,7 @@ impl<'a> TraitDef<'a> { param.bounds.iter().cloned() ).collect(); - cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, vec![], bounds, None) + cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None) } GenericParamKind::Const { ty, kw_span, .. } => { let const_nodefault_kind = GenericParamKind::Const { diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs index 4d46f7cd48a51..36e2e29308694 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs @@ -146,7 +146,6 @@ fn mk_ty_param( cx: &ExtCtxt<'_>, span: Span, name: Symbol, - attrs: &[ast::Attribute], bounds: &[Path], self_ident: Ident, self_generics: &Generics, @@ -158,7 +157,7 @@ fn mk_ty_param( cx.trait_bound(path) }) .collect(); - cx.typaram(span, Ident::new(name, span), attrs.to_owned(), bounds, None) + cx.typaram(span, Ident::new(name, span), bounds, None) } /// Bounds on type parameters. @@ -183,7 +182,7 @@ impl Bounds { .iter() .map(|t| { let (name, ref bounds) = *t; - mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics) + mk_ty_param(cx, span, name, &bounds, self_ty, self_generics) }) .collect(); diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index fa3e2a4a5b81c..1f5cd8decbd86 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -106,14 +106,13 @@ impl<'a> ExtCtxt<'a> { &self, span: Span, ident: Ident, - attrs: Vec, bounds: ast::GenericBounds, default: Option>, ) -> ast::GenericParam { ast::GenericParam { ident: ident.with_span_pos(span), id: ast::DUMMY_NODE_ID, - attrs: attrs.into(), + attrs: AttrVec::new(), bounds, kind: ast::GenericParamKind::Type { default }, is_placeholder: false, From 52d8397985f8bbdc68e9b08f4cc80d6de5e916f8 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 17 Aug 2022 12:38:21 +1000 Subject: [PATCH 14/23] Remove `AttributesExt::other_attrs`. It's unused. --- src/librustdoc/clean/types.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 1a4786c9b0664..2a38713d43b8a 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -820,8 +820,6 @@ pub(crate) trait AttributesExt { fn inner_docs(&self) -> bool; - fn other_attrs(&self) -> Vec; - fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet) -> Option>; } @@ -848,10 +846,6 @@ impl AttributesExt for [ast::Attribute] { self.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == AttrStyle::Inner) } - fn other_attrs(&self) -> Vec { - self.iter().filter(|attr| attr.doc_str().is_none()).cloned().collect() - } - fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet) -> Option> { let sess = tcx.sess; let doc_cfg_active = tcx.features().doc_cfg; From d3bf103d1b3e8391a8d0a4ebecbfa1004e1297cb Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 17 Aug 2022 11:18:48 +0200 Subject: [PATCH 15/23] Fix documentation of rustc_parse::parser::Parser::parse_stmt_without_recovery Something seems to have gotten out of sync during the creation of #81177, where both the argument and comment were introduced. --- compiler/rustc_parse/src/parser/stmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index ade0f4fbc86a2..cac39f8f25fb8 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -34,7 +34,7 @@ impl<'a> Parser<'a> { })) } - /// If `force_capture` is true, forces collection of tokens regardless of whether + /// If `force_collect` is [`ForceCollect::Yes`], forces collection of tokens regardless of whether /// or not we have attributes pub(crate) fn parse_stmt_without_recovery( &mut self, From 98fb65eff9885f1dea6372f007962d9cbeb4a40c Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 17 Aug 2022 17:47:44 +0800 Subject: [PATCH 16/23] Migrate lint reports in typeck::check_unused to LintDiagnostic --- .../locales/en-US/typeck.ftl | 8 +++ compiler/rustc_typeck/src/check_unused.rs | 63 ++++++++----------- compiler/rustc_typeck/src/errors.rs | 20 +++++- 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/typeck.ftl b/compiler/rustc_error_messages/locales/en-US/typeck.ftl index 494b8f913934f..0014da17c88e5 100644 --- a/compiler/rustc_error_messages/locales/en-US/typeck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/typeck.ftl @@ -123,3 +123,11 @@ typeck_manual_implementation = .help = add `#![feature(unboxed_closures)]` to the crate attributes to enable typeck_substs_on_overridden_impl = could not resolve substs on overridden impl + +typeck_unused_extern_crate = + unused extern crate + .suggestion = remove it + +typeck_extern_crate_not_idiomatic = + `extern crate` is not idiomatic in the new edition + .suggestion = convert it to a `{$msg_code}` diff --git a/compiler/rustc_typeck/src/check_unused.rs b/compiler/rustc_typeck/src/check_unused.rs index 4a3cfa1ca376a..1d23ed9292180 100644 --- a/compiler/rustc_typeck/src/check_unused.rs +++ b/compiler/rustc_typeck/src/check_unused.rs @@ -1,5 +1,5 @@ +use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; -use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -108,25 +108,16 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // We do this in any edition. if extern_crate.warn_if_unused { if let Some(&span) = unused_extern_crates.get(&def_id) { + // Removal suggestion span needs to include attributes (Issue #54400) let id = tcx.hir().local_def_id_to_hir_id(def_id); - tcx.struct_span_lint_hir(lint, id, span, |lint| { - // Removal suggestion span needs to include attributes (Issue #54400) - let span_with_attrs = tcx - .hir() - .attrs(id) - .iter() - .map(|attr| attr.span) - .fold(span, |acc, attr_span| acc.to(attr_span)); - - lint.build("unused extern crate") - .span_suggestion_short( - span_with_attrs, - "remove it", - "", - Applicability::MachineApplicable, - ) - .emit(); - }); + let span_with_attrs = tcx + .hir() + .attrs(id) + .iter() + .map(|attr| attr.span) + .fold(span, |acc, attr_span| acc.to(attr_span)); + + tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs }); continue; } } @@ -158,23 +149,23 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { if !tcx.hir().attrs(id).is_empty() { continue; } - tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| { - // Otherwise, we can convert it into a `use` of some kind. - let base_replacement = match extern_crate.orig_name { - Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), - None => format!("use {};", item.ident.name), - }; - let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default(); - let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) }; - lint.build("`extern crate` is not idiomatic in the new edition") - .span_suggestion_short( - extern_crate.span, - &format!("convert it to a `{}`", add_vis("use".to_string())), - add_vis(base_replacement), - Applicability::MachineApplicable, - ) - .emit(); - }) + + let base_replacement = match extern_crate.orig_name { + Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), + None => format!("use {};", item.ident.name), + }; + let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default(); + let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) }; + tcx.emit_spanned_lint( + lint, + id, + extern_crate.span, + ExternCrateNotIdiomatic { + span: extern_crate.span, + msg_code: add_vis("use".to_string()), + suggestion_code: add_vis(base_replacement), + }, + ); } } diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index 0438ac02ea91a..7dfd2e9cb48be 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -1,6 +1,6 @@ //! Errors emitted by typeck. use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed}; -use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; +use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_middle::ty::Ty; use rustc_session::{parse::ParseSess, SessionDiagnostic}; use rustc_span::{symbol::Ident, Span, Symbol}; @@ -324,3 +324,21 @@ pub struct SubstsOnOverriddenImpl { #[primary_span] pub span: Span, } + +#[derive(LintDiagnostic)] +#[lint(typeck::unused_extern_crate)] +pub struct UnusedExternCrate { + #[primary_span] + #[suggestion(applicability = "machine-applicable", code = "")] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[lint(typeck::extern_crate_not_idiomatic)] +pub struct ExternCrateNotIdiomatic { + #[primary_span] + #[suggestion(applicability = "machine-applicable", code = "{suggestion_code}")] + pub span: Span, + pub msg_code: String, + pub suggestion_code: String, +} From 52418d661bf7c66b8605c041a249203458036993 Mon Sep 17 00:00:00 2001 From: Twice Date: Wed, 17 Aug 2022 20:45:18 +0800 Subject: [PATCH 17/23] use `suggestion_short` in `LintDiagnostic` Co-authored-by: David Wood --- compiler/rustc_typeck/src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index 7dfd2e9cb48be..a33d88dfee0c7 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -337,7 +337,7 @@ pub struct UnusedExternCrate { #[lint(typeck::extern_crate_not_idiomatic)] pub struct ExternCrateNotIdiomatic { #[primary_span] - #[suggestion(applicability = "machine-applicable", code = "{suggestion_code}")] + #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")] pub span: Span, pub msg_code: String, pub suggestion_code: String, From b704843c4438387a66111b3464bb3987c45ce294 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 17 Aug 2022 21:05:21 +0800 Subject: [PATCH 18/23] fix test file --- .../issue-54400-unused-extern-crate-attr-span.stderr | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr index 2ef97e7f20e9f..01bd924a415bb 100644 --- a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr +++ b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr @@ -1,11 +1,9 @@ error: unused extern crate - --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1 + --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:11:1 | LL | / #[cfg(blandiloquence)] LL | | extern crate edition_lint_paths; - | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | |________________________________| - | help: remove it + | |________________________________^ help: remove it | note: the lint level is defined here --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9 From 9efe97951165a144c17ab08c8842f4f1cf23b4da Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 17 Aug 2022 22:08:06 +0800 Subject: [PATCH 19/23] remove #[primary_span] --- compiler/rustc_typeck/src/errors.rs | 2 -- .../issue-54400-unused-extern-crate-attr-span.stderr | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index a33d88dfee0c7..76599721e586f 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -328,7 +328,6 @@ pub struct SubstsOnOverriddenImpl { #[derive(LintDiagnostic)] #[lint(typeck::unused_extern_crate)] pub struct UnusedExternCrate { - #[primary_span] #[suggestion(applicability = "machine-applicable", code = "")] pub span: Span, } @@ -336,7 +335,6 @@ pub struct UnusedExternCrate { #[derive(LintDiagnostic)] #[lint(typeck::extern_crate_not_idiomatic)] pub struct ExternCrateNotIdiomatic { - #[primary_span] #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")] pub span: Span, pub msg_code: String, diff --git a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr index 01bd924a415bb..2ef97e7f20e9f 100644 --- a/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr +++ b/src/test/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.stderr @@ -1,9 +1,11 @@ error: unused extern crate - --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:11:1 + --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1 | LL | / #[cfg(blandiloquence)] LL | | extern crate edition_lint_paths; - | |________________________________^ help: remove it + | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- + | |________________________________| + | help: remove it | note: the lint level is defined here --> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9 From c6f9a9c410e063f59bc2f6958674099b3fe5f184 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 11:18:19 -0300 Subject: [PATCH 20/23] Moved structs to rustc_expand::errors, added several more migrations, fixed slug name --- .../locales/en-US/expand.ftl | 12 ++++- compiler/rustc_errors/src/diagnostic.rs | 3 +- compiler/rustc_expand/src/errors.rs | 50 +++++++++++++++++++ compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_expand/src/mbe/transcribe.rs | 40 +++------------ 5 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 compiler/rustc_expand/src/errors.rs diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index b25aaaa0e5175..ee76a4f45005d 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -10,5 +10,13 @@ expand_expr_repeat_no_syntax_vars = expand_must_repeat_once = this must repeat at least once -count_repetition_misplaced = - `count` can not be placed inside the inner-most repetition \ No newline at end of file +expand_count_repetition_misplaced = + `count` can not be placed inside the inner-most repetition + +expand_meta_var_expr_unrecognized_var = + variable `{$key}` is not recognized in meta-variable expression + +expand_var_still_repeating = + variable '{$ident}' is still repeating at this depth + +expand_meta_var_dif_seq_matchers = {$msg} \ No newline at end of file diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 17e6c9e9575fd..356f9dfdb3b2e 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -8,7 +8,7 @@ use rustc_error_messages::FluentValue; use rustc_hir as hir; use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_span::edition::LATEST_STABLE_EDITION; -use rustc_span::symbol::{Ident, Symbol}; +use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol}; use rustc_span::{edition::Edition, Span, DUMMY_SP}; use std::borrow::Cow; use std::fmt; @@ -87,6 +87,7 @@ into_diagnostic_arg_using_display!( hir::Target, Edition, Ident, + MacroRulesNormalizedIdent, ); impl IntoDiagnosticArg for bool { diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs new file mode 100644 index 0000000000000..f8b8750a78965 --- /dev/null +++ b/compiler/rustc_expand/src/errors.rs @@ -0,0 +1,50 @@ +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; +use rustc_span::symbol::{MacroRulesNormalizedIdent}; + +#[derive(SessionDiagnostic)] +#[error(expand::expr_repeat_no_syntax_vars)] +pub(crate) struct NoSyntaxVarsExprRepeat { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[error(expand::must_repeat_once)] +pub(crate) struct MustRepeatOnce { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[error(expand::count_repetition_misplaced)] +pub(crate) struct CountRepetitionMisplaced { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[error(expand::meta_var_expr_unrecognized_var)] +pub(crate) struct MetaVarExprUnrecognizedVar { + #[primary_span] + pub span: Span, + pub key: MacroRulesNormalizedIdent, +} + +#[derive(SessionDiagnostic)] +#[error(expand::var_still_repeating)] +pub(crate) struct VarStillRepeating { + #[primary_span] + pub span: Span, + pub ident: MacroRulesNormalizedIdent, +} + +#[derive(SessionDiagnostic)] +#[error(expand::var_still_repeating)] +pub(crate) struct MetaVarsDifSeqMatchers { + #[primary_span] + pub span: Span, + pub msg: String, +} + + diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index 91a183427843e..e1dde1672c190 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -25,6 +25,7 @@ pub mod base; pub mod build; #[macro_use] pub mod config; +pub mod errors; pub mod expand; pub mod module; pub mod proc_macro; diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index af5489761920f..bec6d1a2df7d8 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -1,4 +1,8 @@ use crate::base::ExtCtxt; +use crate::errors::{ + CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce, + NoSyntaxVarsExprRepeat, VarStillRepeating, +}; use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch}; use crate::mbe::{self, MetaVarExpr}; use rustc_ast::mut_visit::{self, MutVisitor}; @@ -7,7 +11,6 @@ use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{pluralize, PResult}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; -use rustc_macros::SessionDiagnostic; use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; use rustc_span::Span; @@ -54,20 +57,6 @@ impl<'a> Iterator for Frame<'a> { } } -#[derive(SessionDiagnostic)] -#[error(expand::expr_repeat_no_syntax_vars)] -struct NoSyntaxVarsExprRepeat { - #[primary_span] - span: Span, -} - -#[derive(SessionDiagnostic)] -#[error(expand::must_repeat_once)] -struct MustRepeatOnce { - #[primary_span] - span: Span, -} - /// This can do Macro-By-Example transcription. /// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the /// invocation. We are assuming we already know there is a match. @@ -188,7 +177,7 @@ pub(super) fn transcribe<'a>( // happens when two meta-variables are used in the same repetition in a // sequence, but they come from different sequence matchers and repeat // different amounts. - return Err(cx.struct_span_err(seq.span(), &msg)); + return Err(cx.create_err(MetaVarsDifSeqMatchers { span: seq.span(), msg })); } LockstepIterSize::Constraint(len, _) => { @@ -247,10 +236,7 @@ pub(super) fn transcribe<'a>( } MatchedSeq(..) => { // We were unable to descend far enough. This is an error. - return Err(cx.struct_span_err( - sp, /* blame the macro writer */ - &format!("variable '{}' is still repeating at this depth", ident), - )); + return Err(cx.create_err(VarStillRepeating { span: sp, ident })); } } } else { @@ -428,13 +414,6 @@ fn lockstep_iter_size( } } -#[derive(SessionDiagnostic)] -#[error(expand::count_repetition_misplaced)] -struct CountRepetitionMisplaced { - #[primary_span] - span: Span, -} - /// Used solely by the `count` meta-variable expression, counts the outer-most repetitions at a /// given optional nested depth. /// @@ -511,12 +490,7 @@ where { let span = ident.span; let key = MacroRulesNormalizedIdent::new(ident); - interp.get(&key).ok_or_else(|| { - cx.struct_span_err( - span, - &format!("variable `{}` is not recognized in meta-variable expression", key), - ) - }) + interp.get(&key).ok_or_else(|| cx.create_err(MetaVarExprUnrecognizedVar { span, key })) } /// Used by meta-variable expressions when an user input is out of the actual declared bounds. For From caab20c431a05dc3f01d3810ac13b6315f2d9b70 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 11:20:37 -0300 Subject: [PATCH 21/23] Moved structs to rustc_expand::errors, added several more migrations, fixed slug name --- compiler/rustc_expand/src/errors.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index f8b8750a78965..3f62120b1c81f 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -1,6 +1,6 @@ use rustc_macros::SessionDiagnostic; +use rustc_span::symbol::MacroRulesNormalizedIdent; use rustc_span::Span; -use rustc_span::symbol::{MacroRulesNormalizedIdent}; #[derive(SessionDiagnostic)] #[error(expand::expr_repeat_no_syntax_vars)] @@ -46,5 +46,3 @@ pub(crate) struct MetaVarsDifSeqMatchers { pub span: Span, pub msg: String, } - - From a468f131628341ff83bdabd1133f432c17fa1cbe Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 13:13:07 -0300 Subject: [PATCH 22/23] Hotfix for duplicated slug name on VarStillRepeating struct --- compiler/rustc_expand/src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index 3f62120b1c81f..0d7e137c7dd0c 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -40,7 +40,7 @@ pub(crate) struct VarStillRepeating { } #[derive(SessionDiagnostic)] -#[error(expand::var_still_repeating)] +#[error(expand::meta_var_dif_seq_matchers)] pub(crate) struct MetaVarsDifSeqMatchers { #[primary_span] pub span: Span, From fc7fc0fae5079bc433a6e377b163a2a0757d65cd Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 17 Aug 2022 17:23:52 +0000 Subject: [PATCH 23/23] ty::Error does not match other types for region constraints --- .../src/infer/outlives/test_type_match.rs | 9 ++++++++- src/test/ui/regions/outlives-with-missing.rs | 16 ++++++++++++++++ src/test/ui/regions/outlives-with-missing.stderr | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/regions/outlives-with-missing.rs create mode 100644 src/test/ui/regions/outlives-with-missing.stderr diff --git a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs index 772e297b7b445..be03c8b5bae98 100644 --- a/compiler/rustc_infer/src/infer/outlives/test_type_match.rs +++ b/compiler/rustc_infer/src/infer/outlives/test_type_match.rs @@ -174,7 +174,14 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> { #[instrument(skip(self), level = "debug")] fn tys(&mut self, pattern: Ty<'tcx>, value: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { - if pattern == value { Ok(pattern) } else { relate::super_relate_tys(self, pattern, value) } + if let ty::Error(_) = pattern.kind() { + // Unlike normal `TypeRelation` rules, `ty::Error` does not equal any type. + self.no_match() + } else if pattern == value { + Ok(pattern) + } else { + relate::super_relate_tys(self, pattern, value) + } } #[instrument(skip(self), level = "debug")] diff --git a/src/test/ui/regions/outlives-with-missing.rs b/src/test/ui/regions/outlives-with-missing.rs new file mode 100644 index 0000000000000..29d89718b5867 --- /dev/null +++ b/src/test/ui/regions/outlives-with-missing.rs @@ -0,0 +1,16 @@ +trait HandlerFamily { + type Target; +} + +struct HandlerWrapper(H); + +impl HandlerWrapper { + pub fn set_handler(&self, handler: &H::Target) + where + T: Send + Sync + 'static, + //~^ ERROR cannot find type `T` in this scope + { + } +} + +fn main() {} diff --git a/src/test/ui/regions/outlives-with-missing.stderr b/src/test/ui/regions/outlives-with-missing.stderr new file mode 100644 index 0000000000000..e204c918724fe --- /dev/null +++ b/src/test/ui/regions/outlives-with-missing.stderr @@ -0,0 +1,12 @@ +error[E0412]: cannot find type `T` in this scope + --> $DIR/outlives-with-missing.rs:10:9 + | +LL | impl HandlerWrapper { + | - similarly named type parameter `H` defined here +... +LL | T: Send + Sync + 'static, + | ^ help: a type parameter with a similar name exists: `H` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0412`.