From 5ab0548500a30134496ba28eb2291b0523b7346a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Tue, 22 Mar 2022 00:36:47 +0100 Subject: [PATCH 1/4] Stop flagging certain inner attrs as outer ones --- .../rustc_builtin_macros/src/source_util.rs | 11 +++++--- compiler/rustc_parse/src/parser/attr.rs | 26 +++++++++++-------- src/test/ui/parser/attr.rs | 1 - src/test/ui/parser/attr.stderr | 10 +------ .../issues/auxiliary/issue-94340-inc.rs | 3 +++ src/test/ui/parser/issues/issue-94340.rs | 8 ++++++ src/test/ui/parser/issues/issue-94340.stderr | 20 ++++++++++++++ 7 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/test/ui/parser/issues/auxiliary/issue-94340-inc.rs create mode 100644 src/test/ui/parser/issues/issue-94340.rs create mode 100644 src/test/ui/parser/issues/issue-94340.stderr diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs index be628c9202c..ece250f61d5 100644 --- a/compiler/rustc_builtin_macros/src/source_util.rs +++ b/compiler/rustc_builtin_macros/src/source_util.rs @@ -141,7 +141,7 @@ pub fn expand_include<'cx>( fn make_items(mut self: Box>) -> Option; 1]>> { let mut ret = SmallVec::new(); - while self.p.token != token::Eof { + loop { match self.p.parse_item(ForceCollect::No) { Err(mut err) => { err.emit(); @@ -149,9 +149,12 @@ pub fn expand_include<'cx>( } Ok(Some(item)) => ret.push(item), Ok(None) => { - let token = pprust::token_to_string(&self.p.token); - let msg = format!("expected item, found `{}`", token); - self.p.struct_span_err(self.p.token.span, &msg).emit(); + if self.p.token != token::Eof { + let token = pprust::token_to_string(&self.p.token); + let msg = format!("expected item, found `{}`", token); + self.p.struct_span_err(self.p.token.span, &msg).emit(); + } + break; } } diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index 9865588cff4..1724bab5caa 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -13,7 +13,7 @@ use tracing::debug; #[derive(Debug)] pub enum InnerAttrPolicy<'a> { Permitted, - Forbidden { reason: &'a str, saw_doc_comment: bool, prev_attr_sp: Option }, + Forbidden { reason: &'a str, saw_doc_comment: bool, prev_outer_attr_sp: Option }, } const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \ @@ -22,7 +22,7 @@ const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \ pub(super) const DEFAULT_INNER_ATTR_FORBIDDEN: InnerAttrPolicy<'_> = InnerAttrPolicy::Forbidden { reason: DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG, saw_doc_comment: false, - prev_attr_sp: None, + prev_outer_attr_sp: None, }; enum OuterAttributeType { @@ -34,14 +34,16 @@ enum OuterAttributeType { impl<'a> Parser<'a> { /// Parses attributes that appear before an item. pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper> { - let mut attrs: Vec = Vec::new(); + let mut outer_attrs: Vec = Vec::new(); let mut just_parsed_doc_comment = false; let start_pos = self.token_cursor.num_next_calls; loop { let attr = if self.check(&token::Pound) { + let prev_outer_attr_sp = outer_attrs.last().map(|attr| attr.span); + let inner_error_reason = if just_parsed_doc_comment { "an inner attribute is not permitted following an outer doc comment" - } else if !attrs.is_empty() { + } else if prev_outer_attr_sp.is_some() { "an inner attribute is not permitted following an outer attribute" } else { DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG @@ -49,7 +51,7 @@ impl<'a> Parser<'a> { let inner_parse_policy = InnerAttrPolicy::Forbidden { reason: inner_error_reason, saw_doc_comment: just_parsed_doc_comment, - prev_attr_sp: attrs.last().map(|a| a.span), + prev_outer_attr_sp, }; just_parsed_doc_comment = false; Some(self.parse_attribute(inner_parse_policy)?) @@ -97,12 +99,14 @@ impl<'a> Parser<'a> { }; if let Some(attr) = attr { - attrs.push(attr); + if attr.style == ast::AttrStyle::Outer { + outer_attrs.push(attr); + } } else { break; } } - Ok(AttrWrapper::new(attrs.into(), start_pos)) + Ok(AttrWrapper::new(outer_attrs.into(), start_pos)) } /// Matches `attribute = # ! [ meta_item ]`. @@ -215,15 +219,15 @@ impl<'a> Parser<'a> { } pub(super) fn error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerAttrPolicy<'_>) { - if let InnerAttrPolicy::Forbidden { reason, saw_doc_comment, prev_attr_sp } = policy { - let prev_attr_note = + if let InnerAttrPolicy::Forbidden { reason, saw_doc_comment, prev_outer_attr_sp } = policy { + let prev_outer_attr_note = if saw_doc_comment { "previous doc comment" } else { "previous outer attribute" }; let mut diag = self.struct_span_err(attr_sp, reason); - if let Some(prev_attr_sp) = prev_attr_sp { + if let Some(prev_outer_attr_sp) = prev_outer_attr_sp { diag.span_label(attr_sp, "not permitted following an outer attribute") - .span_label(prev_attr_sp, prev_attr_note); + .span_label(prev_outer_attr_sp, prev_outer_attr_note); } diag.note( diff --git a/src/test/ui/parser/attr.rs b/src/test/ui/parser/attr.rs index 91a4abbd038..42b2dfde855 100644 --- a/src/test/ui/parser/attr.rs +++ b/src/test/ui/parser/attr.rs @@ -3,5 +3,4 @@ fn main() {} #![lang = "foo"] //~ ERROR an inner attribute is not permitted in this context - //~| ERROR definition of an unknown language item: `foo` fn foo() {} diff --git a/src/test/ui/parser/attr.stderr b/src/test/ui/parser/attr.stderr index 3cec61fe41e..3527274bd0f 100644 --- a/src/test/ui/parser/attr.stderr +++ b/src/test/ui/parser/attr.stderr @@ -3,7 +3,6 @@ error: an inner attribute is not permitted in this context | LL | #![lang = "foo"] | ^^^^^^^^^^^^^^^^ -LL | LL | fn foo() {} | ----------- the inner attribute doesn't annotate this function | @@ -14,12 +13,5 @@ LL - #![lang = "foo"] LL + #[lang = "foo"] | -error[E0522]: definition of an unknown language item: `foo` - --> $DIR/attr.rs:5:1 - | -LL | #![lang = "foo"] - | ^^^^^^^^^^^^^^^^ definition of unknown language item `foo` +error: aborting due to previous error -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0522`. diff --git a/src/test/ui/parser/issues/auxiliary/issue-94340-inc.rs b/src/test/ui/parser/issues/auxiliary/issue-94340-inc.rs new file mode 100644 index 00000000000..9429e514339 --- /dev/null +++ b/src/test/ui/parser/issues/auxiliary/issue-94340-inc.rs @@ -0,0 +1,3 @@ +// include file for issue-94340.rs +#![deny(rust_2018_idioms)] +#![deny(unused_must_use)] diff --git a/src/test/ui/parser/issues/issue-94340.rs b/src/test/ui/parser/issues/issue-94340.rs new file mode 100644 index 00000000000..d0fb84a689a --- /dev/null +++ b/src/test/ui/parser/issues/issue-94340.rs @@ -0,0 +1,8 @@ +// Make sure that unexpected inner attributes are not labeled as outer ones in diagnostics when +// trying to parse an item and that they are subsequently ignored not triggering confusing extra +// diagnostics like "expected item after attributes" which is not true for `include!` which can +// include empty files. + +include!("auxiliary/issue-94340-inc.rs"); + +fn main() {} diff --git a/src/test/ui/parser/issues/issue-94340.stderr b/src/test/ui/parser/issues/issue-94340.stderr new file mode 100644 index 00000000000..9fd7c38a80b --- /dev/null +++ b/src/test/ui/parser/issues/issue-94340.stderr @@ -0,0 +1,20 @@ +error: an inner attribute is not permitted in this context + --> $DIR/auxiliary/issue-94340-inc.rs:2:1 + | +LL | #![deny(rust_2018_idioms)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files + = note: outer attributes, like `#[test]`, annotate the item following them + +error: an inner attribute is not permitted in this context + --> $DIR/auxiliary/issue-94340-inc.rs:3:1 + | +LL | #![deny(unused_must_use)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files + = note: outer attributes, like `#[test]`, annotate the item following them + +error: aborting due to 2 previous errors + From c7c80577c2a4246311e09af61d8cfe4324ce79ab Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 6 Apr 2022 21:43:34 -0700 Subject: [PATCH 2/4] regression test for #82866 --- src/test/ui/match/issue-82866.rs | 7 +++++++ src/test/ui/match/issue-82866.stderr | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/test/ui/match/issue-82866.rs create mode 100644 src/test/ui/match/issue-82866.stderr diff --git a/src/test/ui/match/issue-82866.rs b/src/test/ui/match/issue-82866.rs new file mode 100644 index 00000000000..95cd62261f1 --- /dev/null +++ b/src/test/ui/match/issue-82866.rs @@ -0,0 +1,7 @@ +fn main() { + match x { + //~^ ERROR cannot find value `x` in this scope + Some::(v) => (), + //~^ ERROR cannot find type `v` in this scope + } +} diff --git a/src/test/ui/match/issue-82866.stderr b/src/test/ui/match/issue-82866.stderr new file mode 100644 index 00000000000..f9e3360a525 --- /dev/null +++ b/src/test/ui/match/issue-82866.stderr @@ -0,0 +1,16 @@ +error[E0425]: cannot find value `x` in this scope + --> $DIR/issue-82866.rs:2:11 + | +LL | match x { + | ^ not found in this scope + +error[E0412]: cannot find type `v` in this scope + --> $DIR/issue-82866.rs:4:16 + | +LL | Some::(v) => (), + | ^ not found in this scope + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0412, E0425. +For more information about an error, try `rustc --explain E0412`. From b399e7ea7c99a24f43e9562b0fb5223833a8bb50 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Thu, 7 Apr 2022 07:52:07 +0300 Subject: [PATCH 3/4] Correct safety reasoning in `str::make_ascii_{lower,upper}case()` --- library/core/src/str/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 6bfa6a5e015..82208c4deaa 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -2407,7 +2407,7 @@ impl str { #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[inline] pub fn make_ascii_uppercase(&mut self) { - // SAFETY: safe because we transmute two types with the same layout. + // SAFETY: changing ASCII letters only does not invalidate UTF-8. let me = unsafe { self.as_bytes_mut() }; me.make_ascii_uppercase() } @@ -2434,7 +2434,7 @@ impl str { #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[inline] pub fn make_ascii_lowercase(&mut self) { - // SAFETY: safe because we transmute two types with the same layout. + // SAFETY: changing ASCII letters only does not invalidate UTF-8. let me = unsafe { self.as_bytes_mut() }; me.make_ascii_lowercase() } From ef59ab738e9e6b6021294cd31901ee42c81b67ec Mon Sep 17 00:00:00 2001 From: James 'zofrex' Sanderson Date: Thu, 7 Apr 2022 08:51:59 +0100 Subject: [PATCH 4/4] Use gender neutral terms --- compiler/rustc_expand/src/mbe/macro_parser.rs | 2 +- compiler/rustc_typeck/src/check/wfcheck.rs | 2 +- compiler/rustc_typeck/src/collect.rs | 2 +- library/std/src/fs/tests.rs | 6 +++--- library/std/src/thread/local.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs index ffe8b10e687..604e25a14e6 100644 --- a/compiler/rustc_expand/src/mbe/macro_parser.rs +++ b/compiler/rustc_expand/src/mbe/macro_parser.rs @@ -315,7 +315,7 @@ pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize { /// only on the nesting depth of repetitions in the originating token tree it /// was derived from. /// -/// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular +/// In layperson's terms: `NamedMatch` will form a tree representing nested matches of a particular /// meta variable. For example, if we are matching the following macro against the following /// invocation... /// diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index 9b1767c7835..b2be70e707d 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -1657,7 +1657,7 @@ fn receiver_is_valid<'fcx, 'tcx>( } } else { debug!("receiver_is_valid: type `{:?}` does not deref to `{:?}`", receiver_ty, self_ty); - // If he receiver already has errors reported due to it, consider it valid to avoid + // If the receiver already has errors reported due to it, consider it valid to avoid // unnecessary errors (#58712). return receiver_ty.references_error(); } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 90b880adcd0..ec783a16ef7 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2700,7 +2700,7 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Linkage { // Use the names from src/llvm/docs/LangRef.rst here. Most types are only // applicable to variable declarations and may not really make sense for // Rust code in the first place but allow them anyway and trust that the - // user knows what s/he's doing. Who knows, unanticipated use cases may pop + // user knows what they're doing. Who knows, unanticipated use cases may pop // up in the future. // // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index 3fa731c9529..e8d0132f4b9 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -644,9 +644,9 @@ fn recursive_rmdir_toctou() { // Test for time-of-check to time-of-use issues. // // Scenario: - // The attacker wants to get directory contents deleted, to which he does not have access. - // He has a way to get a privileged Rust binary call `std::fs::remove_dir_all()` on a - // directory he controls, e.g. in his home directory. + // The attacker wants to get directory contents deleted, to which they do not have access. + // They have a way to get a privileged Rust binary call `std::fs::remove_dir_all()` on a + // directory they control, e.g. in their home directory. // // The POC sets up the `attack_dest/attack_file` which the attacker wants to have deleted. // The attacker repeatedly creates a directory and replaces it with a symlink from diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index a41cb02a607..fc307c5666d 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -980,7 +980,7 @@ pub mod fast { unsafe fn try_initialize T>(&self, init: F) -> Option<&'static T> { // SAFETY: See comment above (this function doc). if !mem::needs_drop::() || unsafe { self.try_register_dtor() } { - // SAFETY: See comment above (his function doc). + // SAFETY: See comment above (this function doc). Some(unsafe { self.inner.initialize(init) }) } else { None