From 4a2214885d469b27624df33145863037ca148f56 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 16 May 2022 19:29:45 +0200 Subject: [PATCH 1/6] Clarify slice and Vec iteration order While already being inferable from the doc examples, it wasn't fully specified. This is the only logical way to do a slice iterator. --- library/alloc/src/vec/mod.rs | 11 +++++++---- library/core/src/slice/mod.rs | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 3dc8a4fbba86b..a1fd9e8de595e 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2626,10 +2626,13 @@ impl IntoIterator for Vec { /// /// ``` /// let v = vec!["a".to_string(), "b".to_string()]; - /// for s in v.into_iter() { - /// // s has type String, not &String - /// println!("{s}"); - /// } + /// let mut v_iter = v.into_iter(); + /// + /// let first_element: Option = v_iter.next(); + /// + /// assert_eq!(first_element, Some("a".to_string())); + /// assert_eq!(v_iter.next(), Some("b".to_string())); + /// assert_eq!(v_iter.next(), None); /// ``` #[inline] fn into_iter(self) -> IntoIter { diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index a226dea54a4f2..7aedea48008c5 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -716,6 +716,8 @@ impl [T] { /// Returns an iterator over the slice. /// + /// The iterator yields all items from start to end. + /// /// # Examples /// /// ``` @@ -735,6 +737,8 @@ impl [T] { /// Returns an iterator that allows modifying each value. /// + /// The iterator yields all items from start to end. + /// /// # Examples /// /// ``` From 8cece636b22620717d6e242ec20fa8d5cf979072 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sat, 21 May 2022 13:53:26 -0400 Subject: [PATCH 2/6] Remove feature: `crate` visibility modifier --- compiler/rustc_ast/src/ast.rs | 11 +-------- compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast_passes/src/feature_gate.rs | 13 ---------- .../rustc_ast_pretty/src/pprust/state/item.rs | 5 +--- compiler/rustc_feature/src/active.rs | 2 -- compiler/rustc_feature/src/removed.rs | 2 ++ compiler/rustc_lint/src/builtin.rs | 8 +------ compiler/rustc_parse/src/parser/item.rs | 3 +-- compiler/rustc_parse/src/parser/mod.rs | 24 ++++--------------- .../rustc_resolve/src/build_reduced_graph.rs | 4 +--- .../crate-visibility-modifier.md | 20 ---------------- .../feature-gate-crate_visibility_modifier.rs | 8 ------- ...ture-gate-crate_visibility_modifier.stderr | 12 ---------- src/test/ui/macros/stringify.rs | 4 +--- .../clippy/clippy_utils/src/ast_utils.rs | 2 +- src/tools/rustfmt/src/items.rs | 2 +- src/tools/rustfmt/src/utils.rs | 13 ++++------ 17 files changed, 19 insertions(+), 116 deletions(-) delete mode 100644 src/doc/unstable-book/src/language-features/crate-visibility-modifier.md delete mode 100644 src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.rs delete mode 100644 src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.stderr diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 0deeb6849a216..534b56a998175 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2566,15 +2566,6 @@ impl PolyTraitRef { } } -#[derive(Copy, Clone, Encodable, Decodable, Debug, HashStable_Generic)] -pub enum CrateSugar { - /// Source is `pub(crate)`. - PubCrate, - - /// Source is (just) `crate`. - JustCrate, -} - #[derive(Clone, Encodable, Decodable, Debug)] pub struct Visibility { pub kind: VisibilityKind, @@ -2585,7 +2576,7 @@ pub struct Visibility { #[derive(Clone, Encodable, Decodable, Debug)] pub enum VisibilityKind { Public, - Crate(CrateSugar), + Crate, Restricted { path: P, id: NodeId }, Inherited, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 1a93da8788a1f..d7d112987cb71 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1469,7 +1469,7 @@ pub fn noop_flat_map_stmt_kind( pub fn noop_visit_vis(visibility: &mut Visibility, vis: &mut T) { match &mut visibility.kind { - VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {} + VisibilityKind::Public | VisibilityKind::Crate | VisibilityKind::Inherited => {} VisibilityKind::Restricted { path, id } => { vis.visit_path(path); vis.visit_id(id); diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 2774bda24f148..f0b94047ed9da 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -697,18 +697,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } visit::walk_assoc_item(self, i, ctxt) } - - fn visit_vis(&mut self, vis: &'a ast::Visibility) { - if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind { - gate_feature_post!( - &self, - crate_visibility_modifier, - vis.span, - "`crate` visibility modifier is experimental" - ); - } - visit::walk_vis(self, vis) - } } pub fn check_crate(krate: &ast::Crate, sess: &Session) { @@ -770,7 +758,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) { gate_all!(trait_alias, "trait aliases are experimental"); gate_all!(associated_type_bounds, "associated type bounds are unstable"); - gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental"); gate_all!(decl_macro, "`macro` is experimental"); gate_all!(box_patterns, "box pattern syntax is experimental"); gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental"); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 0de5e2099fd0b..7151a9f693898 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -403,10 +403,7 @@ impl<'a> State<'a> { pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { match vis.kind { ast::VisibilityKind::Public => self.word_nbsp("pub"), - ast::VisibilityKind::Crate(sugar) => match sugar { - ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"), - ast::CrateSugar::JustCrate => self.word_nbsp("crate"), - }, + ast::VisibilityKind::Crate => self.word_nbsp("pub(crate)"), ast::VisibilityKind::Restricted { ref path, .. } => { let path = Self::to_string(|s| s.print_path(path, false, 0)); if path == "self" || path == "super" { diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 520769d308e66..1956327dfaba4 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -351,8 +351,6 @@ declare_features! ( (active, const_trait_impl, "1.42.0", Some(67792), None), /// Allows the `?` operator in const contexts. (active, const_try, "1.56.0", Some(74935), None), - /// Allows using `crate` as visibility modifier, synonymous with `pub(crate)`. - (active, crate_visibility_modifier, "1.23.0", Some(53120), None), /// Allows non-builtin attributes in inner attribute position. (active, custom_inner_attributes, "1.30.0", Some(54726), None), /// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`. diff --git a/compiler/rustc_feature/src/removed.rs b/compiler/rustc_feature/src/removed.rs index fae9bd633afff..b546662dc1496 100644 --- a/compiler/rustc_feature/src/removed.rs +++ b/compiler/rustc_feature/src/removed.rs @@ -72,6 +72,8 @@ declare_features! ( /// Allows `T: ?const Trait` syntax in bounds. (removed, const_trait_bound_opt_out, "1.42.0", Some(67794), None, Some("Removed in favor of `~const` bound in #![feature(const_trait_impl)]")), + /// Allows using `crate` as visibility modifier, synonymous with `pub(crate)`. + (removed, crate_visibility_modifier, "1.63.0", Some(53120), None, Some("removed in favor of `pub(crate)`")), /// Allows using custom attributes (RFC 572). (removed, custom_attribute, "1.0.0", Some(29642), None, Some("removed in favor of `#![register_tool]` and `#![register_attr]`")), diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 3d50ed2096e50..9317858aed909 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1372,17 +1372,11 @@ impl UnreachablePub { let def_span = cx.tcx.sess.source_map().guess_head_span(span); cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| { let mut err = lint.build(&format!("unreachable `pub` {}", what)); - let replacement = if cx.tcx.features().crate_visibility_modifier { - "crate" - } else { - "pub(crate)" - } - .to_owned(); err.span_suggestion( vis_span, "consider restricting its visibility", - replacement, + "pub(crate)".to_owned(), applicability, ); if exportable { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 0f940cffcc463..e99347206fe50 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -302,8 +302,7 @@ impl<'a> Parser<'a> { /// When parsing a statement, would the start of a path be an item? pub(super) fn is_path_start_item(&mut self) -> bool { - self.is_crate_vis() // no: `crate::b`, yes: `crate $item` - || self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }` + self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }` || self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }` || self.is_async_fn() // no(2015): `async::b`, yes: `async fn` || matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) // no: `macro_rules::b`, yes: `macro_rules! mac` diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 5bd07c31c0e7b..ebe3d00cf73b1 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -25,7 +25,7 @@ use rustc_ast::tokenstream::{self, DelimSpan, Spacing}; use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::AttrId; use rustc_ast::DUMMY_NODE_ID; -use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, CrateSugar, Extern}; +use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, Extern}; use rustc_ast::{Async, Expr, ExprKind, MacArgs, MacArgsEq, MacDelimiter, Mutability, StrLit}; use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind}; use rustc_ast_pretty::pprust; @@ -1245,12 +1245,8 @@ impl<'a> Parser<'a> { res } - fn is_crate_vis(&self) -> bool { - self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep) - } - - /// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `crate` for `pub(crate)`, - /// `pub(self)` for `pub(in self)` and `pub(super)` for `pub(in super)`. + /// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `pub(self)` for `pub(in self)` + /// and `pub(super)` for `pub(in super)`. /// If the following element can't be a tuple (i.e., it's a function definition), then /// it's not a tuple struct field), and the contents within the parentheses aren't valid, /// so emit a proper diagnostic. @@ -1258,17 +1254,6 @@ impl<'a> Parser<'a> { pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> { maybe_whole!(self, NtVis, |x| x.into_inner()); - self.expected_tokens.push(TokenType::Keyword(kw::Crate)); - if self.is_crate_vis() { - self.bump(); // `crate` - self.sess.gated_spans.gate(sym::crate_visibility_modifier, self.prev_token.span); - return Ok(Visibility { - span: self.prev_token.span, - kind: VisibilityKind::Crate(CrateSugar::JustCrate), - tokens: None, - }); - } - if !self.eat_keyword(kw::Pub) { // We need a span for our `Spanned`, but there's inherently no // keyword to grab a span from for inherited visibility; an empty span at the @@ -1293,10 +1278,9 @@ impl<'a> Parser<'a> { self.bump(); // `(` self.bump(); // `crate` self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)` - let vis = VisibilityKind::Crate(CrateSugar::PubCrate); return Ok(Visibility { span: lo.to(self.prev_token.span), - kind: vis, + kind: VisibilityKind::Crate, tokens: None, }); } else if self.is_keyword_ahead(1, &[kw::In]) { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 3879779635b32..44d413081cedb 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -249,9 +249,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let parent_scope = &self.parent_scope; match vis.kind { ast::VisibilityKind::Public => Ok(ty::Visibility::Public), - ast::VisibilityKind::Crate(..) => { - Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())) - } + ast::VisibilityKind::Crate => Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())), ast::VisibilityKind::Inherited => { Ok(match self.parent_scope.module.kind { // Any inherited visibility resolved directly inside an enum or trait diff --git a/src/doc/unstable-book/src/language-features/crate-visibility-modifier.md b/src/doc/unstable-book/src/language-features/crate-visibility-modifier.md deleted file mode 100644 index b59859dd348e7..0000000000000 --- a/src/doc/unstable-book/src/language-features/crate-visibility-modifier.md +++ /dev/null @@ -1,20 +0,0 @@ -# `crate_visibility_modifier` - -The tracking issue for this feature is: [#53120] - -[#53120]: https://github.com/rust-lang/rust/issues/53120 - ------ - -The `crate_visibility_modifier` feature allows the `crate` keyword to be used -as a visibility modifier synonymous to `pub(crate)`, indicating that a type -(function, _&c._) is to be visible to the entire enclosing crate, but not to -other crates. - -```rust -#![feature(crate_visibility_modifier)] - -crate struct Foo { - bar: usize, -} -``` diff --git a/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.rs b/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.rs deleted file mode 100644 index 0e3f6b168be6b..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.rs +++ /dev/null @@ -1,8 +0,0 @@ -crate struct Bender { //~ ERROR `crate` visibility modifier is experimental - earth: bool, - fire: bool, - air: bool, - water: bool, -} - -fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.stderr b/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.stderr deleted file mode 100644 index 969606375c3e9..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-crate_visibility_modifier.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0658]: `crate` visibility modifier is experimental - --> $DIR/feature-gate-crate_visibility_modifier.rs:1:1 - | -LL | crate struct Bender { - | ^^^^^ - | - = note: see issue #53120 for more information - = help: add `#![feature(crate_visibility_modifier)]` to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/macros/stringify.rs b/src/test/ui/macros/stringify.rs index 57e5ab42f79d3..f246aa26a9d03 100644 --- a/src/test/ui/macros/stringify.rs +++ b/src/test/ui/macros/stringify.rs @@ -861,10 +861,8 @@ fn test_vis() { // VisibilityKind::Public assert_eq!(stringify_vis!(pub), "pub "); - // VisibilityKind::Crate - assert_eq!(stringify_vis!(crate), "crate "); - // VisibilityKind::Restricted + assert_eq!(stringify_vis!(pub(crate)), "pub(crate) "); assert_eq!(stringify_vis!(pub(self)), "pub(self) "); assert_eq!(stringify_vis!(pub(super)), "pub(super) "); assert_eq!(stringify_vis!(pub(in self)), "pub(self) "); diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 7919800483f52..9aa3793520893 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -545,7 +545,7 @@ pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool { pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool { use VisibilityKind::*; match (&l.kind, &r.kind) { - (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true, + (Public, Public) | (Inherited, Inherited) | (Crate, Crate) => true, (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r), _ => false, } diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index ad2502b041840..ecbd44e197624 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1361,7 +1361,7 @@ pub(crate) fn format_struct_struct( fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos { match vis.kind { - ast::VisibilityKind::Crate(..) | ast::VisibilityKind::Restricted { .. } => vis.span.hi(), + ast::VisibilityKind::Crate | ast::VisibilityKind::Restricted { .. } => vis.span.hi(), _ => default_span.lo(), } } diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index ed418fb1fece6..4a66c168bb95d 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use rustc_ast::ast::{ - self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility, + self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility, VisibilityKind, }; use rustc_ast::ptr; @@ -46,12 +46,8 @@ pub(crate) fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool { (VisibilityKind::Public, VisibilityKind::Public) | (VisibilityKind::Inherited, VisibilityKind::Inherited) | ( - VisibilityKind::Crate(CrateSugar::PubCrate), - VisibilityKind::Crate(CrateSugar::PubCrate), - ) - | ( - VisibilityKind::Crate(CrateSugar::JustCrate), - VisibilityKind::Crate(CrateSugar::JustCrate), + VisibilityKind::Crate, + VisibilityKind::Crate, ) => true, _ => false, } @@ -65,8 +61,7 @@ pub(crate) fn format_visibility( match vis.kind { VisibilityKind::Public => Cow::from("pub "), VisibilityKind::Inherited => Cow::from(""), - VisibilityKind::Crate(CrateSugar::PubCrate) => Cow::from("pub(crate) "), - VisibilityKind::Crate(CrateSugar::JustCrate) => Cow::from("crate "), + VisibilityKind::Crate => Cow::from("pub(crate) "), VisibilityKind::Restricted { ref path, .. } => { let Path { ref segments, .. } = **path; let mut segments_iter = segments.iter().map(|seg| rewrite_ident(context, seg.ident)); From 7b987e34c083c81a9e70cb1d387759ac98c752c0 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sat, 21 May 2022 14:45:14 -0400 Subject: [PATCH 3/6] Merge crate and restricted visibilities --- compiler/rustc_ast/src/ast.rs | 1 - compiler/rustc_ast/src/mut_visit.rs | 2 +- .../rustc_ast_pretty/src/pprust/state/item.rs | 3 +-- compiler/rustc_parse/src/parser/mod.rs | 24 +++++-------------- .../rustc_resolve/src/build_reduced_graph.rs | 1 - .../clippy/clippy_utils/src/ast_utils.rs | 2 +- src/tools/rustfmt/src/items.rs | 2 +- src/tools/rustfmt/src/utils.rs | 9 ++----- 8 files changed, 12 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 534b56a998175..2c047fbc53945 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2576,7 +2576,6 @@ pub struct Visibility { #[derive(Clone, Encodable, Decodable, Debug)] pub enum VisibilityKind { Public, - Crate, Restricted { path: P, id: NodeId }, Inherited, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index d7d112987cb71..85bb52964865b 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1469,7 +1469,7 @@ pub fn noop_flat_map_stmt_kind( pub fn noop_visit_vis(visibility: &mut Visibility, vis: &mut T) { match &mut visibility.kind { - VisibilityKind::Public | VisibilityKind::Crate | VisibilityKind::Inherited => {} + VisibilityKind::Public | VisibilityKind::Inherited => {} VisibilityKind::Restricted { path, id } => { vis.visit_path(path); vis.visit_id(id); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 7151a9f693898..67b539a7ad41b 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -403,10 +403,9 @@ impl<'a> State<'a> { pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { match vis.kind { ast::VisibilityKind::Public => self.word_nbsp("pub"), - ast::VisibilityKind::Crate => self.word_nbsp("pub(crate)"), ast::VisibilityKind::Restricted { ref path, .. } => { let path = Self::to_string(|s| s.print_path(path, false, 0)); - if path == "self" || path == "super" { + if path == "crate" || path == "self" || path == "super" { self.word_nbsp(format!("pub({})", path)) } else { self.word_nbsp(format!("pub(in {})", path)) diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index ebe3d00cf73b1..6e6c1ffe74737 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -1245,8 +1245,8 @@ impl<'a> Parser<'a> { res } - /// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `pub(self)` for `pub(in self)` - /// and `pub(super)` for `pub(in super)`. + /// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)` + /// for `pub(in self)` and `pub(super)` for `pub(in super)`. /// If the following element can't be a tuple (i.e., it's a function definition), then /// it's not a tuple struct field), and the contents within the parentheses aren't valid, /// so emit a proper diagnostic. @@ -1271,19 +1271,7 @@ impl<'a> Parser<'a> { // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`. // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so // by the following tokens. - if self.is_keyword_ahead(1, &[kw::Crate]) && self.look_ahead(2, |t| t != &token::ModSep) - // account for `pub(crate::foo)` - { - // Parse `pub(crate)`. - self.bump(); // `(` - self.bump(); // `crate` - self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)` - return Ok(Visibility { - span: lo.to(self.prev_token.span), - kind: VisibilityKind::Crate, - tokens: None, - }); - } else if self.is_keyword_ahead(1, &[kw::In]) { + if self.is_keyword_ahead(1, &[kw::In]) { // Parse `pub(in path)`. self.bump(); // `(` self.bump(); // `in` @@ -1296,11 +1284,11 @@ impl<'a> Parser<'a> { tokens: None, }); } else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis)) - && self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower]) + && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower]) { - // Parse `pub(self)` or `pub(super)`. + // Parse `pub(crate)`, `pub(self)`, or `pub(super)`. self.bump(); // `(` - let path = self.parse_path(PathStyle::Mod)?; // `super`/`self` + let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self` self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)` let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID }; return Ok(Visibility { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 44d413081cedb..20d9123e411ab 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -249,7 +249,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let parent_scope = &self.parent_scope; match vis.kind { ast::VisibilityKind::Public => Ok(ty::Visibility::Public), - ast::VisibilityKind::Crate => Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())), ast::VisibilityKind::Inherited => { Ok(match self.parent_scope.module.kind { // Any inherited visibility resolved directly inside an enum or trait diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 9aa3793520893..09e8fa4be41fd 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -545,7 +545,7 @@ pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool { pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool { use VisibilityKind::*; match (&l.kind, &r.kind) { - (Public, Public) | (Inherited, Inherited) | (Crate, Crate) => true, + (Public, Public) | (Inherited, Inherited) => true, (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r), _ => false, } diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index ecbd44e197624..8816d7d2f1fe2 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1361,7 +1361,7 @@ pub(crate) fn format_struct_struct( fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos { match vis.kind { - ast::VisibilityKind::Crate | ast::VisibilityKind::Restricted { .. } => vis.span.hi(), + ast::VisibilityKind::Restricted { .. } => vis.span.hi(), _ => default_span.lo(), } } diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index 4a66c168bb95d..58fd95c656e79 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -44,11 +44,7 @@ pub(crate) fn is_same_visibility(a: &Visibility, b: &Visibility) -> bool { VisibilityKind::Restricted { path: q, .. }, ) => pprust::path_to_string(p) == pprust::path_to_string(q), (VisibilityKind::Public, VisibilityKind::Public) - | (VisibilityKind::Inherited, VisibilityKind::Inherited) - | ( - VisibilityKind::Crate, - VisibilityKind::Crate, - ) => true, + | (VisibilityKind::Inherited, VisibilityKind::Inherited) => true, _ => false, } } @@ -61,7 +57,6 @@ pub(crate) fn format_visibility( match vis.kind { VisibilityKind::Public => Cow::from("pub "), VisibilityKind::Inherited => Cow::from(""), - VisibilityKind::Crate => Cow::from("pub(crate) "), VisibilityKind::Restricted { ref path, .. } => { let Path { ref segments, .. } = **path; let mut segments_iter = segments.iter().map(|seg| rewrite_ident(context, seg.ident)); @@ -70,7 +65,7 @@ pub(crate) fn format_visibility( .next() .expect("Non-global path in pub(restricted)?"); } - let is_keyword = |s: &str| s == "self" || s == "super"; + let is_keyword = |s: &str| s == "crate" || s == "self" || s == "super"; let path = segments_iter.collect::>().join("::"); let in_str = if is_keyword(&path) { "" } else { "in " }; From ea9d2fb85cedcc7e32ab3ada3f539ad5cf974b53 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 22 May 2022 16:13:25 +0900 Subject: [PATCH 4/6] Add regression test for #91949 --- .../traits/issue-91949-hangs-on-recursion.rs | 30 +++++++++++++++++++ .../issue-91949-hangs-on-recursion.stderr | 25 ++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/test/ui/traits/issue-91949-hangs-on-recursion.rs create mode 100644 src/test/ui/traits/issue-91949-hangs-on-recursion.stderr diff --git a/src/test/ui/traits/issue-91949-hangs-on-recursion.rs b/src/test/ui/traits/issue-91949-hangs-on-recursion.rs new file mode 100644 index 0000000000000..cf2218fe52292 --- /dev/null +++ b/src/test/ui/traits/issue-91949-hangs-on-recursion.rs @@ -0,0 +1,30 @@ +// build-fail +// error-pattern: overflow evaluating the requirement `(): Sized` +// error-pattern: function cannot return without recursing + +// Regression test for #91949. +// This hanged *forever* on 1.56, fixed by #90423. + +#![recursion_limit = "256"] + +struct Wrapped(T); + +struct IteratorOfWrapped>(I); + +impl> Iterator for IteratorOfWrapped { + type Item = Wrapped; + fn next(&mut self) -> Option> { + self.0.next().map(Wrapped) + } +} + +fn recurse(elements: T) -> Vec +where + T: Iterator, +{ + recurse(IteratorOfWrapped(elements).map(|t| t.0)) +} + +fn main() { + recurse(std::iter::empty()); +} diff --git a/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr b/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr new file mode 100644 index 0000000000000..6c04616344f54 --- /dev/null +++ b/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr @@ -0,0 +1,25 @@ +warning: function cannot return without recursing + --> $DIR/issue-91949-hangs-on-recursion.rs:21:1 + | +LL | / fn recurse(elements: T) -> Vec +LL | | where +LL | | T: Iterator, +LL | | { +LL | | recurse(IteratorOfWrapped(elements).map(|t| t.0)) + | | ------------------------------------------------- recursive call site +LL | | } + | |_^ cannot return without recursing + | + = note: `#[warn(unconditional_recursion)]` on by default + = help: a `loop` may express intention better if this is on purpose + +error[E0275]: overflow evaluating the requirement `(): Sized` + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "512"]` attribute to your crate (`issue_91949_hangs_on_recursion`) + = note: required because of the requirements on the impl of `Iterator` for `std::iter::Empty<()>` + = note: 171 redundant requirements hidden + = note: required because of the requirements on the impl of `Iterator` for `IteratorOfWrapped<(), Map>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:25:45: 25:52]>>` + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0275`. From 526a665e96017bb630f4cdfd83582939d9374240 Mon Sep 17 00:00:00 2001 From: jersou Date: Mon, 23 May 2022 00:02:09 +0200 Subject: [PATCH 5/6] std::time : fix doc variable name --- library/std/src/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/time.rs b/library/std/src/time.rs index 708e4064e06f3..b2014f462bd34 100644 --- a/library/std/src/time.rs +++ b/library/std/src/time.rs @@ -95,8 +95,8 @@ pub use core::time::FromFloatSecsError; /// use std::time::{Instant, Duration}; /// /// let now = Instant::now(); -/// let max_nanoseconds = u64::MAX / 1_000_000_000; -/// let duration = Duration::new(max_nanoseconds, 0); +/// let max_seconds = u64::MAX / 1_000_000_000; +/// let duration = Duration::new(max_seconds, 0); /// println!("{:?}", now + duration); /// ``` /// From 21a7b4cb97ce1992645ac04efef1c06172d82a61 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 22 May 2022 22:06:27 -0700 Subject: [PATCH 6/6] Fix some typos in arg checking algorithm --- .../rustc_typeck/src/check/fn_ctxt/checks.rs | 10 +++++----- .../ui/argument-suggestions/issue-97197.rs | 6 ++++++ .../argument-suggestions/issue-97197.stderr | 19 +++++++++++++++++++ .../missing_arguments.stderr | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/argument-suggestions/issue-97197.rs create mode 100644 src/test/ui/argument-suggestions/issue-97197.stderr diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index 54003654db010..dff0a225871a4 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -768,7 +768,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let second_input_ty = self.resolve_vars_if_possible(expected_input_tys[second_idx]); let third_input_ty = - self.resolve_vars_if_possible(expected_input_tys[second_idx]); + self.resolve_vars_if_possible(expected_input_tys[third_idx]); let span = if third_idx < provided_arg_count { let first_arg_span = provided_args[first_idx].span; let third_arg_span = provided_args[third_idx].span; @@ -809,16 +809,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } missing_idxs => { let first_idx = *missing_idxs.first().unwrap(); - let second_idx = *missing_idxs.last().unwrap(); + let last_idx = *missing_idxs.last().unwrap(); // NOTE: Because we might be re-arranging arguments, might have extra arguments, etc. // It's hard to *really* know where we should provide this error label, so this is a // decent heuristic - let span = if first_idx < provided_arg_count { + let span = if last_idx < provided_arg_count { let first_arg_span = provided_args[first_idx].span; - let second_arg_span = provided_args[second_idx].span; + let last_arg_span = provided_args[last_idx].span; Span::new( first_arg_span.lo(), - second_arg_span.hi(), + last_arg_span.hi(), first_arg_span.ctxt(), None, ) diff --git a/src/test/ui/argument-suggestions/issue-97197.rs b/src/test/ui/argument-suggestions/issue-97197.rs new file mode 100644 index 0000000000000..6f9f4293e4964 --- /dev/null +++ b/src/test/ui/argument-suggestions/issue-97197.rs @@ -0,0 +1,6 @@ +fn main() { + g((), ()); + //~^ ERROR this function takes 6 arguments but 2 arguments were supplied +} + +pub fn g(a1: (), a2: bool, a3: bool, a4: bool, a5: bool, a6: ()) -> () {} diff --git a/src/test/ui/argument-suggestions/issue-97197.stderr b/src/test/ui/argument-suggestions/issue-97197.stderr new file mode 100644 index 0000000000000..10689d5095748 --- /dev/null +++ b/src/test/ui/argument-suggestions/issue-97197.stderr @@ -0,0 +1,19 @@ +error[E0061]: this function takes 6 arguments but 2 arguments were supplied + --> $DIR/issue-97197.rs:2:5 + | +LL | g((), ()); + | ^-------- multiple arguments are missing + | +note: function defined here + --> $DIR/issue-97197.rs:6:8 + | +LL | pub fn g(a1: (), a2: bool, a3: bool, a4: bool, a5: bool, a6: ()) -> () {} + | ^ ------ -------- -------- -------- -------- ------ +help: provide the arguments + | +LL | g((), {bool}, {bool}, {bool}, {bool}, ()); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0061`. diff --git a/src/test/ui/argument-suggestions/missing_arguments.stderr b/src/test/ui/argument-suggestions/missing_arguments.stderr index b4dadb1b9da00..5236d15b94574 100644 --- a/src/test/ui/argument-suggestions/missing_arguments.stderr +++ b/src/test/ui/argument-suggestions/missing_arguments.stderr @@ -293,7 +293,7 @@ error[E0061]: this function takes 5 arguments but 2 arguments were supplied --> $DIR/missing_arguments.rs:39:3 | LL | complex( 1, "" ); - | ^^^^^^^--------------------------------- three arguments of type `f32`, `i32`, and `i32` are missing + | ^^^^^^^--------------------------------- three arguments of type `f32`, `i32`, and `f32` are missing | note: function defined here --> $DIR/missing_arguments.rs:7:4