From 701ccd3eadcf507a4e035668b45856c6f875fa83 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Sep 2024 09:11:30 +1000 Subject: [PATCH 1/5] Remove `HirCollector::sess`. It's redundant w.r.t. `HirCollector::tcx`. This removes the unnecessary `'a` lifetime. --- src/librustdoc/doctest.rs | 1 - src/librustdoc/doctest/rust.rs | 17 +++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 3ee6b24ac92cb..9ef203b4f1258 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -186,7 +186,6 @@ pub(crate) fn run( let mut collector = CreateRunnableDocTests::new(options, opts); let hir_collector = HirCollector::new( - &compiler.sess, tcx.hir(), ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()), enable_per_target_ignores, diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index cc85a73430b37..7739790c310a7 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -10,7 +10,6 @@ use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_resolve::rustdoc::span_of_fragments; -use rustc_session::Session; use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, DUMMY_SP, FileName, Pos, Span}; @@ -63,8 +62,7 @@ impl DocTestVisitor for RustCollector { fn visit_header(&mut self, _name: &str, _level: u32) {} } -pub(super) struct HirCollector<'a, 'tcx> { - sess: &'a Session, +pub(super) struct HirCollector<'tcx> { map: Map<'tcx>, codes: ErrorCodes, tcx: TyCtxt<'tcx>, @@ -72,21 +70,20 @@ pub(super) struct HirCollector<'a, 'tcx> { collector: RustCollector, } -impl<'a, 'tcx> HirCollector<'a, 'tcx> { +impl<'tcx> HirCollector<'tcx> { pub fn new( - sess: &'a Session, map: Map<'tcx>, codes: ErrorCodes, enable_per_target_ignores: bool, tcx: TyCtxt<'tcx>, ) -> Self { let collector = RustCollector { - source_map: sess.psess.clone_source_map(), + source_map: tcx.sess.psess.clone_source_map(), cur_path: vec![], position: DUMMY_SP, tests: vec![], }; - Self { sess, map, codes, enable_per_target_ignores, tcx, collector } + Self { map, codes, enable_per_target_ignores, tcx, collector } } pub fn collect_crate(mut self) -> Vec { @@ -98,7 +95,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> { } } -impl<'a, 'tcx> HirCollector<'a, 'tcx> { +impl<'tcx> HirCollector<'tcx> { fn visit_testable( &mut self, name: String, @@ -108,7 +105,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> { ) { let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id)); if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) { - if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) { + if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) { return; } } @@ -141,7 +138,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> { } } -impl<'a, 'tcx> intravisit::Visitor<'tcx> for HirCollector<'a, 'tcx> { +impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'tcx> { type NestedFilter = nested_filter::All; fn nested_visit_map(&mut self) -> Self::Map { From 711c91da7fa5882020c8d9f7a6edfa6d0b9c062f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Sep 2024 09:24:22 +1000 Subject: [PATCH 2/5] Remove `HirCollector::map`. Because `rustc_middle::hir::map::Map` is a trivial wrapper around `TyCtxt`, and `HirCollector` has a `TyCtxt` field. --- src/librustdoc/doctest.rs | 1 - src/librustdoc/doctest/rust.rs | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 9ef203b4f1258..48272a41c938c 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -186,7 +186,6 @@ pub(crate) fn run( let mut collector = CreateRunnableDocTests::new(options, opts); let hir_collector = HirCollector::new( - tcx.hir(), ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()), enable_per_target_ignores, tcx, diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index 7739790c310a7..a9ab02e29cd95 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -6,7 +6,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lrc; use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId}; use rustc_hir::{self as hir, CRATE_HIR_ID, intravisit}; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_resolve::rustdoc::span_of_fragments; @@ -63,7 +62,6 @@ impl DocTestVisitor for RustCollector { } pub(super) struct HirCollector<'tcx> { - map: Map<'tcx>, codes: ErrorCodes, tcx: TyCtxt<'tcx>, enable_per_target_ignores: bool, @@ -71,19 +69,14 @@ pub(super) struct HirCollector<'tcx> { } impl<'tcx> HirCollector<'tcx> { - pub fn new( - map: Map<'tcx>, - codes: ErrorCodes, - enable_per_target_ignores: bool, - tcx: TyCtxt<'tcx>, - ) -> Self { + pub fn new(codes: ErrorCodes, enable_per_target_ignores: bool, tcx: TyCtxt<'tcx>) -> Self { let collector = RustCollector { source_map: tcx.sess.psess.clone_source_map(), cur_path: vec![], position: DUMMY_SP, tests: vec![], }; - Self { map, codes, enable_per_target_ignores, tcx, collector } + Self { codes, enable_per_target_ignores, tcx, collector } } pub fn collect_crate(mut self) -> Vec { @@ -142,13 +135,13 @@ impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'tcx> { type NestedFilter = nested_filter::All; fn nested_visit_map(&mut self) -> Self::Map { - self.map + self.tcx.hir() } fn visit_item(&mut self, item: &'tcx hir::Item<'_>) { let name = match &item.kind { hir::ItemKind::Impl(impl_) => { - rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id) + rustc_hir_pretty::id_to_string(&self.tcx.hir(), impl_.self_ty.hir_id) } _ => item.ident.to_string(), }; From 0882ad57a5012be0bac72d2d37ea95bc32d901c6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Sep 2024 09:30:45 +1000 Subject: [PATCH 3/5] Rename `AstValidator::session` as `AstValidator::sess`. Because `sess` is the standard name used everywhere else. --- .../rustc_ast_passes/src/ast_validation.rs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 229d04f8de2fd..5bdd9b6eda80f 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -63,7 +63,7 @@ impl TraitOrTraitImpl { } struct AstValidator<'a> { - session: &'a Session, + sess: &'a Session, features: &'a Features, /// The span of the `extern` in an `extern { ... }` block, if any. @@ -267,7 +267,7 @@ impl<'a> AstValidator<'a> { } fn dcx(&self) -> DiagCtxtHandle<'a> { - self.session.dcx() + self.sess.dcx() } fn visibility_not_permitted(&self, vis: &Visibility, note: errors::VisibilityNotPermittedNote) { @@ -359,7 +359,7 @@ impl<'a> AstValidator<'a> { in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }), const_context_label: parent_constness, remove_const_sugg: ( - self.session.source_map().span_extend_while_whitespace(span), + self.sess.source_map().span_extend_while_whitespace(span), match parent_constness { Some(_) => rustc_errors::Applicability::MachineApplicable, None => rustc_errors::Applicability::MaybeIncorrect, @@ -472,7 +472,7 @@ impl<'a> AstValidator<'a> { fn check_defaultness(&self, span: Span, defaultness: Defaultness) { if let Defaultness::Default(def_span) = defaultness { - let span = self.session.source_map().guess_head_span(span); + let span = self.sess.source_map().guess_head_span(span); self.dcx().emit_err(errors::ForbiddenDefault { span, def_span }); } } @@ -480,7 +480,7 @@ impl<'a> AstValidator<'a> { /// If `sp` ends with a semicolon, returns it as a `Span` /// Otherwise, returns `sp.shrink_to_hi()` fn ending_semi_or_hi(&self, sp: Span) -> Span { - let source_map = self.session.source_map(); + let source_map = self.sess.source_map(); let end = source_map.end_point(sp); if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") { @@ -552,7 +552,7 @@ impl<'a> AstValidator<'a> { } fn current_extern_span(&self) -> Span { - self.session.source_map().guess_head_span(self.extern_mod.unwrap()) + self.sess.source_map().guess_head_span(self.extern_mod.unwrap()) } /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`. @@ -648,7 +648,7 @@ impl<'a> AstValidator<'a> { if ident.name.as_str().is_ascii() { return; } - let span = self.session.source_map().guess_head_span(item_span); + let span = self.sess.source_map().guess_head_span(item_span); self.dcx().emit_err(errors::NoMangleAscii { span }); } @@ -753,7 +753,7 @@ impl<'a> AstValidator<'a> { self.dcx().emit_err(errors::PatternFnPointer { span }); }); if let Extern::Implicit(_) = bfty.ext { - let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo()); + let sig_span = self.sess.source_map().next_point(ty.span.shrink_to_lo()); self.maybe_lint_missing_abi(sig_span, ty.id); } } @@ -795,7 +795,7 @@ impl<'a> AstValidator<'a> { // FIXME(davidtwco): This is a hack to detect macros which produce spans of the // call site which do not have a macro backtrace. See #61963. if self - .session + .sess .source_map() .span_to_snippet(span) .is_ok_and(|snippet| !snippet.starts_with("#[")) @@ -885,7 +885,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara impl<'a> Visitor<'a> for AstValidator<'a> { fn visit_attribute(&mut self, attr: &Attribute) { - validate_attr::check_attr(&self.session.psess, attr); + validate_attr::check_attr(&self.sess.psess, attr); } fn visit_ty(&mut self, ty: &'a Ty) { @@ -1192,7 +1192,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } else if where_clauses.after.has_where_token { self.dcx().emit_err(errors::WhereClauseAfterTypeAlias { span: where_clauses.after.span, - help: self.session.is_nightly_build(), + help: self.sess.is_nightly_build(), }); } } @@ -1328,7 +1328,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { (BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_)) if !self.features.more_maybe_bounds => { - self.session + self.sess .create_feature_err( errors::OptionalTraitSupertrait { span: trait_ref.span, @@ -1341,7 +1341,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { (BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_)) if !self.features.more_maybe_bounds => { - self.session + self.sess .create_feature_err( errors::OptionalTraitObject { span: trait_ref.span }, sym::more_maybe_bounds, @@ -1752,13 +1752,13 @@ fn deny_equality_constraints( } pub fn check_crate( - session: &Session, + sess: &Session, features: &Features, krate: &Crate, lints: &mut LintBuffer, ) -> bool { let mut validator = AstValidator { - session, + sess, features, extern_mod: None, outer_trait_or_trait_impl: None, From 0ae796440a476a4ffe982fbb7cc54a9097e7f161 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 2 Oct 2024 20:54:09 +0200 Subject: [PATCH 4/5] Fix target_abi in SOLID targets The `armv7a-kmc-solid_asp3-eabi` and `armv7a-kmc-solid_asp3-eabihf` targets clearly have the ABI in their name, so it should also be exposed in Rust's `target_abi` cfg variable. --- .../rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabi.rs | 1 + .../src/spec/targets/armv7a_kmc_solid_asp3_eabihf.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabi.rs b/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabi.rs index 7e14c5efe7191..e5ae1064d9716 100644 --- a/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabi.rs +++ b/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabi.rs @@ -14,6 +14,7 @@ pub(crate) fn target() -> Target { data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".into(), arch: "arm".into(), options: TargetOptions { + abi: "eabi".into(), linker: Some("arm-kmc-eabi-gcc".into()), features: "+v7,+soft-float,+thumb2,-neon".into(), relocation_model: RelocModel::Static, diff --git a/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabihf.rs b/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabihf.rs index 1958f4a7c30a2..0879fa24a1b73 100644 --- a/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabihf.rs +++ b/compiler/rustc_target/src/spec/targets/armv7a_kmc_solid_asp3_eabihf.rs @@ -14,6 +14,7 @@ pub(crate) fn target() -> Target { data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".into(), arch: "arm".into(), options: TargetOptions { + abi: "eabihf".into(), linker: Some("arm-kmc-eabi-gcc".into()), features: "+v7,+vfp3,-d32,+thumb2,-neon".into(), relocation_model: RelocModel::Static, From 4b48d72eaa7f4d934f56bcf2b6d0348954977823 Mon Sep 17 00:00:00 2001 From: Jaken Herman Date: Wed, 2 Oct 2024 00:24:42 -0500 Subject: [PATCH 5/5] Add `get_line` confusable to `Stdin::read_line()` Add tests for addition of `#[rustc_confusables("get_line")]` --- library/std/src/io/stdio.rs | 1 + tests/ui/attributes/rustc_confusables_std_cases.rs | 4 ++++ .../attributes/rustc_confusables_std_cases.stderr | 13 ++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 6de069a518e3d..bf242e715bd94 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -394,6 +394,7 @@ impl Stdin { /// in which case it will wait for the Enter key to be pressed before /// continuing #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_confusables("get_line")] pub fn read_line(&self, buf: &mut String) -> io::Result { self.lock().read_line(buf) } diff --git a/tests/ui/attributes/rustc_confusables_std_cases.rs b/tests/ui/attributes/rustc_confusables_std_cases.rs index d9121695950b2..4f6baea26dfd7 100644 --- a/tests/ui/attributes/rustc_confusables_std_cases.rs +++ b/tests/ui/attributes/rustc_confusables_std_cases.rs @@ -23,4 +23,8 @@ fn main() { //~^ HELP you might have meant to use `push_str` String::new().append(""); //~ ERROR E0599 //~^ HELP you might have meant to use `push_str` + let mut buffer = String::new(); + let stdin = std::io::stdin(); + stdin.get_line(&mut buffer).unwrap(); //~ ERROR E0599 + //~^ HELP you might have meant to use `read_line` } diff --git a/tests/ui/attributes/rustc_confusables_std_cases.stderr b/tests/ui/attributes/rustc_confusables_std_cases.stderr index f4b6947ccd94a..7bf96241ca725 100644 --- a/tests/ui/attributes/rustc_confusables_std_cases.stderr +++ b/tests/ui/attributes/rustc_confusables_std_cases.stderr @@ -106,7 +106,18 @@ help: you might have meant to use `push_str` LL | String::new().push_str(""); | ~~~~~~~~ -error: aborting due to 8 previous errors +error[E0599]: no method named `get_line` found for struct `Stdin` in the current scope + --> $DIR/rustc_confusables_std_cases.rs:28:11 + | +LL | stdin.get_line(&mut buffer).unwrap(); + | ^^^^^^^^ method not found in `Stdin` + | +help: you might have meant to use `read_line` + | +LL | stdin.read_line(&mut buffer).unwrap(); + | ~~~~~~~~~ + +error: aborting due to 9 previous errors Some errors have detailed explanations: E0308, E0599. For more information about an error, try `rustc --explain E0308`.