From defcc44238d9d79b7bcf5dfae2ec33001f568dd0 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 15 Mar 2024 11:36:21 +0000 Subject: [PATCH 01/11] Make `unexpected` always "return" `PResult<()>` & add `unexpected_any` This prevents breakage when `?` no longer skews inference. --- compiler/rustc_builtin_macros/src/asm.rs | 2 +- compiler/rustc_builtin_macros/src/assert.rs | 2 +- compiler/rustc_parse/src/parser/attr.rs | 6 +++--- compiler/rustc_parse/src/parser/generics.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 12 ++++++------ compiler/rustc_parse/src/parser/mod.rs | 14 ++++++++++++-- compiler/rustc_parse/src/parser/path.rs | 2 +- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 62c02817011fd..76805617c933b 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -189,7 +189,7 @@ pub fn parse_asm_args<'a>( args.templates.push(template); continue; } else { - return p.unexpected(); + p.unexpected_any()? }; allow_templates = false; diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 35a0857fe51ce..5905bdd710849 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -151,7 +151,7 @@ fn parse_assert<'a>(cx: &mut ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PRes }; if parser.token != token::Eof { - return parser.unexpected(); + parser.unexpected()?; } Ok(Assert { cond_expr, custom_message }) diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index eb9a10f4bdab1..d08c50b5b0628 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -380,12 +380,12 @@ impl<'a> Parser<'a> { }; if let Some(item) = nt_meta { - return match item.meta(item.path.span) { + match item.meta(item.path.span) { Some(meta) => { self.bump(); - Ok(meta) + return Ok(meta); } - None => self.unexpected(), + None => self.unexpected()?, }; } diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index 263b2a9064356..fde16ac957dfe 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -481,7 +481,7 @@ impl<'a> Parser<'a> { })) } else { self.maybe_recover_bounds_doubled_colon(&ty)?; - self.unexpected() + self.unexpected_any() } } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 79492fe62ed97..18d210eacfa61 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1514,7 +1514,7 @@ impl<'a> Parser<'a> { let ident = this.parse_field_ident("enum", vlo)?; if this.token == token::Not { - if let Err(err) = this.unexpected::<()>() { + if let Err(err) = this.unexpected() { err.with_note(fluent::parse_macro_expands_to_enum_variant).emit(); } @@ -1937,7 +1937,7 @@ impl<'a> Parser<'a> { ) -> PResult<'a, FieldDef> { let name = self.parse_field_ident(adt_ty, lo)?; if self.token.kind == token::Not { - if let Err(mut err) = self.unexpected::() { + if let Err(mut err) = self.unexpected() { // Encounter the macro invocation err.subdiagnostic(self.dcx(), MacroExpandsToAdtField { adt_ty }); return Err(err); @@ -2067,7 +2067,7 @@ impl<'a> Parser<'a> { let params = self.parse_token_tree(); // `MacParams` let pspan = params.span(); if !self.check(&token::OpenDelim(Delimiter::Brace)) { - return self.unexpected(); + self.unexpected()?; } let body = self.parse_token_tree(); // `MacBody` // Convert `MacParams MacBody` into `{ MacParams => MacBody }`. @@ -2077,7 +2077,7 @@ impl<'a> Parser<'a> { let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi()); P(DelimArgs { dspan, delim: Delimiter::Brace, tokens }) } else { - return self.unexpected(); + self.unexpected_any()? }; self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span)); @@ -2692,7 +2692,7 @@ impl<'a> Parser<'a> { debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required); let (pat, colon) = this.parse_fn_param_pat_colon()?; if !colon { - let mut err = this.unexpected::<()>().unwrap_err(); + let mut err = this.unexpected().unwrap_err(); return if let Some(ident) = this.parameter_without_type(&mut err, pat, is_name_required, first_param) { @@ -2716,7 +2716,7 @@ impl<'a> Parser<'a> { { // This wasn't actually a type, but a pattern looking like a type, // so we are going to rollback and re-parse for recovery. - ty = this.unexpected(); + ty = this.unexpected_any(); } match ty { Ok(ty) => { diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 12879dc429bc9..125e77d8ee7c4 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -465,7 +465,9 @@ impl<'a> Parser<'a> { matches!(self.recovery, Recovery::Allowed) } - pub fn unexpected(&mut self) -> PResult<'a, T> { + /// Version of [`unexpected`](Parser::unexpected) that "returns" any type in the `Ok` + /// (both those functions never return "Ok", and so can lie like that in the type). + pub fn unexpected_any(&mut self) -> PResult<'a, T> { match self.expect_one_of(&[], &[]) { Err(e) => Err(e), // We can get `Ok(true)` from `recover_closing_delimiter` @@ -474,6 +476,10 @@ impl<'a> Parser<'a> { } } + pub fn unexpected(&mut self) -> PResult<'a, ()> { + self.unexpected_any() + } + /// Expects and consumes the token `t`. Signals an error if the next token is not `t`. pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, Recovered> { if self.expected_tokens.is_empty() { @@ -1278,7 +1284,11 @@ impl<'a> Parser<'a> { } fn parse_delim_args(&mut self) -> PResult<'a, P> { - if let Some(args) = self.parse_delim_args_inner() { Ok(P(args)) } else { self.unexpected() } + if let Some(args) = self.parse_delim_args_inner() { + Ok(P(args)) + } else { + self.unexpected_any() + } } fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 163d10d03f03c..7dd6cd0e80672 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -660,7 +660,7 @@ impl<'a> Parser<'a> { // Add `>` to the list of expected tokens. self.check(&token::Gt); // Handle `,` to `;` substitution - let mut err = self.unexpected::<()>().unwrap_err(); + let mut err = self.unexpected().unwrap_err(); self.bump(); err.span_suggestion_verbose( self.prev_token.span.until(self.token.span), From 75d940f6379572631209a64791cdb317a0279dac Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 15 Mar 2024 11:37:42 +0000 Subject: [PATCH 02/11] Use `do yeet ()` and `do yeet _` instead of `None?` and `Err(_)?` in compiler This prevents breakage when `?` no longer skews inference. --- compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_expand/src/module.rs | 2 +- .../rustc_infer/src/infer/error_reporting/need_type_info.rs | 4 ++-- compiler/rustc_infer/src/lib.rs | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index e550f7242c338..94852884d6ba5 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -12,6 +12,7 @@ #![feature(proc_macro_internals)] #![feature(proc_macro_span)] #![feature(try_blocks)] +#![feature(yeet_expr)] #![allow(rustc::diagnostic_outside_of_impl)] #![allow(internal_features)] diff --git a/compiler/rustc_expand/src/module.rs b/compiler/rustc_expand/src/module.rs index 8a68b39e4965e..c8983619e7016 100644 --- a/compiler/rustc_expand/src/module.rs +++ b/compiler/rustc_expand/src/module.rs @@ -62,7 +62,7 @@ pub(crate) fn parse_external_mod( // Ensure file paths are acyclic. if let Some(pos) = module.file_path_stack.iter().position(|p| p == &mp.file_path) { - Err(ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()))?; + do yeet ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()); } // Actually parse the external file as a module. diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 13e2152e45ea1..0686994b0378d 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -990,7 +990,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { let generics_def_id = tcx.res_generics_def_id(path.res)?; let generics = tcx.generics_of(generics_def_id); if generics.has_impl_trait() { - None?; + do yeet (); } let insert_span = path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi()); @@ -1044,7 +1044,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { let generics = tcx.generics_of(def_id); let segment: Option<_> = try { if !segment.infer_args || generics.has_impl_trait() { - None?; + do yeet (); } let span = tcx.hir().span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index 029bddda1e1c5..97e3dfe8d0743 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -27,6 +27,7 @@ #![feature(iterator_try_collect)] #![cfg_attr(bootstrap, feature(min_specialization))] #![feature(try_blocks)] +#![feature(yeet_expr)] #![recursion_limit = "512"] // For rustdoc #[macro_use] From ead18f43f817dee6737137c68e8f6b3126efa98b Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 24 Feb 2024 13:48:48 +0300 Subject: [PATCH 03/11] reorder clippy rules to their original order before passing them We need to keep the order of the given clippy lint rules before passing them. Since clap doesn't offer any useful interface for this purpose out of the box, we have to handle it manually. Signed-off-by: onur-ozkan --- src/bootstrap/src/core/build_steps/check.rs | 35 +++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs index a90139a070ac2..f582d5ada3e99 100644 --- a/src/bootstrap/src/core/build_steps/check.rs +++ b/src/bootstrap/src/core/build_steps/check.rs @@ -61,14 +61,11 @@ fn args(builder: &Builder<'_>) -> Vec { } } + let all_args = std::env::args().collect::>(); + args.extend(strings(&["--", "--cap-lints", "warn"])); args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); - let mut clippy_lint_levels: Vec = Vec::new(); - allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v))); - deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v))); - warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v))); - forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v))); - args.extend(clippy_lint_levels); + args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid)); args.extend(builder.config.free_args.clone()); args } else { @@ -76,6 +73,32 @@ fn args(builder: &Builder<'_>) -> Vec { } } +/// We need to keep the order of the given clippy lint rules before passing them. +/// Since clap doesn't offer any useful interface for this purpose out of the box, +/// we have to handle it manually. +pub(crate) fn get_clippy_rules_in_order( + all_args: &[String], + allow_rules: &[String], + deny_rules: &[String], + warn_rules: &[String], + forbid_rules: &[String], +) -> Vec { + let mut result = vec![]; + + for (prefix, item) in + [("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)] + { + item.iter().for_each(|v| { + let rule = format!("{prefix}{v}"); + let position = all_args.iter().position(|t| t == &rule).unwrap(); + result.push((position, rule)); + }); + } + + result.sort_by_key(|&(position, _)| position); + result.into_iter().map(|v| v.1).collect() +} + fn cargo_subcommand(kind: Kind) -> &'static str { match kind { Kind::Check => "check", From b43dc065dd65c1c459957559882b1f87896e0208 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 24 Feb 2024 15:08:11 +0300 Subject: [PATCH 04/11] add unit test: `order_of_clippy_rules` Signed-off-by: onur-ozkan --- src/bootstrap/src/core/config/tests.rs | 54 +++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/src/core/config/tests.rs b/src/bootstrap/src/core/config/tests.rs index 93ba5f4120ac1..ee8581ed50917 100644 --- a/src/bootstrap/src/core/config/tests.rs +++ b/src/bootstrap/src/core/config/tests.rs @@ -1,5 +1,6 @@ use super::{flags::Flags, ChangeIdWrapper, Config}; use crate::core::config::{LldMode, TomlConfig}; +use crate::core::build_steps::check::get_clippy_rules_in_order; use clap::CommandFactory; use serde::Deserialize; @@ -11,12 +12,13 @@ use std::{ }; fn parse(config: &str) -> Config { - let config = format!("{config} \r\n build.rustc = \"/does-not-exists\" "); Config::parse_inner( &[ - "check".to_owned(), - "--config=/does/not/exist".to_owned(), - "--skip-stage0-validation".to_owned(), + "check".to_string(), + "--set=build.rustc=/does/not/exist".to_string(), + "--set=build.cargo=/does/not/exist".to_string(), + "--config=/does/not/exist".to_string(), + "--skip-stage0-validation".to_string(), ], |&_| toml::from_str(&config).unwrap(), ) @@ -169,7 +171,10 @@ fn override_toml_duplicate() { Config::parse_inner( &[ "check".to_owned(), + "--set=build.rustc=/does/not/exist".to_string(), + "--set=build.cargo=/does/not/exist".to_string(), "--config=/does/not/exist".to_owned(), + "--skip-stage0-validation".to_owned(), "--set=change-id=1".to_owned(), "--set=change-id=2".to_owned(), ], @@ -192,7 +197,15 @@ fn profile_user_dist() { .and_then(|table: toml::Value| TomlConfig::deserialize(table)) .unwrap() } - Config::parse_inner(&["check".to_owned()], get_toml); + Config::parse_inner( + &[ + "check".to_owned(), + "--set=build.rustc=/does/not/exist".to_string(), + "--set=build.cargo=/does/not/exist".to_string(), + "--skip-stage0-validation".to_string(), + ], + get_toml, + ); } #[test] @@ -254,3 +267,34 @@ fn parse_change_id_with_unknown_field() { let change_id_wrapper: ChangeIdWrapper = toml::from_str(config).unwrap(); assert_eq!(change_id_wrapper.inner, Some(3461)); } + +#[test] +fn order_of_clippy_rules() { + let args = vec![ + "clippy".to_string(), + "--fix".to_string(), + "--allow-dirty".to_string(), + "--allow-staged".to_string(), + "-Aclippy:all".to_string(), + "-Wclippy::style".to_string(), + "-Aclippy::foo1".to_string(), + "-Aclippy::foo2".to_string(), + ]; + let config = Config::parse(&args); + + let actual = match &config.cmd { + crate::Subcommand::Clippy { allow, deny, warn, forbid, .. } => { + get_clippy_rules_in_order(&args, &allow, &deny, &warn, &forbid) + } + _ => panic!("invalid subcommand"), + }; + + let expected = vec![ + "-Aclippy:all".to_string(), + "-Wclippy::style".to_string(), + "-Aclippy::foo1".to_string(), + "-Aclippy::foo2".to_string(), + ]; + + assert_eq!(expected, actual); +} From 1945e8f1f6e54dc53ace8cf2314ed76301ff51ee Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 24 Feb 2024 16:02:13 +0300 Subject: [PATCH 05/11] pass ignored lints after manual ones Previously, when passing lint rules manually using `x clippy ..`, ignored lints would override manual ones. This change corrects the order by passing ignored lints after the manual ones. Signed-off-by: onur-ozkan --- src/bootstrap/src/core/build_steps/check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs index f582d5ada3e99..4d13cf94d918e 100644 --- a/src/bootstrap/src/core/build_steps/check.rs +++ b/src/bootstrap/src/core/build_steps/check.rs @@ -64,8 +64,8 @@ fn args(builder: &Builder<'_>) -> Vec { let all_args = std::env::args().collect::>(); args.extend(strings(&["--", "--cap-lints", "warn"])); - args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid)); + args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); args.extend(builder.config.free_args.clone()); args } else { From a61bf3093eb57d6b63522d3ec1f4acceede0363b Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 24 Feb 2024 16:08:28 +0300 Subject: [PATCH 06/11] use `--cap-lints` only when deny and forbid rules are not specified Signed-off-by: onur-ozkan --- src/bootstrap/src/core/build_steps/check.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs index 4d13cf94d918e..55180a82885bb 100644 --- a/src/bootstrap/src/core/build_steps/check.rs +++ b/src/bootstrap/src/core/build_steps/check.rs @@ -61,10 +61,15 @@ fn args(builder: &Builder<'_>) -> Vec { } } - let all_args = std::env::args().collect::>(); + args.extend(strings(&["--"])); + + if deny.is_empty() && forbid.is_empty() { + args.extend(strings(&["--cap-lints", "warn"])); + } - args.extend(strings(&["--", "--cap-lints", "warn"])); + let all_args = std::env::args().collect::>(); args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid)); + args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); args.extend(builder.config.free_args.clone()); args From d82d4196ac2e313330a4095dc5c4b0aede4b265a Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Tue, 19 Mar 2024 06:54:07 -0700 Subject: [PATCH 07/11] Expose ucred::peer_cred on QNX targets to enable dist builds --- library/std/src/os/unix/net/stream.rs | 6 ++++-- library/std/src/os/unix/net/ucred.rs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index d2e23bdee6c88..d67493aaf4d65 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -8,7 +8,8 @@ target_os = "macos", target_os = "watchos", target_os = "netbsd", - target_os = "openbsd" + target_os = "openbsd", + target_os = "nto" ))] use super::{peer_cred, UCred}; #[cfg(any(doc, target_os = "android", target_os = "linux"))] @@ -234,7 +235,8 @@ impl UnixStream { target_os = "macos", target_os = "watchos", target_os = "netbsd", - target_os = "openbsd" + target_os = "openbsd", + target_os = "nto" ))] pub fn peer_cred(&self) -> io::Result { peer_cred(self) diff --git a/library/std/src/os/unix/net/ucred.rs b/library/std/src/os/unix/net/ucred.rs index de09c93840a54..4c915c579066f 100644 --- a/library/std/src/os/unix/net/ucred.rs +++ b/library/std/src/os/unix/net/ucred.rs @@ -30,7 +30,8 @@ pub(super) use self::impl_linux::peer_cred; target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", - target_os = "netbsd" + target_os = "netbsd", + target_os = "nto" ))] pub(super) use self::impl_bsd::peer_cred; From 3d56178880a8f03b3d6322930de778d4064790ba Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 19 Mar 2024 11:37:28 -0400 Subject: [PATCH 08/11] Remove redundant coroutine captures note --- .../src/traits/error_reporting/suggestions.rs | 118 +++++++++--------- .../async-await/async-await-let-else.stderr | 1 - tests/ui/async-await/issue-68112.stderr | 1 - .../issue-70935-complex-spans.stderr | 2 - .../ui/async-await/issues/issue-67893.stderr | 1 - .../partial-drop-partial-reinit.rs | 1 - .../partial-drop-partial-reinit.stderr | 5 +- tests/ui/coroutine/issue-68112.rs | 1 - tests/ui/coroutine/issue-68112.stderr | 1 - .../print/coroutine-print-verbose-1.stderr | 1 - 10 files changed, 60 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index ee390d9c9f0ad..e5f7f805b052e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3199,71 +3199,69 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } }; - // Don't print the tuple of capture types - 'print: { - if !is_upvar_tys_infer_tuple { - let ty_str = tcx.short_ty_string(ty, &mut long_ty_file); - let msg = format!("required because it appears within the type `{ty_str}`"); - match ty.kind() { - ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) { - Some(ident) => err.span_note(ident.span, msg), - None => err.note(msg), - }, - ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => { - // If the previous type is async fn, this is the future generated by the body of an async function. - // Avoid printing it twice (it was already printed in the `ty::Coroutine` arm below). - let is_future = tcx.ty_is_opaque_future(ty); - debug!( - ?obligated_types, - ?is_future, - "note_obligation_cause_code: check for async fn" - ); - if is_future - && obligated_types.last().is_some_and(|ty| match ty.kind() { - ty::Coroutine(last_def_id, ..) => { - tcx.coroutine_is_async(*last_def_id) - } - _ => false, - }) - { - break 'print; - } - err.span_note(tcx.def_span(def_id), msg) + if !is_upvar_tys_infer_tuple { + let ty_str = tcx.short_ty_string(ty, &mut long_ty_file); + let msg = format!("required because it appears within the type `{ty_str}`"); + match ty.kind() { + ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) { + Some(ident) => { + err.span_note(ident.span, msg); } - ty::CoroutineWitness(def_id, args) => { - use std::fmt::Write; - - // FIXME: this is kind of an unusual format for rustc, can we make it more clear? - // Maybe we should just remove this note altogether? - // FIXME: only print types which don't meet the trait requirement - let mut msg = - "required because it captures the following types: ".to_owned(); - for bty in tcx.coroutine_hidden_types(*def_id) { - let ty = bty.instantiate(tcx, args); - write!(msg, "`{ty}`, ").unwrap(); - } - err.note(msg.trim_end_matches(", ").to_string()) + None => { + err.note(msg); } - ty::Coroutine(def_id, _) => { - let sp = tcx.def_span(def_id); - - // Special-case this to say "async block" instead of `[static coroutine]`. - let kind = tcx.coroutine_kind(def_id).unwrap(); - err.span_note( - sp, - with_forced_trimmed_paths!(format!( - "required because it's used within this {kind:#}", - )), - ) + }, + ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => { + // If the previous type is async fn, this is the future generated by the body of an async function. + // Avoid printing it twice (it was already printed in the `ty::Coroutine` arm below). + let is_future = tcx.ty_is_opaque_future(ty); + debug!( + ?obligated_types, + ?is_future, + "note_obligation_cause_code: check for async fn" + ); + if is_future + && obligated_types.last().is_some_and(|ty| match ty.kind() { + ty::Coroutine(last_def_id, ..) => { + tcx.coroutine_is_async(*last_def_id) + } + _ => false, + }) + { + // See comment above; skip printing twice. + } else { + err.span_note(tcx.def_span(def_id), msg); } - ty::Closure(def_id, _) => err.span_note( + } + ty::Coroutine(def_id, _) => { + let sp = tcx.def_span(def_id); + + // Special-case this to say "async block" instead of `[static coroutine]`. + let kind = tcx.coroutine_kind(def_id).unwrap(); + err.span_note( + sp, + with_forced_trimmed_paths!(format!( + "required because it's used within this {kind:#}", + )), + ); + } + ty::CoroutineWitness(..) => { + // Skip printing coroutine-witnesses, since we'll drill into + // the bad field in another derived obligation cause. + } + ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => { + err.span_note( tcx.def_span(def_id), "required because it's used within this closure", - ), - ty::Str => err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes"), - _ => err.note(msg), - }; - } + ); + } + ty::Str => { + err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes"); + } + _ => { + err.note(msg); + } + }; } obligated_types.push(ty); diff --git a/tests/ui/async-await/async-await-let-else.stderr b/tests/ui/async-await/async-await-let-else.stderr index 057906b49a37b..0952be2abe53b 100644 --- a/tests/ui/async-await/async-await-let-else.stderr +++ b/tests/ui/async-await/async-await-let-else.stderr @@ -38,7 +38,6 @@ LL | async fn bar2(_: T) -> ! { LL | | panic!() LL | | } | |_^ - = note: required because it captures the following types: `impl Future` note: required because it's used within this `async` fn body --> $DIR/async-await-let-else.rs:18:32 | diff --git a/tests/ui/async-await/issue-68112.stderr b/tests/ui/async-await/issue-68112.stderr index f92ac5dd0bc56..96fc1633cc916 100644 --- a/tests/ui/async-await/issue-68112.stderr +++ b/tests/ui/async-await/issue-68112.stderr @@ -58,7 +58,6 @@ note: required because it appears within the type `impl Future impl Future>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `impl Future>>`, `Ready` note: required because it's used within this `async` block --> $DIR/issue-68112.rs:57:20 | diff --git a/tests/ui/async-await/issue-70935-complex-spans.stderr b/tests/ui/async-await/issue-70935-complex-spans.stderr index 8dc3f476ec8dd..85c0c0c3088bb 100644 --- a/tests/ui/async-await/issue-70935-complex-spans.stderr +++ b/tests/ui/async-await/issue-70935-complex-spans.stderr @@ -25,7 +25,6 @@ LL | async fn baz(_c: impl FnMut() -> T) where T: Future { | ___________________________________________________________________^ LL | | } | |_^ - = note: required because it captures the following types: `impl Future` note: required because it's used within this `async` block --> $DIR/issue-70935-complex-spans.rs:18:5 | @@ -63,7 +62,6 @@ LL | async fn baz(_c: impl FnMut() -> T) where T: Future { | ___________________________________________________________________^ LL | | } | |_^ - = note: required because it captures the following types: `impl Future` note: required because it's used within this `async` block --> $DIR/issue-70935-complex-spans.rs:18:5 | diff --git a/tests/ui/async-await/issues/issue-67893.stderr b/tests/ui/async-await/issues/issue-67893.stderr index 12bbfc125521d..0c28aea44bb94 100644 --- a/tests/ui/async-await/issues/issue-67893.stderr +++ b/tests/ui/async-await/issues/issue-67893.stderr @@ -12,7 +12,6 @@ LL | pub async fn run() { | ------------------ within this `impl Future` | = help: within `impl Future`, the trait `Send` is not implemented for `MutexGuard<'_, ()>`, which is required by `impl Future: Send` - = note: required because it captures the following types: `Arc>`, `MutexGuard<'_, ()>`, `impl Future` note: required because it's used within this `async` fn body --> $DIR/auxiliary/issue_67893.rs:9:20 | diff --git a/tests/ui/async-await/partial-drop-partial-reinit.rs b/tests/ui/async-await/partial-drop-partial-reinit.rs index 36b3f2bc9ff65..b72552ed32479 100644 --- a/tests/ui/async-await/partial-drop-partial-reinit.rs +++ b/tests/ui/async-await/partial-drop-partial-reinit.rs @@ -8,7 +8,6 @@ fn main() { //~| NOTE cannot be sent //~| NOTE bound introduced by //~| NOTE appears within the type - //~| NOTE captures the following types } fn gimme_send(t: T) { diff --git a/tests/ui/async-await/partial-drop-partial-reinit.stderr b/tests/ui/async-await/partial-drop-partial-reinit.stderr index a6140c6db8286..0bd7d50b94164 100644 --- a/tests/ui/async-await/partial-drop-partial-reinit.stderr +++ b/tests/ui/async-await/partial-drop-partial-reinit.stderr @@ -11,9 +11,8 @@ LL | async fn foo() { | = help: within `impl Future`, the trait `Send` is not implemented for `NotSend`, which is required by `impl Future: Send` = note: required because it appears within the type `(NotSend,)` - = note: required because it captures the following types: `(NotSend,)`, `impl Future` note: required because it's used within this `async` fn body - --> $DIR/partial-drop-partial-reinit.rs:28:16 + --> $DIR/partial-drop-partial-reinit.rs:27:16 | LL | async fn foo() { | ________________^ @@ -25,7 +24,7 @@ LL | | bar().await; LL | | } | |_^ note: required by a bound in `gimme_send` - --> $DIR/partial-drop-partial-reinit.rs:14:18 + --> $DIR/partial-drop-partial-reinit.rs:13:18 | LL | fn gimme_send(t: T) { | ^^^^ required by this bound in `gimme_send` diff --git a/tests/ui/coroutine/issue-68112.rs b/tests/ui/coroutine/issue-68112.rs index e2be704dab738..ccec2acc97623 100644 --- a/tests/ui/coroutine/issue-68112.rs +++ b/tests/ui/coroutine/issue-68112.rs @@ -65,7 +65,6 @@ fn test2() { //~^ ERROR `RefCell` cannot be shared between threads safely //~| NOTE `RefCell` cannot be shared between threads safely //~| NOTE required for - //~| NOTE captures the following types //~| NOTE use `std::sync::RwLock` instead } diff --git a/tests/ui/coroutine/issue-68112.stderr b/tests/ui/coroutine/issue-68112.stderr index ded325eda544c..443195d36a30a 100644 --- a/tests/ui/coroutine/issue-68112.stderr +++ b/tests/ui/coroutine/issue-68112.stderr @@ -44,7 +44,6 @@ note: required because it appears within the type `impl Coroutine impl Coroutine>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `impl Coroutine>>` note: required because it's used within this coroutine --> $DIR/issue-68112.rs:60:20 | diff --git a/tests/ui/coroutine/print/coroutine-print-verbose-1.stderr b/tests/ui/coroutine/print/coroutine-print-verbose-1.stderr index 1b9ca632f0cb6..37db83d57f756 100644 --- a/tests/ui/coroutine/print/coroutine-print-verbose-1.stderr +++ b/tests/ui/coroutine/print/coroutine-print-verbose-1.stderr @@ -43,7 +43,6 @@ note: required because it appears within the type `Opaque(DefId(0:36 ~ coroutine | LL | fn make_non_send_coroutine2() -> impl Coroutine>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `Opaque(DefId(0:36 ~ coroutine_print_verbose_1[75fb]::make_non_send_coroutine2::{opaque#0}), [])` note: required because it's used within this coroutine --> $DIR/coroutine-print-verbose-1.rs:52:20 | From 49dd50f880aeb20b89c696acd56581808412a7ca Mon Sep 17 00:00:00 2001 From: Sky Date: Tue, 19 Mar 2024 14:28:01 -0400 Subject: [PATCH 09/11] Add "put" as a confusable for insert on hash map/set --- library/std/src/collections/hash/map.rs | 2 +- library/std/src/collections/hash/set.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 627befb63a1bb..2cc9afe92497b 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1101,7 +1101,7 @@ where /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_confusables("push", "append")] + #[rustc_confusables("push", "append", "put")] pub fn insert(&mut self, k: K, v: V) -> Option { self.base.insert(k, v) } diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 371201ff44cbe..3910100f212fb 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -885,7 +885,7 @@ where /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_confusables("push", "append")] + #[rustc_confusables("push", "append", "put")] pub fn insert(&mut self, value: T) -> bool { self.base.insert(value) } From de388882563652ab91106f3046a5e7ec93f3ba17 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 19 Mar 2024 13:31:28 +1100 Subject: [PATCH 10/11] Reduce `pub` usage in `rustc_session`. In particular, almost none of the errors in `errors.rs` are used outside the crate. --- compiler/rustc_session/src/config.rs | 54 ++++--- compiler/rustc_session/src/errors.rs | 208 +++++++++++++-------------- 2 files changed, 130 insertions(+), 132 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index e56684808bb50..e6eb1a3e83c6c 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -313,7 +313,7 @@ pub struct LocationDetail { } impl LocationDetail { - pub fn all() -> Self { + pub(crate) fn all() -> Self { Self { file: true, line: true, column: true } } } @@ -549,7 +549,7 @@ impl OutputTypes { OutputTypes(BTreeMap::from_iter(entries.iter().map(|&(k, ref v)| (k, v.clone())))) } - pub fn get(&self, key: &OutputType) -> Option<&Option> { + pub(crate) fn get(&self, key: &OutputType) -> Option<&Option> { self.0.get(key) } @@ -662,10 +662,6 @@ impl Externs { pub fn iter(&self) -> BTreeMapIter<'_, String, ExternEntry> { self.0.iter() } - - pub fn len(&self) -> usize { - self.0.len() - } } impl ExternEntry { @@ -854,13 +850,13 @@ impl OutFileName { #[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)] pub struct OutputFilenames { - pub out_directory: PathBuf, + pub(crate) out_directory: PathBuf, /// Crate name. Never contains '-'. crate_stem: String, /// Typically based on `.rs` input file name. Any '-' is preserved. filestem: String, pub single_output_file: Option, - pub temps_directory: Option, + temps_directory: Option, pub outputs: OutputTypes, } @@ -898,7 +894,7 @@ impl OutputFilenames { /// Gets the output path where a compilation artifact of the given type /// should be placed on disk. - pub fn output_path(&self, flavor: OutputType) -> PathBuf { + fn output_path(&self, flavor: OutputType) -> PathBuf { let extension = flavor.extension(); match flavor { OutputType::Metadata => { @@ -1092,7 +1088,7 @@ impl Options { || self.unstable_opts.query_dep_graph } - pub fn file_path_mapping(&self) -> FilePathMapping { + pub(crate) fn file_path_mapping(&self) -> FilePathMapping { file_path_mapping(self.remap_path_prefix.clone(), &self.unstable_opts) } @@ -1173,14 +1169,14 @@ pub enum Passes { } impl Passes { - pub fn is_empty(&self) -> bool { + fn is_empty(&self) -> bool { match *self { Passes::Some(ref v) => v.is_empty(), Passes::All => false, } } - pub fn extend(&mut self, passes: impl IntoIterator) { + pub(crate) fn extend(&mut self, passes: impl IntoIterator) { match *self { Passes::Some(ref mut v) => v.extend(passes), Passes::All => {} @@ -1206,7 +1202,7 @@ pub struct BranchProtection { pub pac_ret: Option, } -pub const fn default_lib_output() -> CrateType { +pub(crate) const fn default_lib_output() -> CrateType { CrateType::Rlib } @@ -1584,15 +1580,15 @@ pub fn build_target_config( } #[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub enum OptionStability { +enum OptionStability { Stable, Unstable, } pub struct RustcOptGroup { pub apply: Box &mut getopts::Options>, - pub name: &'static str, - pub stability: OptionStability, + name: &'static str, + stability: OptionStability, } impl RustcOptGroup { @@ -1628,8 +1624,8 @@ mod opt { use super::RustcOptGroup; - pub type R = RustcOptGroup; - pub type S = &'static str; + type R = RustcOptGroup; + type S = &'static str; fn stable(name: S, f: F) -> R where @@ -1649,32 +1645,34 @@ mod opt { if a.len() > b.len() { a } else { b } } - pub fn opt_s(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn opt_s(a: S, b: S, c: S, d: S) -> R { stable(longer(a, b), move |opts| opts.optopt(a, b, c, d)) } - pub fn multi_s(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn multi_s(a: S, b: S, c: S, d: S) -> R { stable(longer(a, b), move |opts| opts.optmulti(a, b, c, d)) } - pub fn flag_s(a: S, b: S, c: S) -> R { + pub(crate) fn flag_s(a: S, b: S, c: S) -> R { stable(longer(a, b), move |opts| opts.optflag(a, b, c)) } - pub fn flagmulti_s(a: S, b: S, c: S) -> R { + pub(crate) fn flagmulti_s(a: S, b: S, c: S) -> R { stable(longer(a, b), move |opts| opts.optflagmulti(a, b, c)) } - pub fn opt(a: S, b: S, c: S, d: S) -> R { + fn opt(a: S, b: S, c: S, d: S) -> R { unstable(longer(a, b), move |opts| opts.optopt(a, b, c, d)) } - pub fn multi(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn multi(a: S, b: S, c: S, d: S) -> R { unstable(longer(a, b), move |opts| opts.optmulti(a, b, c, d)) } } + static EDITION_STRING: LazyLock = LazyLock::new(|| { format!( "Specify which edition of the compiler to use when compiling code. \ The default is {DEFAULT_EDITION} and the latest stable edition is {LATEST_STABLE_EDITION}." ) }); + /// Returns the "short" subset of the rustc command line options, /// including metadata for each option, such as whether the option is /// part of the stable long-term interface for rustc. @@ -1864,9 +1862,9 @@ pub fn parse_color(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Col /// Possible json config files pub struct JsonConfig { pub json_rendered: HumanReadableErrorType, - pub json_artifact_notifications: bool, + json_artifact_notifications: bool, pub json_unused_externs: JsonUnusedExterns, - pub json_future_incompat: bool, + json_future_incompat: bool, } /// Report unused externs in event stream @@ -2992,7 +2990,7 @@ pub mod nightly_options { is_nightly_build(matches.opt_str("crate-name").as_deref()) } - pub fn is_nightly_build(krate: Option<&str>) -> bool { + fn is_nightly_build(krate: Option<&str>) -> bool { UnstableFeatures::from_environment(krate).is_nightly_build() } @@ -3199,7 +3197,7 @@ pub(crate) mod dep_tracking { use std::num::NonZero; use std::path::PathBuf; - pub trait DepTrackingHash { + pub(crate) trait DepTrackingHash { fn hash( &self, hasher: &mut DefaultHasher, diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index d523da1ad7e38..94dfbf1692312 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -12,9 +12,9 @@ use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTriple}; use crate::{config::CrateType, parse::ParseSess}; -pub struct FeatureGateError { - pub span: MultiSpan, - pub explain: DiagMessage, +pub(crate) struct FeatureGateError { + pub(crate) span: MultiSpan, + pub(crate) explain: DiagMessage, } impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError { @@ -26,22 +26,22 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError { #[derive(Subdiagnostic)] #[note(session_feature_diagnostic_for_issue)] -pub struct FeatureDiagnosticForIssue { - pub n: NonZero, +pub(crate) struct FeatureDiagnosticForIssue { + pub(crate) n: NonZero, } #[derive(Subdiagnostic)] #[note(session_feature_suggest_upgrade_compiler)] -pub struct SuggestUpgradeCompiler { +pub(crate) struct SuggestUpgradeCompiler { date: &'static str, } impl SuggestUpgradeCompiler { - pub fn ui_testing() -> Self { + pub(crate) fn ui_testing() -> Self { Self { date: "YYYY-MM-DD" } } - pub fn new() -> Option { + pub(crate) fn new() -> Option { let date = option_env!("CFG_VER_DATE")?; Some(Self { date }) @@ -50,108 +50,108 @@ impl SuggestUpgradeCompiler { #[derive(Subdiagnostic)] #[help(session_feature_diagnostic_help)] -pub struct FeatureDiagnosticHelp { - pub feature: Symbol, +pub(crate) struct FeatureDiagnosticHelp { + pub(crate) feature: Symbol, } #[derive(Subdiagnostic)] #[help(session_cli_feature_diagnostic_help)] -pub struct CliFeatureDiagnosticHelp { - pub feature: Symbol, +pub(crate) struct CliFeatureDiagnosticHelp { + pub(crate) feature: Symbol, } #[derive(Diagnostic)] #[diag(session_not_circumvent_feature)] -pub struct NotCircumventFeature; +pub(crate) struct NotCircumventFeature; #[derive(Diagnostic)] #[diag(session_linker_plugin_lto_windows_not_supported)] -pub struct LinkerPluginToWindowsNotSupported; +pub(crate) struct LinkerPluginToWindowsNotSupported; #[derive(Diagnostic)] #[diag(session_profile_use_file_does_not_exist)] -pub struct ProfileUseFileDoesNotExist<'a> { - pub path: &'a std::path::Path, +pub(crate) struct ProfileUseFileDoesNotExist<'a> { + pub(crate) path: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_profile_sample_use_file_does_not_exist)] -pub struct ProfileSampleUseFileDoesNotExist<'a> { - pub path: &'a std::path::Path, +pub(crate) struct ProfileSampleUseFileDoesNotExist<'a> { + pub(crate) path: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_target_requires_unwind_tables)] -pub struct TargetRequiresUnwindTables; +pub(crate) struct TargetRequiresUnwindTables; #[derive(Diagnostic)] #[diag(session_instrumentation_not_supported)] -pub struct InstrumentationNotSupported { - pub us: String, +pub(crate) struct InstrumentationNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_sanitizer_not_supported)] -pub struct SanitizerNotSupported { - pub us: String, +pub(crate) struct SanitizerNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_sanitizers_not_supported)] -pub struct SanitizersNotSupported { - pub us: String, +pub(crate) struct SanitizersNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_cannot_mix_and_match_sanitizers)] -pub struct CannotMixAndMatchSanitizers { - pub first: String, - pub second: String, +pub(crate) struct CannotMixAndMatchSanitizers { + pub(crate) first: String, + pub(crate) second: String, } #[derive(Diagnostic)] #[diag(session_cannot_enable_crt_static_linux)] -pub struct CannotEnableCrtStaticLinux; +pub(crate) struct CannotEnableCrtStaticLinux; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_requires_lto)] -pub struct SanitizerCfiRequiresLto; +pub(crate) struct SanitizerCfiRequiresLto; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_requires_single_codegen_unit)] -pub struct SanitizerCfiRequiresSingleCodegenUnit; +pub(crate) struct SanitizerCfiRequiresSingleCodegenUnit; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_canonical_jump_tables_requires_cfi)] -pub struct SanitizerCfiCanonicalJumpTablesRequiresCfi; +pub(crate) struct SanitizerCfiCanonicalJumpTablesRequiresCfi; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_generalize_pointers_requires_cfi)] -pub struct SanitizerCfiGeneralizePointersRequiresCfi; +pub(crate) struct SanitizerCfiGeneralizePointersRequiresCfi; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_normalize_integers_requires_cfi)] -pub struct SanitizerCfiNormalizeIntegersRequiresCfi; +pub(crate) struct SanitizerCfiNormalizeIntegersRequiresCfi; #[derive(Diagnostic)] #[diag(session_split_lto_unit_requires_lto)] -pub struct SplitLtoUnitRequiresLto; +pub(crate) struct SplitLtoUnitRequiresLto; #[derive(Diagnostic)] #[diag(session_unstable_virtual_function_elimination)] -pub struct UnstableVirtualFunctionElimination; +pub(crate) struct UnstableVirtualFunctionElimination; #[derive(Diagnostic)] #[diag(session_unsupported_dwarf_version)] -pub struct UnsupportedDwarfVersion { - pub dwarf_version: u32, +pub(crate) struct UnsupportedDwarfVersion { + pub(crate) dwarf_version: u32, } #[derive(Diagnostic)] #[diag(session_target_stack_protector_not_supported)] -pub struct StackProtectorNotSupportedForTarget<'a> { - pub stack_protector: StackProtector, - pub target_triple: &'a TargetTriple, +pub(crate) struct StackProtectorNotSupportedForTarget<'a> { + pub(crate) stack_protector: StackProtector, + pub(crate) target_triple: &'a TargetTriple, } #[derive(Diagnostic)] @@ -160,58 +160,58 @@ pub(crate) struct BranchProtectionRequiresAArch64; #[derive(Diagnostic)] #[diag(session_split_debuginfo_unstable_platform)] -pub struct SplitDebugInfoUnstablePlatform { - pub debuginfo: SplitDebuginfo, +pub(crate) struct SplitDebugInfoUnstablePlatform { + pub(crate) debuginfo: SplitDebuginfo, } #[derive(Diagnostic)] #[diag(session_file_is_not_writeable)] -pub struct FileIsNotWriteable<'a> { - pub file: &'a std::path::Path, +pub(crate) struct FileIsNotWriteable<'a> { + pub(crate) file: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_file_write_fail)] pub(crate) struct FileWriteFail<'a> { - pub path: &'a std::path::Path, - pub err: String, + pub(crate) path: &'a std::path::Path, + pub(crate) err: String, } #[derive(Diagnostic)] #[diag(session_crate_name_does_not_match)] -pub struct CrateNameDoesNotMatch { +pub(crate) struct CrateNameDoesNotMatch { #[primary_span] - pub span: Span, - pub s: Symbol, - pub name: Symbol, + pub(crate) span: Span, + pub(crate) s: Symbol, + pub(crate) name: Symbol, } #[derive(Diagnostic)] #[diag(session_crate_name_invalid)] -pub struct CrateNameInvalid<'a> { - pub s: &'a str, +pub(crate) struct CrateNameInvalid<'a> { + pub(crate) s: &'a str, } #[derive(Diagnostic)] #[diag(session_crate_name_empty)] -pub struct CrateNameEmpty { +pub(crate) struct CrateNameEmpty { #[primary_span] - pub span: Option, + pub(crate) span: Option, } #[derive(Diagnostic)] #[diag(session_invalid_character_in_create_name)] -pub struct InvalidCharacterInCrateName { +pub(crate) struct InvalidCharacterInCrateName { #[primary_span] - pub span: Option, - pub character: char, - pub crate_name: Symbol, + pub(crate) span: Option, + pub(crate) character: char, + pub(crate) crate_name: Symbol, #[subdiagnostic] - pub crate_name_help: Option, + pub(crate) crate_name_help: Option, } #[derive(Subdiagnostic)] -pub enum InvalidCrateNameHelp { +pub(crate) enum InvalidCrateNameHelp { #[help(session_invalid_character_in_create_name_help)] AddCrateName, } @@ -220,9 +220,9 @@ pub enum InvalidCrateNameHelp { #[multipart_suggestion(session_expr_parentheses_needed, applicability = "machine-applicable")] pub struct ExprParenthesesNeeded { #[suggestion_part(code = "(")] - pub left: Span, + left: Span, #[suggestion_part(code = ")")] - pub right: Span, + right: Span, } impl ExprParenthesesNeeded { @@ -233,13 +233,13 @@ impl ExprParenthesesNeeded { #[derive(Diagnostic)] #[diag(session_skipping_const_checks)] -pub struct SkippingConstChecks { +pub(crate) struct SkippingConstChecks { #[subdiagnostic] - pub unleashed_features: Vec, + pub(crate) unleashed_features: Vec, } #[derive(Subdiagnostic)] -pub enum UnleashedFeatureHelp { +pub(crate) enum UnleashedFeatureHelp { #[help(session_unleashed_feature_help_named)] Named { #[primary_span] @@ -255,101 +255,101 @@ pub enum UnleashedFeatureHelp { #[derive(Diagnostic)] #[diag(session_invalid_literal_suffix)] -pub(crate) struct InvalidLiteralSuffix<'a> { +struct InvalidLiteralSuffix<'a> { #[primary_span] #[label] - pub span: Span, + span: Span, // FIXME(#100717) - pub kind: &'a str, - pub suffix: Symbol, + kind: &'a str, + suffix: Symbol, } #[derive(Diagnostic)] #[diag(session_invalid_int_literal_width)] #[help] -pub(crate) struct InvalidIntLiteralWidth { +struct InvalidIntLiteralWidth { #[primary_span] - pub span: Span, - pub width: String, + span: Span, + width: String, } #[derive(Diagnostic)] #[diag(session_invalid_num_literal_base_prefix)] #[note] -pub(crate) struct InvalidNumLiteralBasePrefix { +struct InvalidNumLiteralBasePrefix { #[primary_span] #[suggestion(applicability = "maybe-incorrect", code = "{fixed}")] - pub span: Span, - pub fixed: String, + span: Span, + fixed: String, } #[derive(Diagnostic)] #[diag(session_invalid_num_literal_suffix)] #[help] -pub(crate) struct InvalidNumLiteralSuffix { +struct InvalidNumLiteralSuffix { #[primary_span] #[label] - pub span: Span, - pub suffix: String, + span: Span, + suffix: String, } #[derive(Diagnostic)] #[diag(session_invalid_float_literal_width)] #[help] -pub(crate) struct InvalidFloatLiteralWidth { +struct InvalidFloatLiteralWidth { #[primary_span] - pub span: Span, - pub width: String, + span: Span, + width: String, } #[derive(Diagnostic)] #[diag(session_invalid_float_literal_suffix)] #[help] -pub(crate) struct InvalidFloatLiteralSuffix { +struct InvalidFloatLiteralSuffix { #[primary_span] #[label] - pub span: Span, - pub suffix: String, + span: Span, + suffix: String, } #[derive(Diagnostic)] #[diag(session_int_literal_too_large)] #[note] -pub(crate) struct IntLiteralTooLarge { +struct IntLiteralTooLarge { #[primary_span] - pub span: Span, - pub limit: String, + span: Span, + limit: String, } #[derive(Diagnostic)] #[diag(session_hexadecimal_float_literal_not_supported)] -pub(crate) struct HexadecimalFloatLiteralNotSupported { +struct HexadecimalFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_octal_float_literal_not_supported)] -pub(crate) struct OctalFloatLiteralNotSupported { +struct OctalFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_binary_float_literal_not_supported)] -pub(crate) struct BinaryFloatLiteralNotSupported { +struct BinaryFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_unsupported_crate_type_for_target)] -pub struct UnsupportedCrateTypeForTarget<'a> { - pub crate_type: CrateType, - pub target_triple: &'a TargetTriple, +pub(crate) struct UnsupportedCrateTypeForTarget<'a> { + pub(crate) crate_type: CrateType, + pub(crate) target_triple: &'a TargetTriple, } pub fn report_lit_error( @@ -431,16 +431,16 @@ pub fn report_lit_error( #[derive(Diagnostic)] #[diag(session_optimization_fuel_exhausted)] -pub struct OptimisationFuelExhausted { - pub msg: String, +pub(crate) struct OptimisationFuelExhausted { + pub(crate) msg: String, } #[derive(Diagnostic)] #[diag(session_incompatible_linker_flavor)] #[note] -pub struct IncompatibleLinkerFlavor { - pub flavor: &'static str, - pub compatible_list: String, +pub(crate) struct IncompatibleLinkerFlavor { + pub(crate) flavor: &'static str, + pub(crate) compatible_list: String, } #[derive(Diagnostic)] @@ -453,6 +453,6 @@ pub(crate) struct FunctionReturnThunkExternRequiresNonLargeCodeModel; #[derive(Diagnostic)] #[diag(session_failed_to_create_profiler)] -pub struct FailedToCreateProfiler { - pub err: String, +pub(crate) struct FailedToCreateProfiler { + pub(crate) err: String, } From 81d7d7aabd5cdfb1e574a7ebae0b884e3aad8dea Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sun, 25 Feb 2024 01:11:09 +0300 Subject: [PATCH 11/11] resolve clippy errors Signed-off-by: onur-ozkan --- compiler/rustc_ast/src/token.rs | 1 + compiler/rustc_codegen_llvm/src/context.rs | 1 + .../rustc_const_eval/src/interpret/intern.rs | 4 +++- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 20 ++++++++----------- .../rustc_middle/src/mir/interpret/mod.rs | 4 ++-- .../rustc_mir_build/src/build/matches/mod.rs | 1 - compiler/stable_mir/src/mir/alloc.rs | 8 ++++---- library/std/src/io/buffered/bufreader.rs | 7 +++---- src/bootstrap/src/core/config/tests.rs | 2 +- 10 files changed, 24 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 5ccc7d51066d9..c17020ed6632e 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -11,6 +11,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_macros::HashStable_Generic; use rustc_span::symbol::{kw, sym}; +#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint. #[allow(hidden_glob_reexports)] use rustc_span::symbol::{Ident, Symbol}; use rustc_span::{edition::Edition, ErrorGuaranteed, Span, DUMMY_SP}; diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index f89c8c9f836bf..f9f7e0d4e3754 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -326,6 +326,7 @@ pub unsafe fn create_module<'ll>( // // On the wasm targets it will get hooked up to the "producer" sections // `processed-by` information. + #[allow(clippy::option_env_unwrap)] let rustc_producer = format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION")); let name_metadata = llvm::LLVMMDStringInContext( diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 17bb59aae8f17..6775a17987206 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -291,7 +291,9 @@ pub fn intern_const_alloc_for_constprop< return Ok(()); } // Move allocation to `tcx`. - for _ in intern_shallow(ecx, alloc_id, Mutability::Not).map_err(|()| err_ub!(DeadLocal))? { + if let Some(_) = + (intern_shallow(ecx, alloc_id, Mutability::Not).map_err(|()| err_ub!(DeadLocal))?).next() + { // We are not doing recursive interning, so we don't currently support provenance. // (If this assertion ever triggers, we should just implement a // proper recursive interning loop -- or just call `intern_const_alloc_recursive`. diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index d8cfceab460a0..9db8bddfae5f9 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2201,7 +2201,7 @@ impl Decodable for EncodedMetadata { let mmap = if len > 0 { let mut mmap = MmapMut::map_anon(len).unwrap(); for _ in 0..len { - (&mut mmap[..]).write(&[d.read_u8()]).unwrap(); + (&mut mmap[..]).write_all(&[d.read_u8()]).unwrap(); } mmap.flush().unwrap(); Some(mmap.make_read_only().unwrap()) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 5043bd855ccb9..b56bdb86129bd 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -114,20 +114,16 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> { if self.current_id == CRATE_HIR_ID { return None; } - loop { - // There are nodes that do not have entries, so we need to skip them. - let parent_id = self.map.def_key(self.current_id.owner.def_id).parent; - let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| { - let def_id = LocalDefId { local_def_index }; - self.map.tcx.local_def_id_to_hir_id(def_id).owner - }); - self.current_id = HirId::make_owner(parent_id.def_id); + let parent_id = self.map.def_key(self.current_id.owner.def_id).parent; + let parent_id = parent_id.map_or(CRATE_OWNER_ID, |local_def_index| { + let def_id = LocalDefId { local_def_index }; + self.map.tcx.local_def_id_to_hir_id(def_id).owner + }); + self.current_id = HirId::make_owner(parent_id.def_id); - // If this `HirId` doesn't have an entry, skip it and look for its `parent_id`. - let node = self.map.tcx.hir_owner_node(self.current_id.owner); - return Some((self.current_id.owner, node)); - } + let node = self.map.tcx.hir_owner_node(self.current_id.owner); + return Some((self.current_id.owner, node)); } } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index f9edbb3c5ae21..6275942bafe65 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -671,11 +671,11 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result { - source.read(&mut buf)?; + source.read_exact(&mut buf[..source.len()])?; Ok(u128::from_le_bytes(buf)) } Endian::Big => { - source.read(&mut buf[16 - source.len()..])?; + source.read_exact(&mut buf[16 - source.len()..])?; Ok(u128::from_be_bytes(buf)) } }; diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs index e7808ff880b57..d2cbbf9be32c3 100644 --- a/compiler/rustc_mir_build/src/build/matches/mod.rs +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -229,7 +229,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { span: Span, scrutinee_span: Span, ) -> BlockAnd<()> { - let scrutinee_span = scrutinee_span; let scrutinee_place = unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span)); diff --git a/compiler/stable_mir/src/mir/alloc.rs b/compiler/stable_mir/src/mir/alloc.rs index c780042ff261c..6645793343875 100644 --- a/compiler/stable_mir/src/mir/alloc.rs +++ b/compiler/stable_mir/src/mir/alloc.rs @@ -57,11 +57,11 @@ pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result { let mut buf = [0u8; std::mem::size_of::()]; match MachineInfo::target_endianess() { Endian::Little => { - bytes.read(&mut buf)?; + bytes.read_exact(&mut buf[..bytes.len()])?; Ok(u128::from_le_bytes(buf)) } Endian::Big => { - bytes.read(&mut buf[16 - bytes.len()..])?; + bytes.read_exact(&mut buf[16 - bytes.len()..])?; Ok(u128::from_be_bytes(buf)) } } @@ -72,11 +72,11 @@ pub(crate) fn read_target_int(mut bytes: &[u8]) -> Result { let mut buf = [0u8; std::mem::size_of::()]; match MachineInfo::target_endianess() { Endian::Little => { - bytes.read(&mut buf)?; + bytes.read_exact(&mut buf[..bytes.len()])?; Ok(i128::from_le_bytes(buf)) } Endian::Big => { - bytes.read(&mut buf[16 - bytes.len()..])?; + bytes.read_exact(&mut buf[16 - bytes.len()..])?; Ok(i128::from_be_bytes(buf)) } } diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index 83db332ee2558..acaa7e9228ecc 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -328,10 +328,9 @@ impl Read for BufReader { self.discard_buffer(); return self.inner.read_vectored(bufs); } - let nread = { - let mut rem = self.fill_buf()?; - rem.read_vectored(bufs)? - }; + let mut rem = self.fill_buf()?; + let nread = rem.read_vectored(bufs)?; + self.consume(nread); Ok(nread) } diff --git a/src/bootstrap/src/core/config/tests.rs b/src/bootstrap/src/core/config/tests.rs index ee8581ed50917..8cd538953c56d 100644 --- a/src/bootstrap/src/core/config/tests.rs +++ b/src/bootstrap/src/core/config/tests.rs @@ -1,6 +1,6 @@ use super::{flags::Flags, ChangeIdWrapper, Config}; -use crate::core::config::{LldMode, TomlConfig}; use crate::core::build_steps::check::get_clippy_rules_in_order; +use crate::core::config::{LldMode, TomlConfig}; use clap::CommandFactory; use serde::Deserialize;