From 0a227f33a80f02ce0182d52ff1224ace87ad2101 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 17 Jul 2019 18:36:15 +0300 Subject: [PATCH 001/148] stabilize duration_float --- src/libcore/time.rs | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 0f5f91f41a8cd..767ab539877ff 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -505,13 +505,12 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.as_secs_f64(), 2.7); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub const fn as_secs_f64(&self) -> f64 { (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64) @@ -523,13 +522,12 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.as_secs_f32(), 2.7); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub const fn as_secs_f32(&self) -> f32 { (self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32) @@ -543,13 +541,12 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::from_secs_f64(2.7); /// assert_eq!(dur, Duration::new(2, 700_000_000)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn from_secs_f64(secs: f64) -> Duration { const MAX_NANOS_F64: f64 = @@ -579,13 +576,12 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::from_secs_f32(2.7); /// assert_eq!(dur, Duration::new(2, 700_000_000)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn from_secs_f32(secs: f32) -> Duration { const MAX_NANOS_F32: f32 = @@ -614,14 +610,13 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn mul_f64(self, rhs: f64) -> Duration { Duration::from_secs_f64(rhs * self.as_secs_f64()) @@ -634,7 +629,6 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -643,7 +637,7 @@ impl Duration { /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_640)); /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847799, 969_120_256)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn mul_f32(self, rhs: f32) -> Duration { Duration::from_secs_f32(rhs * self.as_secs_f32()) @@ -656,7 +650,6 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -664,7 +657,7 @@ impl Duration { /// // note that truncation is used, not rounding /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn div_f64(self, rhs: f64) -> Duration { Duration::from_secs_f64(self.as_secs_f64() / rhs) @@ -677,7 +670,6 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur = Duration::new(2, 700_000_000); @@ -687,7 +679,7 @@ impl Duration { /// // note that truncation is used, not rounding /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_598)); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn div_f32(self, rhs: f32) -> Duration { Duration::from_secs_f32(self.as_secs_f32() / rhs) @@ -697,14 +689,13 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f64(dur2), 0.5); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn div_duration_f64(self, rhs: Duration) -> f64 { self.as_secs_f64() / rhs.as_secs_f64() @@ -714,14 +705,13 @@ impl Duration { /// /// # Examples /// ``` - /// #![feature(duration_float)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f32(dur2), 0.5); /// ``` - #[unstable(feature = "duration_float", issue = "54361")] + #[stable(feature = "duration_float", since = "1.38.0")] #[inline] pub fn div_duration_f32(self, rhs: Duration) -> f32 { self.as_secs_f32() / rhs.as_secs_f32() From ad3632401c42a6dd8bd7c4d22590b1963e663430 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Wed, 17 Jul 2019 18:58:33 +0300 Subject: [PATCH 002/148] unconstify methods --- src/libcore/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 767ab539877ff..9285dce8af8eb 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -512,7 +512,7 @@ impl Duration { /// ``` #[stable(feature = "duration_float", since = "1.38.0")] #[inline] - pub const fn as_secs_f64(&self) -> f64 { + pub fn as_secs_f64(&self) -> f64 { (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64) } @@ -529,7 +529,7 @@ impl Duration { /// ``` #[stable(feature = "duration_float", since = "1.38.0")] #[inline] - pub const fn as_secs_f32(&self) -> f32 { + pub fn as_secs_f32(&self) -> f32 { (self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32) } From 55ee8fe852d93ce7ac87a5fa36d371cf0f0b195d Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Tue, 30 Jul 2019 15:00:17 +0000 Subject: [PATCH 003/148] mark div_duration methods as unstable, update tracking issue --- src/libcore/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 9285dce8af8eb..6a5ba0afa42a6 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -695,7 +695,7 @@ impl Duration { /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f64(dur2), 0.5); /// ``` - #[stable(feature = "duration_float", since = "1.38.0")] + #[unstable(feature = "div_duration", issue = "63139")] #[inline] pub fn div_duration_f64(self, rhs: Duration) -> f64 { self.as_secs_f64() / rhs.as_secs_f64() @@ -711,7 +711,7 @@ impl Duration { /// let dur2 = Duration::new(5, 400_000_000); /// assert_eq!(dur1.div_duration_f32(dur2), 0.5); /// ``` - #[stable(feature = "duration_float", since = "1.38.0")] + #[unstable(feature = "div_duration", issue = "63139")] #[inline] pub fn div_duration_f32(self, rhs: Duration) -> f32 { self.as_secs_f32() / rhs.as_secs_f32() From 4281e6136df6886462e8da4e45877f6eeecd552a Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Tue, 30 Jul 2019 16:41:03 +0000 Subject: [PATCH 004/148] fix tests --- src/libcore/time.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 6a5ba0afa42a6..5a0e4388e0325 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -689,6 +689,7 @@ impl Duration { /// /// # Examples /// ``` + /// #![feature(div_duration)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); @@ -705,6 +706,7 @@ impl Duration { /// /// # Examples /// ``` + /// #![feature(div_duration)] /// use std::time::Duration; /// /// let dur1 = Duration::new(2, 700_000_000); From 8ae8bc2808bd3b5e125b72a44bfdcd24353e5211 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Mon, 5 Aug 2019 23:06:02 +0100 Subject: [PATCH 005/148] Fix various issues with making items reachable through macros * Allow items to be accessible through private modules and fields when a macro can access them. * Don't mark type-private items as reachable. * Never make items exported/public via macros --- src/librustc_privacy/lib.rs | 190 +++++++++++++++--- .../auxiliary/field-method-macro.rs | 23 +++ .../auxiliary/nested-fn-macro.rs | 11 + .../auxiliary/private-use-macro.rs | 11 + .../ui/definition-reachable/field-method.rs | 11 + src/test/ui/definition-reachable/nested-fn.rs | 11 + .../definition-reachable/private-non-types.rs | 21 ++ .../ui/definition-reachable/private-types.rs | 19 ++ .../ui/definition-reachable/private-use.rs | 10 + 9 files changed, 276 insertions(+), 31 deletions(-) create mode 100644 src/test/ui/definition-reachable/auxiliary/field-method-macro.rs create mode 100644 src/test/ui/definition-reachable/auxiliary/nested-fn-macro.rs create mode 100644 src/test/ui/definition-reachable/auxiliary/private-use-macro.rs create mode 100644 src/test/ui/definition-reachable/field-method.rs create mode 100644 src/test/ui/definition-reachable/nested-fn.rs create mode 100644 src/test/ui/definition-reachable/private-non-types.rs create mode 100644 src/test/ui/definition-reachable/private-types.rs create mode 100644 src/test/ui/definition-reachable/private-use.rs diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index ac18f0e440b78..330370e3803dd 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -229,6 +229,13 @@ fn def_id_visibility<'tcx>( let vis = match tcx.hir().get(hir_id) { Node::Item(item) => &item.vis, Node::ForeignItem(foreign_item) => &foreign_item.vis, + Node::MacroDef(macro_def) => { + if attr::contains_name(¯o_def.attrs, sym::macro_export) { + return (ty::Visibility::Public, macro_def.span, "public"); + } else { + ¯o_def.vis + } + }, Node::TraitItem(..) | Node::Variant(..) => { return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id)); } @@ -433,11 +440,24 @@ impl VisibilityLike for Option { struct EmbargoVisitor<'tcx> { tcx: TyCtxt<'tcx>, - // Accessibility levels for reachable nodes. + /// Accessibility levels for reachable nodes. access_levels: AccessLevels, - // Previous accessibility level; `None` means unreachable. + /// A set of pairs corresponding to modules, where the first module is + /// reachable via a macro that's defined in the second module. This cannot + /// be represented as reachable because it can't handle the following case: + /// + /// pub mod n { // Should be `Public` + /// pub(crate) mod p { // Should *not* be accessible + /// pub fn f() -> i32 { 12 } // Must be `Reachable` + /// } + /// } + /// pub macro m() { + /// n::p::f() + /// } + macro_reachable: FxHashSet<(hir::HirId, DefId)>, + /// Previous accessibility level; `None` means unreachable. prev_level: Option, - // Has something changed in the level map? + /// Has something changed in the level map? changed: bool, } @@ -452,7 +472,7 @@ impl EmbargoVisitor<'tcx> { self.access_levels.map.get(&id).cloned() } - // Updates node level and returns the updated level. + /// Updates node level and returns the updated level. fn update(&mut self, id: hir::HirId, level: Option) -> Option { let old_level = self.get(id); // Accessibility levels can only grow. @@ -477,6 +497,127 @@ impl EmbargoVisitor<'tcx> { } } + /// Updates the item as being reachable through a macro defined in the given + /// module. Returns `true` if the level has changed. + fn update_macro_reachable(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) -> bool { + if self.macro_reachable.insert((reachable_mod, defining_mod)) { + self.update_macro_reachable_mod(reachable_mod, defining_mod); + true + } else { + false + } + } + + fn update_macro_reachable_mod( + &mut self, + reachable_mod: hir::HirId, + defining_mod: DefId, + ) { + let module_def_id = self.tcx.hir().local_def_id(reachable_mod); + let module = self.tcx.hir().get_module(module_def_id).0; + for item_id in &module.item_ids { + let hir_id = item_id.id; + let item_def_id = self.tcx.hir().local_def_id(hir_id); + if let Some(def_kind) = self.tcx.def_kind(item_def_id) { + let item = self.tcx.hir().expect_item(hir_id); + let vis = ty::Visibility::from_hir(&item.vis, hir_id, self.tcx); + self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); + } + } + + if let Some(exports) = self.tcx.module_exports(module_def_id) { + for export in exports { + if export.vis.is_accessible_from(defining_mod, self.tcx) { + if let Res::Def(def_kind, def_id) = export.res { + let vis = def_id_visibility(self.tcx, def_id).0; + if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + self.update_macro_reachable_def( + hir_id, + def_kind, + vis, + defining_mod, + ); + } + } + } + } + } + } + + fn update_macro_reachable_def( + &mut self, + hir_id: hir::HirId, + def_kind: DefKind, + vis: ty::Visibility, + module: DefId, + ) { + let level = Some(AccessLevel::Reachable); + if let ty::Visibility::Public = vis { + self.update(hir_id, level); + } + match def_kind { + // No type privacy, so can be directly marked as reachable. + DefKind::Const + | DefKind::Macro(_) + | DefKind::Static + | DefKind::TraitAlias + | DefKind::TyAlias => { + if vis.is_accessible_from(module, self.tcx) { + self.update(hir_id, level); + } + }, + + // We can't use a module name as the final segment of a path, except + // in use statements. Since re-export checking doesn't consider + // hygiene these don't need to be marked reachable. The contents of + // the module, however may be reachable. + DefKind::Mod => { + if vis.is_accessible_from(module, self.tcx) { + self.update_macro_reachable(hir_id, module); + } + } + + DefKind::Struct | DefKind::Union => { + // While structs and unions have type privacy, their fields do + // not. + if let ty::Visibility::Public = vis { + let item = self.tcx.hir().expect_item(hir_id); + if let hir::ItemKind::Struct(ref struct_def, _) + | hir::ItemKind::Union(ref struct_def, _) = item.node + { + for field in struct_def.fields() { + let field_vis = ty::Visibility::from_hir( + &field.vis, + field.hir_id, + self.tcx, + ); + if field_vis.is_accessible_from(module, self.tcx) { + self.reach(field.hir_id, level).ty(); + } + } + } else { + bug!("item {:?} with DefKind {:?}", item, def_kind); + } + } + } + + // These have type privacy, so are not reachable unless they're + // public + DefKind::AssocConst + | DefKind::AssocTy + | DefKind::AssocOpaqueTy + | DefKind::ConstParam + | DefKind::Ctor(_, _) + | DefKind::Enum + | DefKind::ForeignTy + | DefKind::Fn + | DefKind::OpaqueTy + | DefKind::Method + | DefKind::Trait + | DefKind::TyParam + | DefKind::Variant => (), + } + } /// Given the path segments of a `ItemKind::Use`, then we need /// to update the visibility of the intermediate use so that it isn't linted @@ -746,40 +887,21 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { return } - let module_did = ty::DefIdTree::parent( + let macro_module_def_id = ty::DefIdTree::parent( self.tcx, self.tcx.hir().local_def_id(md.hir_id) ).unwrap(); - let mut module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap(); + let mut module_id = self.tcx.hir().as_local_hir_id(macro_module_def_id).unwrap(); let level = if md.vis.node.is_pub() { self.get(module_id) } else { None }; - let level = self.update(md.hir_id, level); - if level.is_none() { + let new_level = self.update(md.hir_id, level); + if new_level.is_none() { return } loop { - let module = if module_id == hir::CRATE_HIR_ID { - &self.tcx.hir().krate().module - } else if let hir::ItemKind::Mod(ref module) = - self.tcx.hir().expect_item(module_id).node { - module - } else { - unreachable!() - }; - for id in &module.item_ids { - self.update(id.id, level); - } - let def_id = self.tcx.hir().local_def_id(module_id); - if let Some(exports) = self.tcx.module_exports(def_id) { - for export in exports.iter() { - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(export.res.def_id()) { - self.update(hir_id, level); - } - } - } - - if module_id == hir::CRATE_HIR_ID { - break + let changed_reachability = self.update_macro_reachable(module_id, macro_module_def_id); + if changed_reachability || module_id == hir::CRATE_HIR_ID { + break; } module_id = self.tcx.hir().get_parent_node(module_id); } @@ -826,7 +948,12 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { fn tcx(&self) -> TyCtxt<'tcx> { self.ev.tcx } fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool { if let Some(hir_id) = self.ev.tcx.hir().as_local_hir_id(def_id) { - self.ev.update(hir_id, self.access_level); + if let ((ty::Visibility::Public, ..), _) + | (_, Some(AccessLevel::ReachableFromImplTrait)) + = (def_id_visibility(self.tcx(), def_id), self.access_level) + { + self.ev.update(hir_id, self.access_level); + } } false } @@ -1860,6 +1987,7 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, krate: CrateNum) -> &AccessLevels { let mut visitor = EmbargoVisitor { tcx, access_levels: Default::default(), + macro_reachable: Default::default(), prev_level: Some(AccessLevel::Public), changed: false, }; diff --git a/src/test/ui/definition-reachable/auxiliary/field-method-macro.rs b/src/test/ui/definition-reachable/auxiliary/field-method-macro.rs new file mode 100644 index 0000000000000..30ba70bdfeb66 --- /dev/null +++ b/src/test/ui/definition-reachable/auxiliary/field-method-macro.rs @@ -0,0 +1,23 @@ +#![feature(decl_macro)] + +mod n { + pub struct B(pub(crate) p::C); + impl B { + pub fn new() -> Self { + B(p::C) + } + } + mod p { + pub struct C; + + impl C { + pub fn foo(&self) -> i32 { + 33 + } + } + } +} + +pub macro m() { + n::B::new().0.foo() +} diff --git a/src/test/ui/definition-reachable/auxiliary/nested-fn-macro.rs b/src/test/ui/definition-reachable/auxiliary/nested-fn-macro.rs new file mode 100644 index 0000000000000..a39e8c986c391 --- /dev/null +++ b/src/test/ui/definition-reachable/auxiliary/nested-fn-macro.rs @@ -0,0 +1,11 @@ +#![feature(decl_macro)] + +mod n { + pub(crate) mod p { + pub fn f() -> i32 { 12 } + } +} + +pub macro m() { + n::p::f() +} diff --git a/src/test/ui/definition-reachable/auxiliary/private-use-macro.rs b/src/test/ui/definition-reachable/auxiliary/private-use-macro.rs new file mode 100644 index 0000000000000..4f283d9c19c04 --- /dev/null +++ b/src/test/ui/definition-reachable/auxiliary/private-use-macro.rs @@ -0,0 +1,11 @@ +#![feature(decl_macro)] + +mod n { + pub static S: i32 = 57; +} + +use n::S; + +pub macro m() { + S +} diff --git a/src/test/ui/definition-reachable/field-method.rs b/src/test/ui/definition-reachable/field-method.rs new file mode 100644 index 0000000000000..60e895a2f9a07 --- /dev/null +++ b/src/test/ui/definition-reachable/field-method.rs @@ -0,0 +1,11 @@ +// Check that functions accessible through a field visible to a macro are +// considered reachable + +// aux-build:nested-fn-macro.rs +// run-pass + +extern crate nested_fn_macro; + +fn main() { + assert_eq!(nested_fn_macro::m!(), 12); +} diff --git a/src/test/ui/definition-reachable/nested-fn.rs b/src/test/ui/definition-reachable/nested-fn.rs new file mode 100644 index 0000000000000..b596ba8936a43 --- /dev/null +++ b/src/test/ui/definition-reachable/nested-fn.rs @@ -0,0 +1,11 @@ +// Check that functions visible to macros through paths with >2 segements are +// considered reachable + +// aux-build:field-method-macro.rs +// run-pass + +extern crate field_method_macro; + +fn main() { + assert_eq!(field_method_macro::m!(), 33); +} diff --git a/src/test/ui/definition-reachable/private-non-types.rs b/src/test/ui/definition-reachable/private-non-types.rs new file mode 100644 index 0000000000000..a601dabcb0b3f --- /dev/null +++ b/src/test/ui/definition-reachable/private-non-types.rs @@ -0,0 +1,21 @@ +// Check that we don't require stability annotations for private modules, +// imports and fields that are accessible to opaque macros. + +// check-pass + +#![feature(decl_macro, staged_api)] +#![stable(feature = "test", since = "1.0.0")] + +extern crate std as local_std; +use local_std::marker::Copy as LocalCopy; +mod private_mod { + #[stable(feature = "test", since = "1.0.0")] + pub struct A { + pub(crate) f: i32, + } +} + +#[stable(feature = "test", since = "1.0.0")] +pub macro m() {} + +fn main() {} diff --git a/src/test/ui/definition-reachable/private-types.rs b/src/test/ui/definition-reachable/private-types.rs new file mode 100644 index 0000000000000..02c1224f4e142 --- /dev/null +++ b/src/test/ui/definition-reachable/private-types.rs @@ -0,0 +1,19 @@ +// Check that type privacy is taken into account when considering reachability + +// check-pass + +#![feature(decl_macro, staged_api)] +#![stable(feature = "test", since = "1.0.0")] + +// Type privacy should prevent use of these in other crates, so we shouldn't +// need a stability annotation. +fn private_function() {} +struct PrivateStruct { f: () } +enum PrivateEnum { V } +union PrivateUnion { g: () } +trait PrivateTrait {} + +#[stable(feature = "test", since = "1.0.0")] +pub macro m() {} + +fn main() {} diff --git a/src/test/ui/definition-reachable/private-use.rs b/src/test/ui/definition-reachable/private-use.rs new file mode 100644 index 0000000000000..02cff0475e586 --- /dev/null +++ b/src/test/ui/definition-reachable/private-use.rs @@ -0,0 +1,10 @@ +// Check that private use statements can be used by + +// run-pass +// aux-build:private-use-macro.rs + +extern crate private_use_macro; + +fn main() { + assert_eq!(private_use_macro::m!(), 57); +} From 7b41fd215893c06110c7f650be47efed3910d90b Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Mon, 5 Aug 2019 23:06:40 +0100 Subject: [PATCH 006/148] Make some items in core::unicode private They were reachable through opaque macros defined in `core` --- src/libcore/unicode/tables.rs | 32 ++++++++++++++++---------------- src/libcore/unicode/unicode.py | 8 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libcore/unicode/tables.rs b/src/libcore/unicode/tables.rs index bfe784afaa47d..3fae3a46ada6b 100644 --- a/src/libcore/unicode/tables.rs +++ b/src/libcore/unicode/tables.rs @@ -14,8 +14,8 @@ pub const UNICODE_VERSION: UnicodeVersion = UnicodeVersion { micro: 0, _priv: (), }; -pub mod general_category { - pub const Cc_table: &super::SmallBoolTrie = &super::SmallBoolTrie { +pub(crate) mod general_category { + const Cc_table: &super::SmallBoolTrie = &super::SmallBoolTrie { r1: &[ 0, 1, 0 ], @@ -28,7 +28,7 @@ pub mod general_category { Cc_table.lookup(c) } - pub const N_table: &super::BoolTrie = &super::BoolTrie { + const N_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x03ff000000000000, 0x0000000000000000, 0x720c000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, @@ -141,8 +141,8 @@ pub mod general_category { } -pub mod derived_property { - pub const Alphabetic_table: &super::BoolTrie = &super::BoolTrie { +pub(crate) mod derived_property { + const Alphabetic_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x07fffffe07fffffe, 0x0420040000000000, 0xff7fffffff7fffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, @@ -327,7 +327,7 @@ pub mod derived_property { Alphabetic_table.lookup(c) } - pub const Case_Ignorable_table: &super::BoolTrie = &super::BoolTrie { + const Case_Ignorable_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0400408000000000, 0x0000000140000000, 0x0190a10000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, @@ -464,7 +464,7 @@ pub mod derived_property { Case_Ignorable_table.lookup(c) } - pub const Cased_table: &super::BoolTrie = &super::BoolTrie { + const Cased_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x07fffffe07fffffe, 0x0420040000000000, 0xff7fffffff7fffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xf7ffffffffffffff, 0xfffffffffffffff0, @@ -565,7 +565,7 @@ pub mod derived_property { Cased_table.lookup(c) } - pub const Grapheme_Extend_table: &super::BoolTrie = &super::BoolTrie { + const Grapheme_Extend_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, @@ -689,7 +689,7 @@ pub mod derived_property { Grapheme_Extend_table.lookup(c) } - pub const Lowercase_table: &super::BoolTrie = &super::BoolTrie { + const Lowercase_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x07fffffe00000000, 0x0420040000000000, 0xff7fffff80000000, 0x55aaaaaaaaaaaaaa, 0xd4aaaaaaaaaaab55, 0xe6512d2a4e243129, 0xaa29aaaab5555240, @@ -789,7 +789,7 @@ pub mod derived_property { Lowercase_table.lookup(c) } - pub const Uppercase_table: &super::BoolTrie = &super::BoolTrie { + const Uppercase_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x0000000007fffffe, 0x0000000000000000, 0x000000007f7fffff, 0xaa55555555555555, 0x2b555555555554aa, 0x11aed2d5b1dbced6, 0x55d255554aaaa490, @@ -890,7 +890,7 @@ pub mod derived_property { Uppercase_table.lookup(c) } - pub const XID_Continue_table: &super::BoolTrie = &super::BoolTrie { + const XID_Continue_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x03ff000000000000, 0x07fffffe87fffffe, 0x04a0040000000000, 0xff7fffffff7fffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, @@ -1068,7 +1068,7 @@ pub mod derived_property { XID_Continue_table.lookup(c) } - pub const XID_Start_table: &super::BoolTrie = &super::BoolTrie { + const XID_Start_table: &super::BoolTrie = &super::BoolTrie { r1: [ 0x0000000000000000, 0x07fffffe07fffffe, 0x0420040000000000, 0xff7fffffff7fffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, @@ -1250,8 +1250,8 @@ pub mod derived_property { } -pub mod property { - pub const Pattern_White_Space_table: &super::SmallBoolTrie = &super::SmallBoolTrie { +pub(crate) mod property { + const Pattern_White_Space_table: &super::SmallBoolTrie = &super::SmallBoolTrie { r1: &[ 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -1268,7 +1268,7 @@ pub mod property { Pattern_White_Space_table.lookup(c) } - pub const White_Space_table: &super::SmallBoolTrie = &super::SmallBoolTrie { + const White_Space_table: &super::SmallBoolTrie = &super::SmallBoolTrie { r1: &[ 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -1290,7 +1290,7 @@ pub mod property { } -pub mod conversions { +pub(crate) mod conversions { pub fn to_lower(c: char) -> [char; 3] { match bsearch_case_table(c, to_lowercase_table) { None => [c, '\0', '\0'], diff --git a/src/libcore/unicode/unicode.py b/src/libcore/unicode/unicode.py index 5389d1cf80383..6de5d9e033b93 100755 --- a/src/libcore/unicode/unicode.py +++ b/src/libcore/unicode/unicode.py @@ -606,7 +606,7 @@ def compute_trie(raw_data, chunk_size): return root, child_data -def generate_bool_trie(name, codepoint_ranges, is_pub=True): +def generate_bool_trie(name, codepoint_ranges, is_pub=False): # type: (str, List[Tuple[int, int]], bool) -> Iterator[str] """ Generate Rust code for BoolTrie struct. @@ -681,7 +681,7 @@ def generate_bool_trie(name, codepoint_ranges, is_pub=True): yield " };\n\n" -def generate_small_bool_trie(name, codepoint_ranges, is_pub=True): +def generate_small_bool_trie(name, codepoint_ranges, is_pub=False): # type: (str, List[Tuple[int, int]], bool) -> Iterator[str] """ Generate Rust code for `SmallBoolTrie` struct. @@ -726,7 +726,7 @@ def generate_property_module(mod, grouped_categories, category_subset): Generate Rust code for module defining properties. """ - yield "pub mod %s {\n" % mod + yield "pub(crate) mod %s {\n" % mod for cat in sorted(category_subset): if cat in ("Cc", "White_Space", "Pattern_White_Space"): generator = generate_small_bool_trie("%s_table" % cat, grouped_categories[cat]) @@ -749,7 +749,7 @@ def generate_conversions_module(unicode_data): Generate Rust code for module defining conversions. """ - yield "pub mod conversions {" + yield "pub(crate) mod conversions {" yield """ pub fn to_lower(c: char) -> [char; 3] { match bsearch_case_table(c, to_lowercase_table) { From d9d9246418ae884cb67feb3574832696660b8e2e Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Mon, 5 Aug 2019 23:10:32 +0100 Subject: [PATCH 007/148] Remove gensym from format_args --- src/libcore/macros.rs | 2 -- src/libsyntax_ext/format.rs | 4 ++-- src/test/ui/format-hygiene.rs | 8 -------- src/test/ui/hygiene/format-args.rs | 12 ++++++++++++ 4 files changed, 14 insertions(+), 12 deletions(-) delete mode 100644 src/test/ui/format-hygiene.rs create mode 100644 src/test/ui/hygiene/format-args.rs diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 37cc71bff62b4..6a2c3bff4ddca 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -767,7 +767,6 @@ pub(crate) mod builtin { #[stable(feature = "rust1", since = "1.0.0")] #[allow_internal_unstable(fmt_internals)] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] pub macro format_args { ($fmt:expr) => ({ /* compiler built-in */ }), ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) @@ -779,7 +778,6 @@ pub(crate) mod builtin { language use and is subject to change")] #[allow_internal_unstable(fmt_internals)] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] pub macro format_args_nl { ($fmt:expr) => ({ /* compiler built-in */ }), ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index fe9cad1e32fca..1dfcc14021c3b 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -646,7 +646,7 @@ impl<'a, 'b> Context<'a, 'b> { let mut heads = Vec::with_capacity(self.args.len()); let names_pos: Vec<_> = (0..self.args.len()) - .map(|i| self.ecx.ident_of(&format!("arg{}", i)).gensym()) + .map(|i| ast::Ident::from_str_and_span(&format!("arg{}", i), self.macsp)) .collect(); // First, build up the static array which will become our precompiled @@ -843,7 +843,7 @@ pub fn expand_preparsed_format_args( let arg_unique_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect(); let mut macsp = ecx.call_site(); - macsp = macsp.apply_mark(ecx.current_expansion.id); + macsp = macsp.with_ctxt(ecx.backtrace()); let msg = "format argument must be a string literal"; let fmt_sp = efmt.span; diff --git a/src/test/ui/format-hygiene.rs b/src/test/ui/format-hygiene.rs deleted file mode 100644 index 6bf5ae8beaddb..0000000000000 --- a/src/test/ui/format-hygiene.rs +++ /dev/null @@ -1,8 +0,0 @@ -// run-pass - -#![allow(non_upper_case_globals)] -pub const arg0: u8 = 1; - -pub fn main() { - format!("{}", 1); -} diff --git a/src/test/ui/hygiene/format-args.rs b/src/test/ui/hygiene/format-args.rs new file mode 100644 index 0000000000000..d74889b95cc12 --- /dev/null +++ b/src/test/ui/hygiene/format-args.rs @@ -0,0 +1,12 @@ +// check-pass + +#![allow(non_upper_case_globals)] +#![feature(format_args_nl)] + +static arg0: () = (); + +fn main() { + static arg1: () = (); + format_args!("{} {:?}", 0, 1); + format_args_nl!("{} {:?}", 0, 1); +} From ffa4d7e87f7565177693eefb4650bb32c3498968 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Wed, 7 Aug 2019 09:30:16 -0700 Subject: [PATCH 008/148] Sort the fat LTO modules to produce deterministic output. --- src/librustc_codegen_ssa/back/write.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index c9e4663fdbddf..2bd46b9f6efee 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -755,6 +755,15 @@ pub enum FatLTOInput { InMemory(ModuleCodegen), } +impl FatLTOInput { + fn name(&'a self) -> &'a String { + match self { + FatLTOInput::Serialized { name, buffer: _ } => &name, + FatLTOInput::InMemory(module) => &module.name, + } + } +} + fn execute_work_item( cgcx: &CodegenContext, work_item: WorkItem, @@ -1345,10 +1354,15 @@ fn start_executing_work( assert!(!started_lto); started_lto = true; - let needs_fat_lto = mem::take(&mut needs_fat_lto); + let mut needs_fat_lto: Vec> = mem::take(&mut needs_fat_lto); let needs_thin_lto = mem::take(&mut needs_thin_lto); let import_only_modules = mem::take(&mut lto_import_only_modules); + // Regardless of what order these modules completed in, report them to + // the backend in the same order every time to ensure that we're handing + // out deterministic results. + needs_fat_lto.sort_by(|m1, m2| m1.name().cmp(m2.name())); + for (work, cost) in generate_lto_work(&cgcx, needs_fat_lto, needs_thin_lto, import_only_modules) { let insertion_index = work_items From 5b2c5e181ac321f04621ef0b7dc78354bf3397d3 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 8 Aug 2019 07:54:27 -0700 Subject: [PATCH 009/148] Sort fat LTO modules later and add a test. --- src/librustc_codegen_llvm/back/lto.rs | 6 ++++-- src/librustc_codegen_ssa/back/write.rs | 16 +--------------- .../reproducible-build/Makefile | 11 ++++++++++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index 5d3cc0c0a255f..33b4c8eec54ce 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -265,7 +265,7 @@ fn fat_lto(cgcx: &CodegenContext, // and we want to move everything to the same LLVM context. Currently the // way we know of to do that is to serialize them to a string and them parse // them later. Not great but hey, that's why it's "fat" LTO, right? - serialized_modules.extend(modules.into_iter().map(|module| { + let mut new_modules = modules.into_iter().map(|module| { match module { FatLTOInput::InMemory(module) => { let buffer = ModuleBuffer::new(module.module_llvm.llmod()); @@ -277,7 +277,9 @@ fn fat_lto(cgcx: &CodegenContext, (SerializedModule::Local(buffer), llmod_id) } } - })); + }).collect::>(); + new_modules.sort_by(|module1, module2| module1.1.partial_cmp(&module2.1).unwrap()); + serialized_modules.extend(new_modules); serialized_modules.extend(cached_modules.into_iter().map(|(buffer, wp)| { (buffer, CString::new(wp.cgu_name).unwrap()) })); diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index 2bd46b9f6efee..c9e4663fdbddf 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -755,15 +755,6 @@ pub enum FatLTOInput { InMemory(ModuleCodegen), } -impl FatLTOInput { - fn name(&'a self) -> &'a String { - match self { - FatLTOInput::Serialized { name, buffer: _ } => &name, - FatLTOInput::InMemory(module) => &module.name, - } - } -} - fn execute_work_item( cgcx: &CodegenContext, work_item: WorkItem, @@ -1354,15 +1345,10 @@ fn start_executing_work( assert!(!started_lto); started_lto = true; - let mut needs_fat_lto: Vec> = mem::take(&mut needs_fat_lto); + let needs_fat_lto = mem::take(&mut needs_fat_lto); let needs_thin_lto = mem::take(&mut needs_thin_lto); let import_only_modules = mem::take(&mut lto_import_only_modules); - // Regardless of what order these modules completed in, report them to - // the backend in the same order every time to ensure that we're handing - // out deterministic results. - needs_fat_lto.sort_by(|m1, m2| m1.name().cmp(m2.name())); - for (work, cost) in generate_lto_work(&cgcx, needs_fat_lto, needs_thin_lto, import_only_modules) { let insertion_index = work_items diff --git a/src/test/run-make-fulldeps/reproducible-build/Makefile b/src/test/run-make-fulldeps/reproducible-build/Makefile index a17ec212cfd58..5b9c9d3d03521 100644 --- a/src/test/run-make-fulldeps/reproducible-build/Makefile +++ b/src/test/run-make-fulldeps/reproducible-build/Makefile @@ -10,7 +10,8 @@ all: \ link_paths \ remap_paths \ different_source_dirs \ - extern_flags + extern_flags \ + fat_lto smoke: rm -rf $(TMPDIR) && mkdir $(TMPDIR) @@ -76,3 +77,11 @@ extern_flags: --extern reproducible_build_aux=$(TMPDIR)/libbar.rlib \ --crate-type rlib cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1 + +fat_lto: + rm -rf $(TMPDIR) && mkdir $(TMPDIR) + $(RUSTC) reproducible-build-aux.rs + $(RUSTC) reproducible-build.rs -C lto=fat + cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a + $(RUSTC) reproducible-build.rs -C lto=fat + cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1 From a46e36f9df68f9c59157ccc3c1519413802993b5 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 8 Aug 2019 09:05:32 -0700 Subject: [PATCH 010/148] Fix fat LTO determinism test so it fails without the fix. --- src/test/run-make-fulldeps/reproducible-build/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/run-make-fulldeps/reproducible-build/Makefile b/src/test/run-make-fulldeps/reproducible-build/Makefile index 5b9c9d3d03521..addbf9928bf4a 100644 --- a/src/test/run-make-fulldeps/reproducible-build/Makefile +++ b/src/test/run-make-fulldeps/reproducible-build/Makefile @@ -81,7 +81,7 @@ extern_flags: fat_lto: rm -rf $(TMPDIR) && mkdir $(TMPDIR) $(RUSTC) reproducible-build-aux.rs - $(RUSTC) reproducible-build.rs -C lto=fat + $(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1 cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a - $(RUSTC) reproducible-build.rs -C lto=fat + $(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1 cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1 From 3e6a9273057aec611b9f22025bb28355060af265 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 8 Aug 2019 10:51:52 -0700 Subject: [PATCH 011/148] Explain why we're sorting the modules. --- src/librustc_codegen_llvm/back/lto.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index 33b4c8eec54ce..5ed08943fe6fd 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -278,6 +278,7 @@ fn fat_lto(cgcx: &CodegenContext, } } }).collect::>(); + // Sort the modules to ensure we produce deterministic results. new_modules.sort_by(|module1, module2| module1.1.partial_cmp(&module2.1).unwrap()); serialized_modules.extend(new_modules); serialized_modules.extend(cached_modules.into_iter().map(|(buffer, wp)| { From 3a6a29b4ecfdb7f641ca699fcd66a09b4baaae6a Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Wed, 31 Jul 2019 21:00:35 +0200 Subject: [PATCH 012/148] Use associated_type_bounds where applicable - closes #61738 --- src/liballoc/borrow.rs | 12 +- src/liballoc/lib.rs | 1 + src/liballoc/tests/str.rs | 10 +- src/libcore/convert.rs | 4 +- src/libcore/future/future.rs | 3 +- src/libcore/iter/adapters/flatten.rs | 61 +++++---- src/libcore/iter/traits/collect.rs | 4 +- src/libcore/lib.rs | 1 + src/libcore/pin.rs | 10 +- src/libcore/str/mod.rs | 116 +++++++++++------- src/librustc/lib.rs | 1 + src/librustc/traits/select.rs | 2 +- src/librustc/traits/structural_impls.rs | 6 +- src/librustc/ty/context.rs | 4 +- src/librustc/ty/layout.rs | 6 +- src/librustc/ty/query/on_disk_cache.rs | 3 +- src/librustc_codegen_ssa/back/command.rs | 4 +- src/librustc_codegen_ssa/lib.rs | 1 + src/librustc_data_structures/lib.rs | 1 + .../owning_ref/mod.rs | 8 +- src/librustc_mir/dataflow/mod.rs | 11 +- src/librustc_mir/lib.rs | 1 + src/libserialize/collection_impls.rs | 10 +- src/libserialize/lib.rs | 1 + src/libstd/sys/sgx/abi/usercalls/alloc.rs | 12 +- 25 files changed, 169 insertions(+), 124 deletions(-) diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index d5e15b3719c2e..a9c5bce4c25fc 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -329,8 +329,8 @@ impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Cow<'_, B> - where B: fmt::Debug + ToOwned, - ::Owned: fmt::Debug +where + B: fmt::Debug + ToOwned, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { @@ -342,8 +342,8 @@ impl fmt::Debug for Cow<'_, B> #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for Cow<'_, B> - where B: fmt::Display + ToOwned, - ::Owned: fmt::Display +where + B: fmt::Display + ToOwned, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { @@ -355,8 +355,8 @@ impl fmt::Display for Cow<'_, B> #[stable(feature = "default", since = "1.11.0")] impl Default for Cow<'_, B> - where B: ToOwned, - ::Owned: Default +where + B: ToOwned, { /// Creates an owned Cow<'a, B> with the default value for the contained owned value. fn default() -> Self { diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index deea74daa52d5..a1936b36ac6bf 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -122,6 +122,7 @@ #![feature(alloc_layout_extra)] #![feature(try_trait)] #![feature(mem_take)] +#![feature(associated_type_bounds)] // Allow testing this library diff --git a/src/liballoc/tests/str.rs b/src/liballoc/tests/str.rs index c5198ca39fedf..4332b2e90fdaf 100644 --- a/src/liballoc/tests/str.rs +++ b/src/liballoc/tests/str.rs @@ -1638,10 +1638,12 @@ mod pattern { } } - fn cmp_search_to_vec<'a, P: Pattern<'a>>(rev: bool, pat: P, haystack: &'a str, - right: Vec) - where P::Searcher: ReverseSearcher<'a> - { + fn cmp_search_to_vec<'a>( + rev: bool, + pat: impl Pattern<'a, Searcher: ReverseSearcher<'a>>, + haystack: &'a str, + right: Vec + ) { let mut searcher = pat.into_searcher(haystack); let mut v = vec![]; loop { diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 624b13d96472c..641621f492baf 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -513,7 +513,7 @@ impl AsRef for &mut T where T: AsRef // FIXME (#45742): replace the above impls for &/&mut with the following more general one: // // As lifts over Deref -// impl AsRef for D where D::Target: AsRef { +// impl>, U: ?Sized> AsRef for D { // fn as_ref(&self) -> &U { // self.deref().as_ref() // } @@ -530,7 +530,7 @@ impl AsMut for &mut T where T: AsMut // FIXME (#45742): replace the above impl for &mut with the following more general one: // // AsMut lifts over DerefMut -// impl AsMut for D where D::Target: AsMut { +// impl>, U: ?Sized> AsMut for D { // fn as_mut(&mut self) -> &mut U { // self.deref_mut().as_mut() // } diff --git a/src/libcore/future/future.rs b/src/libcore/future/future.rs index 593c01060ca49..f14ed38b9b0f2 100644 --- a/src/libcore/future/future.rs +++ b/src/libcore/future/future.rs @@ -111,8 +111,7 @@ impl Future for &mut F { #[stable(feature = "futures_api", since = "1.36.0")] impl

Future for Pin

where - P: Unpin + ops::DerefMut, - P::Target: Future, + P: Unpin + ops::DerefMut, { type Output = <

::Target as Future>::Output; diff --git a/src/libcore/iter/adapters/flatten.rs b/src/libcore/iter/adapters/flatten.rs index 8c2aae477bf2a..d8d41a2a31ef6 100644 --- a/src/libcore/iter/adapters/flatten.rs +++ b/src/libcore/iter/adapters/flatten.rs @@ -24,15 +24,17 @@ impl U> FlatMap { } #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for FlatMap - where ::IntoIter: Clone +impl Clone for FlatMap +where + U: Clone + IntoIterator, { fn clone(&self) -> Self { FlatMap { inner: self.inner.clone() } } } #[stable(feature = "core_impl_debug", since = "1.9.0")] -impl fmt::Debug for FlatMap - where U::IntoIter: fmt::Debug +impl fmt::Debug for FlatMap +where + U: IntoIterator, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FlatMap").field("inner", &self.inner).finish() @@ -68,9 +70,10 @@ impl Iterator for FlatMap #[stable(feature = "rust1", since = "1.0.0")] impl DoubleEndedIterator for FlatMap - where F: FnMut(I::Item) -> U, - U: IntoIterator, - U::IntoIter: DoubleEndedIterator +where + F: FnMut(I::Item) -> U, + U: IntoIterator, + U::IntoIter: DoubleEndedIterator, { #[inline] fn next_back(&mut self) -> Option { self.inner.next_back() } @@ -105,11 +108,13 @@ impl FusedIterator for FlatMap #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iterator_flatten", since = "1.29.0")] pub struct Flatten -where I::Item: IntoIterator { +where + I::Item: IntoIterator, +{ inner: FlattenCompat::IntoIter>, } -impl Flatten -where I::Item: IntoIterator { + +impl> Flatten { pub(in super::super) fn new(iter: I) -> Flatten { Flatten { inner: FlattenCompat::new(iter) } } @@ -117,8 +122,9 @@ where I::Item: IntoIterator { #[stable(feature = "iterator_flatten", since = "1.29.0")] impl fmt::Debug for Flatten - where I: Iterator + fmt::Debug, U: Iterator + fmt::Debug, - I::Item: IntoIterator, +where + I: fmt::Debug + Iterator>, + U: fmt::Debug + Iterator, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Flatten").field("inner", &self.inner).finish() @@ -127,16 +133,18 @@ impl fmt::Debug for Flatten #[stable(feature = "iterator_flatten", since = "1.29.0")] impl Clone for Flatten - where I: Iterator + Clone, U: Iterator + Clone, - I::Item: IntoIterator, +where + I: Clone + Iterator>, + U: Clone + Iterator, { fn clone(&self) -> Self { Flatten { inner: self.inner.clone() } } } #[stable(feature = "iterator_flatten", since = "1.29.0")] impl Iterator for Flatten - where I: Iterator, U: Iterator, - I::Item: IntoIterator +where + I: Iterator>, + U: Iterator, { type Item = U::Item; @@ -163,8 +171,9 @@ impl Iterator for Flatten #[stable(feature = "iterator_flatten", since = "1.29.0")] impl DoubleEndedIterator for Flatten - where I: DoubleEndedIterator, U: DoubleEndedIterator, - I::Item: IntoIterator +where + I: DoubleEndedIterator>, + U: DoubleEndedIterator, { #[inline] fn next_back(&mut self) -> Option { self.inner.next_back() } @@ -186,8 +195,10 @@ impl DoubleEndedIterator for Flatten #[stable(feature = "iterator_flatten", since = "1.29.0")] impl FusedIterator for Flatten - where I: FusedIterator, U: Iterator, - I::Item: IntoIterator {} +where + I: FusedIterator>, + U: Iterator, +{} /// Real logic of both `Flatten` and `FlatMap` which simply delegate to /// this type. @@ -205,8 +216,9 @@ impl FlattenCompat { } impl Iterator for FlattenCompat - where I: Iterator, U: Iterator, - I::Item: IntoIterator +where + I: Iterator>, + U: Iterator, { type Item = U::Item; @@ -274,8 +286,9 @@ impl Iterator for FlattenCompat } impl DoubleEndedIterator for FlattenCompat - where I: DoubleEndedIterator, U: DoubleEndedIterator, - I::Item: IntoIterator +where + I: DoubleEndedIterator>, + U: DoubleEndedIterator, { #[inline] fn next_back(&mut self) -> Option { diff --git a/src/libcore/iter/traits/collect.rs b/src/libcore/iter/traits/collect.rs index 1865160bc3cf4..9f15de33425f4 100644 --- a/src/libcore/iter/traits/collect.rs +++ b/src/libcore/iter/traits/collect.rs @@ -195,8 +195,8 @@ pub trait FromIterator: Sized { /// /// ```rust /// fn collect_as_strings(collection: T) -> Vec -/// where T: IntoIterator, -/// T::Item: std::fmt::Debug, +/// where +/// T: IntoIterator, /// { /// collection /// .into_iter() diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 4d627383fd7cc..678ff7687921b 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -132,6 +132,7 @@ #![feature(maybe_uninit_slice, maybe_uninit_array)] #![feature(external_doc)] #![feature(mem_take)] +#![feature(associated_type_bounds)] #[prelude_import] #[allow(unused)] diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index 88a56174629f9..243209d022b49 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -439,10 +439,7 @@ where } } -impl Pin

-where - P::Target: Unpin, -{ +impl> Pin

{ /// Construct a new `Pin

` around a pointer to some data of a type that /// implements [`Unpin`]. /// @@ -730,10 +727,7 @@ impl Deref for Pin

{ } #[stable(feature = "pin", since = "1.33.0")] -impl DerefMut for Pin

-where - P::Target: Unpin -{ +impl> DerefMut for Pin

{ fn deref_mut(&mut self) -> &mut P::Target { Pin::get_mut(Pin::as_mut(self)) } diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 4faf9ff4d2ee2..f20cb7bfbc3bd 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -851,8 +851,9 @@ unsafe impl TrustedRandomAccess for Bytes<'_> { /// wrapper types of the form X<'a, P> macro_rules! derive_pattern_clone { (clone $t:ident with |$s:ident| $e:expr) => { - impl<'a, P: Pattern<'a>> Clone for $t<'a, P> - where P::Searcher: Clone + impl<'a, P> Clone for $t<'a, P> + where + P: Pattern<'a, Searcher: Clone>, { fn clone(&self) -> Self { let $s = self; @@ -928,8 +929,9 @@ macro_rules! generate_pattern_iterators { pub struct $forward_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>); $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> fmt::Debug for $forward_iterator<'a, P> - where P::Searcher: fmt::Debug + impl<'a, P> fmt::Debug for $forward_iterator<'a, P> + where + P: Pattern<'a, Searcher: fmt::Debug>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple(stringify!($forward_iterator)) @@ -949,8 +951,9 @@ macro_rules! generate_pattern_iterators { } $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> Clone for $forward_iterator<'a, P> - where P::Searcher: Clone + impl<'a, P> Clone for $forward_iterator<'a, P> + where + P: Pattern<'a, Searcher: Clone>, { fn clone(&self) -> Self { $forward_iterator(self.0.clone()) @@ -962,8 +965,9 @@ macro_rules! generate_pattern_iterators { pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>); $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> fmt::Debug for $reverse_iterator<'a, P> - where P::Searcher: fmt::Debug + impl<'a, P> fmt::Debug for $reverse_iterator<'a, P> + where + P: Pattern<'a, Searcher: fmt::Debug>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple(stringify!($reverse_iterator)) @@ -973,8 +977,9 @@ macro_rules! generate_pattern_iterators { } $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> Iterator for $reverse_iterator<'a, P> - where P::Searcher: ReverseSearcher<'a> + impl<'a, P> Iterator for $reverse_iterator<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { type Item = $iterty; @@ -985,8 +990,9 @@ macro_rules! generate_pattern_iterators { } $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> Clone for $reverse_iterator<'a, P> - where P::Searcher: Clone + impl<'a, P> Clone for $reverse_iterator<'a, P> + where + P: Pattern<'a, Searcher: Clone>, { fn clone(&self) -> Self { $reverse_iterator(self.0.clone()) @@ -997,8 +1003,10 @@ macro_rules! generate_pattern_iterators { impl<'a, P: Pattern<'a>> FusedIterator for $forward_iterator<'a, P> {} #[stable(feature = "fused", since = "1.26.0")] - impl<'a, P: Pattern<'a>> FusedIterator for $reverse_iterator<'a, P> - where P::Searcher: ReverseSearcher<'a> {} + impl<'a, P> FusedIterator for $reverse_iterator<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, + {} generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*, $forward_iterator, @@ -1010,8 +1018,9 @@ macro_rules! generate_pattern_iterators { $reverse_iterator:ident, $iterty:ty } => { $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> DoubleEndedIterator for $forward_iterator<'a, P> - where P::Searcher: DoubleEndedSearcher<'a> + impl<'a, P> DoubleEndedIterator for $forward_iterator<'a, P> + where + P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>, { #[inline] fn next_back(&mut self) -> Option<$iterty> { @@ -1020,8 +1029,9 @@ macro_rules! generate_pattern_iterators { } $(#[$common_stability_attribute])* - impl<'a, P: Pattern<'a>> DoubleEndedIterator for $reverse_iterator<'a, P> - where P::Searcher: DoubleEndedSearcher<'a> + impl<'a, P> DoubleEndedIterator for $reverse_iterator<'a, P> + where + P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>, { #[inline] fn next_back(&mut self) -> Option<$iterty> { @@ -1049,7 +1059,10 @@ struct SplitInternal<'a, P: Pattern<'a>> { finished: bool, } -impl<'a, P: Pattern<'a>> fmt::Debug for SplitInternal<'a, P> where P::Searcher: fmt::Debug { +impl<'a, P> fmt::Debug for SplitInternal<'a, P> +where + P: Pattern<'a, Searcher: fmt::Debug>, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SplitInternal") .field("start", &self.start) @@ -1166,7 +1179,10 @@ struct SplitNInternal<'a, P: Pattern<'a>> { count: usize, } -impl<'a, P: Pattern<'a>> fmt::Debug for SplitNInternal<'a, P> where P::Searcher: fmt::Debug { +impl<'a, P> fmt::Debug for SplitNInternal<'a, P> +where + P: Pattern<'a, Searcher: fmt::Debug>, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SplitNInternal") .field("iter", &self.iter) @@ -1222,7 +1238,10 @@ derive_pattern_clone!{ struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher); -impl<'a, P: Pattern<'a>> fmt::Debug for MatchIndicesInternal<'a, P> where P::Searcher: fmt::Debug { +impl<'a, P> fmt::Debug for MatchIndicesInternal<'a, P> +where + P: Pattern<'a, Searcher: fmt::Debug>, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("MatchIndicesInternal") .field(&self.0) @@ -1273,7 +1292,10 @@ derive_pattern_clone!{ struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher); -impl<'a, P: Pattern<'a>> fmt::Debug for MatchesInternal<'a, P> where P::Searcher: fmt::Debug { +impl<'a, P> fmt::Debug for MatchesInternal<'a, P> +where + P: Pattern<'a, Searcher: fmt::Debug>, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("MatchesInternal") .field(&self.0) @@ -2882,8 +2904,9 @@ impl str { /// assert!(!bananas.ends_with("nana")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool - where P::Searcher: ReverseSearcher<'a> + pub fn ends_with<'a, P>(&'a self, pat: P) -> bool + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { pat.is_suffix_of(self) } @@ -2975,8 +2998,9 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option - where P::Searcher: ReverseSearcher<'a> + pub fn rfind<'a, P>(&'a self, pat: P) -> Option + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { pat.into_searcher(self).next_match_back().map(|(i, _)| i) } @@ -3142,8 +3166,9 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P> - where P::Searcher: ReverseSearcher<'a> + pub fn rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { RSplit(self.split(pat).0) } @@ -3233,8 +3258,9 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P> - where P::Searcher: ReverseSearcher<'a> + pub fn rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { RSplitTerminator(self.split_terminator(pat).0) } @@ -3333,8 +3359,9 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rsplitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> RSplitN<'a, P> - where P::Searcher: ReverseSearcher<'a> + pub fn rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { RSplitN(self.splitn(n, pat).0) } @@ -3406,8 +3433,9 @@ impl str { /// ``` #[stable(feature = "str_matches", since = "1.2.0")] #[inline] - pub fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P> - where P::Searcher: ReverseSearcher<'a> + pub fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { RMatches(self.matches(pat).0) } @@ -3491,8 +3519,9 @@ impl str { /// ``` #[stable(feature = "str_match_indices", since = "1.5.0")] #[inline] - pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P> - where P::Searcher: ReverseSearcher<'a> + pub fn rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P> + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { RMatchIndices(self.match_indices(pat).0) } @@ -3700,8 +3729,9 @@ impl str { #[must_use = "this returns the trimmed string as a new slice, \ without modifying the original"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str - where P::Searcher: DoubleEndedSearcher<'a> + pub fn trim_matches<'a, P>(&'a self, pat: P) -> &'a str + where + P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>, { let mut i = 0; let mut j = 0; @@ -3792,8 +3822,9 @@ impl str { #[must_use = "this returns the trimmed string as a new slice, \ without modifying the original"] #[stable(feature = "trim_direction", since = "1.30.0")] - pub fn trim_end_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str - where P::Searcher: ReverseSearcher<'a> + pub fn trim_end_matches<'a, P>(&'a self, pat: P) -> &'a str + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { let mut j = 0; let mut matcher = pat.into_searcher(self); @@ -3880,8 +3911,9 @@ impl str { reason = "superseded by `trim_end_matches`", suggestion = "trim_end_matches", )] - pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str - where P::Searcher: ReverseSearcher<'a> + pub fn trim_right_matches<'a, P>(&'a self, pat: P) -> &'a str + where + P: Pattern<'a, Searcher: ReverseSearcher<'a>>, { self.trim_end_matches(pat) } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 8e0581b41ef7a..8d4cd51e4608c 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -61,6 +61,7 @@ #![feature(proc_macro_hygiene)] #![feature(log_syntax)] #![feature(mem_take)] +#![feature(associated_type_bounds)] #![recursion_limit="512"] diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index da582c015e4ea..d4ae366262cbf 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -3901,7 +3901,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // each predicate must be preceded by the obligations required // to normalize it. // for example, if we have: - // impl> Foo for V where U::Item: Copy + // impl, V: Iterator> Foo for V // the impl will have the following predicates: // ::Item = U, // U: Iterator, U: Sized, diff --git a/src/librustc/traits/structural_impls.rs b/src/librustc/traits/structural_impls.rs index 129a400d28f4c..05b698eb4c4ea 100644 --- a/src/librustc/traits/structural_impls.rs +++ b/src/librustc/traits/structural_impls.rs @@ -980,8 +980,7 @@ EnumTypeFoldableImpl! { (chalk_engine::DelayedLiteral::Negative)(a), (chalk_engine::DelayedLiteral::Positive)(a, b), } where - C: chalk_engine::context::Context + Clone, - C::CanonicalConstrainedSubst: TypeFoldable<'tcx>, + C: chalk_engine::context::Context> + Clone, } EnumTypeFoldableImpl! { @@ -989,8 +988,7 @@ EnumTypeFoldableImpl! { (chalk_engine::Literal::Negative)(a), (chalk_engine::Literal::Positive)(a), } where - C: chalk_engine::context::Context + Clone, - C::GoalInEnvironment: Clone + TypeFoldable<'tcx>, + C: chalk_engine::context::Context> + Clone, } CloneTypeFoldableAndLiftImpls! { diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index dadc126eba48e..ef74d9e5b2899 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -2663,8 +2663,8 @@ impl<'tcx> TyCtxt<'tcx> { unsafety: hir::Unsafety, abi: abi::Abi) -> , ty::FnSig<'tcx>>>::Output - where I: Iterator, - I::Item: InternIteratorElement, ty::FnSig<'tcx>> + where + I: Iterator, ty::FnSig<'tcx>>>, { inputs.chain(iter::once(output)).intern_with(|xs| ty::FnSig { inputs_and_output: self.intern_type_list(xs), diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 1c9a5ad621854..a9d1fd1fffc92 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2027,9 +2027,9 @@ impl ty::query::TyCtxtAt<'tcx> { impl<'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> where - C: LayoutOf> + HasTyCtxt<'tcx>, - C::TyLayout: MaybeResult>, - C: HasParamEnv<'tcx>, + C: LayoutOf, TyLayout: MaybeResult>> + + HasTyCtxt<'tcx> + + HasParamEnv<'tcx>, { fn for_variant(this: TyLayout<'tcx>, cx: &C, variant_index: VariantIdx) -> TyLayout<'tcx> { let details = match this.variants { diff --git a/src/librustc/ty/query/on_disk_cache.rs b/src/librustc/ty/query/on_disk_cache.rs index 45bc89f5a84ab..40bcd028db58d 100644 --- a/src/librustc/ty/query/on_disk_cache.rs +++ b/src/librustc/ty/query/on_disk_cache.rs @@ -1055,9 +1055,8 @@ fn encode_query_results<'a, 'tcx, Q, E>( query_result_index: &mut EncodedQueryResultIndex, ) -> Result<(), E::Error> where - Q: super::config::QueryDescription<'tcx>, + Q: super::config::QueryDescription<'tcx, Value: Encodable>, E: 'a + TyEncoder, - Q::Value: Encodable, { let desc = &format!("encode_query_results for {}", ::std::any::type_name::()); diff --git a/src/librustc_codegen_ssa/back/command.rs b/src/librustc_codegen_ssa/back/command.rs index d610805b5bbd0..340cc772e5f07 100644 --- a/src/librustc_codegen_ssa/back/command.rs +++ b/src/librustc_codegen_ssa/back/command.rs @@ -50,8 +50,8 @@ impl Command { } pub fn args(&mut self, args: I) -> &mut Command - where I: IntoIterator, - I::Item: AsRef, + where + I: IntoIterator>, { for arg in args { self._arg(arg.as_ref()); diff --git a/src/librustc_codegen_ssa/lib.rs b/src/librustc_codegen_ssa/lib.rs index 73ef16e009146..0e3c3a77b28f4 100644 --- a/src/librustc_codegen_ssa/lib.rs +++ b/src/librustc_codegen_ssa/lib.rs @@ -11,6 +11,7 @@ #![feature(nll)] #![feature(trusted_len)] #![feature(mem_take)] +#![feature(associated_type_bounds)] #![recursion_limit="256"] diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 8fb0ea0271b97..9f103437d368e 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -23,6 +23,7 @@ #![feature(core_intrinsics)] #![feature(integer_atomics)] #![feature(test)] +#![feature(associated_type_bounds)] #![cfg_attr(unix, feature(libc))] diff --git a/src/librustc_data_structures/owning_ref/mod.rs b/src/librustc_data_structures/owning_ref/mod.rs index ea9c5283aee7d..b835b1706b85f 100644 --- a/src/librustc_data_structures/owning_ref/mod.rs +++ b/src/librustc_data_structures/owning_ref/mod.rs @@ -847,7 +847,9 @@ pub trait ToHandleMut { } impl OwningHandle - where O: StableAddress, O::Target: ToHandle, H: Deref, +where + O: StableAddress>, + H: Deref, { /// Creates a new `OwningHandle` for a type that implements `ToHandle`. For types /// that don't implement `ToHandle`, callers may invoke `new_with_fn`, which accepts @@ -858,7 +860,9 @@ impl OwningHandle } impl OwningHandle - where O: StableAddress, O::Target: ToHandleMut, H: DerefMut, +where + O: StableAddress>, + H: DerefMut, { /// Creates a new mutable `OwningHandle` for a type that implements `ToHandleMut`. pub fn new_mut(o: O) -> Self { diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index 3bdd3e3da048e..7fe2a890a5371 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -589,10 +589,8 @@ impl GenKillSet { self.gen_set.insert(e); self.kill_set.remove(e); } - fn gen_all(&mut self, i: I) - where I: IntoIterator, - I::Item: Borrow - { + + fn gen_all(&mut self, i: impl IntoIterator>) { for j in i { self.gen(*j.borrow()); } @@ -603,10 +601,7 @@ impl GenKillSet { self.kill_set.insert(e); } - fn kill_all(&mut self, i: I) - where I: IntoIterator, - I::Item: Borrow - { + fn kill_all(&mut self, i: impl IntoIterator>) { for j in i { self.kill(*j.borrow()); } diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 20d5e54d2ce49..cccf7b9545bdb 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -23,6 +23,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(trusted_len)] #![feature(try_blocks)] #![feature(mem_take)] +#![feature(associated_type_bounds)] #![recursion_limit="256"] diff --git a/src/libserialize/collection_impls.rs b/src/libserialize/collection_impls.rs index 80aeecb84d72b..d981740780e6f 100644 --- a/src/libserialize/collection_impls.rs +++ b/src/libserialize/collection_impls.rs @@ -9,10 +9,7 @@ use std::sync::Arc; use smallvec::{Array, SmallVec}; -impl Encodable for SmallVec - where A: Array, - A::Item: Encodable -{ +impl> Encodable for SmallVec { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { @@ -23,10 +20,7 @@ impl Encodable for SmallVec } } -impl Decodable for SmallVec - where A: Array, - A::Item: Decodable -{ +impl> Decodable for SmallVec { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut vec = SmallVec::with_capacity(len); diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index 2ad85c603d1e4..67a48ca4af902 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -13,6 +13,7 @@ Core encoding and decoding interfaces. #![feature(specialization)] #![feature(never_type)] #![feature(nll)] +#![feature(associated_type_bounds)] #![cfg_attr(test, feature(test))] pub use self::serialize::{Decoder, Encoder, Decodable, Encodable}; diff --git a/src/libstd/sys/sgx/abi/usercalls/alloc.rs b/src/libstd/sys/sgx/abi/usercalls/alloc.rs index c9ff53d0a4fd6..75dd0d429c214 100644 --- a/src/libstd/sys/sgx/abi/usercalls/alloc.rs +++ b/src/libstd/sys/sgx/abi/usercalls/alloc.rs @@ -522,7 +522,11 @@ impl Drop for User where T: UserSafe { impl, U> CoerceUnsized> for UserRef {} #[unstable(feature = "sgx_platform", issue = "56975")] -impl> Index for UserRef<[T]> where [T]: UserSafe, I::Output: UserSafe { +impl Index for UserRef<[T]> +where + [T]: UserSafe, + I: SliceIndex<[T], Output: UserSafe>, +{ type Output = UserRef; #[inline] @@ -538,7 +542,11 @@ impl> Index for UserRef<[T]> where [T]: UserSafe, I::Ou } #[unstable(feature = "sgx_platform", issue = "56975")] -impl> IndexMut for UserRef<[T]> where [T]: UserSafe, I::Output: UserSafe { +impl IndexMut for UserRef<[T]> +where + [T]: UserSafe, + I: SliceIndex<[T], Output: UserSafe>, +{ #[inline] fn index_mut(&mut self, index: I) -> &mut UserRef { unsafe { From 322a7d6387a875d9f751c1b332f84c181e57ac64 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 8 Aug 2019 19:16:08 -0400 Subject: [PATCH 013/148] Add test for issue 36804 --- src/test/ui/specialization/issue-36804.rs | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/ui/specialization/issue-36804.rs diff --git a/src/test/ui/specialization/issue-36804.rs b/src/test/ui/specialization/issue-36804.rs new file mode 100644 index 0000000000000..36cb939bc48fb --- /dev/null +++ b/src/test/ui/specialization/issue-36804.rs @@ -0,0 +1,31 @@ +// check-pass +#![feature(specialization)] + +pub struct Cloned(I); + +impl<'a, I, T: 'a> Iterator for Cloned +where + I: Iterator, + T: Clone, +{ + type Item = T; + + fn next(&mut self) -> Option { + unimplemented!() + } +} + +impl<'a, I, T: 'a> Iterator for Cloned +where + I: Iterator, + T: Copy, +{ + fn count(self) -> usize { + unimplemented!() + } +} + +fn main() { + let a = [1,2,3,4]; + Cloned(a.iter()).count(); +} From 642ee709427ada3711fb27dba9d26bbdaee6a3da Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 9 Aug 2019 08:37:40 +0900 Subject: [PATCH 014/148] Add test for issue-43623 --- src/test/ui/issues/issue-43623.rs | 19 ++++++++++++ src/test/ui/issues/issue-43623.stderr | 42 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/test/ui/issues/issue-43623.rs create mode 100644 src/test/ui/issues/issue-43623.stderr diff --git a/src/test/ui/issues/issue-43623.rs b/src/test/ui/issues/issue-43623.rs new file mode 100644 index 0000000000000..b259e9e269d06 --- /dev/null +++ b/src/test/ui/issues/issue-43623.rs @@ -0,0 +1,19 @@ +pub trait Trait<'a> { + type Assoc; +} + +pub struct Type; + +impl<'a> Trait<'a> for Type { + type Assoc = (); +} + +pub fn break_me(f: F) +where T: for<'b> Trait<'b>, + F: for<'b> FnMut(>::Assoc) { + break_me::; + //~^ ERROR: type mismatch in function arguments + //~| ERROR: type mismatch resolving +} + +fn main() {} diff --git a/src/test/ui/issues/issue-43623.stderr b/src/test/ui/issues/issue-43623.stderr new file mode 100644 index 0000000000000..b5674105f75d2 --- /dev/null +++ b/src/test/ui/issues/issue-43623.stderr @@ -0,0 +1,42 @@ +error[E0631]: type mismatch in function arguments + --> $DIR/issue-43623.rs:14:5 + | +LL | break_me::; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected signature of `for<'b> fn(>::Assoc) -> _` + | found signature of `fn(_) -> _` + | +note: required by `break_me` + --> $DIR/issue-43623.rs:11:1 + | +LL | / pub fn break_me(f: F) +LL | | where T: for<'b> Trait<'b>, +LL | | F: for<'b> FnMut(>::Assoc) { +LL | | break_me::; +LL | | +LL | | +LL | | } + | |_^ + +error[E0271]: type mismatch resolving `for<'b> >::Assoc,)>>::Output == ()` + --> $DIR/issue-43623.rs:14:5 + | +LL | break_me::; + | ^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'b, found concrete lifetime + | +note: required by `break_me` + --> $DIR/issue-43623.rs:11:1 + | +LL | / pub fn break_me(f: F) +LL | | where T: for<'b> Trait<'b>, +LL | | F: for<'b> FnMut(>::Assoc) { +LL | | break_me::; +LL | | +LL | | +LL | | } + | |_^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. From 55f15d765565773e95c60e7e14b956bfeed30157 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 9 Aug 2019 08:37:55 +0900 Subject: [PATCH 015/148] Add test for issue-44405 --- src/test/ui/issues/issue-44405.rs | 22 ++++++++++++++++++++++ src/test/ui/issues/issue-44405.stderr | 11 +++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/test/ui/issues/issue-44405.rs create mode 100644 src/test/ui/issues/issue-44405.stderr diff --git a/src/test/ui/issues/issue-44405.rs b/src/test/ui/issues/issue-44405.rs new file mode 100644 index 0000000000000..d404b9044dd6f --- /dev/null +++ b/src/test/ui/issues/issue-44405.rs @@ -0,0 +1,22 @@ +use std::ops::Index; + +struct Test; +struct Container(Test); + +impl Test { + fn test(&mut self) {} +} + +impl<'a> Index<&'a bool> for Container { + type Output = Test; + + fn index(&self, _index: &'a bool) -> &Test { + &self.0 + } +} + +fn main() { + let container = Container(Test); + let mut val = true; + container[&mut val].test(); //~ ERROR: cannot borrow data +} diff --git a/src/test/ui/issues/issue-44405.stderr b/src/test/ui/issues/issue-44405.stderr new file mode 100644 index 0000000000000..1fd69f6e77799 --- /dev/null +++ b/src/test/ui/issues/issue-44405.stderr @@ -0,0 +1,11 @@ +error[E0596]: cannot borrow data in an index of `Container` as mutable + --> $DIR/issue-44405.rs:21:5 + | +LL | container[&mut val].test(); + | ^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable + | + = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `Container` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`. From 36c4873d0934faf56dbfa3e1b84c3a750008c2f8 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 8 Aug 2019 16:44:42 -0700 Subject: [PATCH 016/148] Try to fix test on Windows. --- src/test/run-make-fulldeps/reproducible-build/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/run-make-fulldeps/reproducible-build/Makefile b/src/test/run-make-fulldeps/reproducible-build/Makefile index addbf9928bf4a..5b9c9d3d03521 100644 --- a/src/test/run-make-fulldeps/reproducible-build/Makefile +++ b/src/test/run-make-fulldeps/reproducible-build/Makefile @@ -81,7 +81,7 @@ extern_flags: fat_lto: rm -rf $(TMPDIR) && mkdir $(TMPDIR) $(RUSTC) reproducible-build-aux.rs - $(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1 + $(RUSTC) reproducible-build.rs -C lto=fat cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a - $(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1 + $(RUSTC) reproducible-build.rs -C lto=fat cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1 From fd7ac6b17ef98f63438f59edca40562c400d4128 Mon Sep 17 00:00:00 2001 From: BO41 Date: Sun, 23 Jun 2019 12:19:46 +0000 Subject: [PATCH 017/148] Deprecate `try!` macro Co-Authored-By: Mazdak Farrokhzad Co-Authored-By: Oliver Middleton --- src/libcore/macros.rs | 1 + src/libstd/lib.rs | 5 ++++- src/test/ui/associated-types/cache/chrono-scan.rs | 2 +- src/test/ui/derived-errors/issue-31997.rs | 2 +- src/test/ui/derived-errors/issue-31997.stderr | 6 +++--- src/test/ui/lint/lint-qualification.rs | 2 +- src/test/ui/macros/macro-comma-support-rpass.rs | 2 ++ src/test/ui/macros/try-macro.rs | 1 + src/test/ui/rust-2018/try-macro.fixed | 1 + src/test/ui/rust-2018/try-macro.rs | 1 + src/test/ui/rust-2018/try-macro.stderr | 2 +- 11 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 09d2331b60fed..ba641f7dc5c12 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -300,6 +300,7 @@ macro_rules! debug_assert_ne { /// Ok(()) /// } /// ``` +#[rustc_deprecated(since = "1.38.0", reason = "use the `?` operator instead")] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] #[doc(alias = "?")] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index cfee49a7b555c..34956fc839450 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -330,7 +330,10 @@ use prelude::v1::*; #[stable(feature = "rust1", since = "1.0.0")] pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::{unreachable, unimplemented, write, writeln, r#try, todo}; +pub use core::{unreachable, unimplemented, write, writeln, todo}; +#[allow(deprecated)] +#[stable(feature = "rust1", since = "1.0.0")] +pub use core::r#try; #[allow(unused_imports)] // macros from `alloc` are not used on all platforms #[macro_use] diff --git a/src/test/ui/associated-types/cache/chrono-scan.rs b/src/test/ui/associated-types/cache/chrono-scan.rs index 8ddd347ff3607..5c05240552761 100644 --- a/src/test/ui/associated-types/cache/chrono-scan.rs +++ b/src/test/ui/associated-types/cache/chrono-scan.rs @@ -18,7 +18,7 @@ pub fn timezone_offset_zulu(s: &str, colon: F) -> ParseResult<(&str, i32)> pub fn parse<'a, I>(mut s: &str, items: I) -> ParseResult<()> where I: Iterator> { macro_rules! try_consume { - ($e:expr) => ({ let (s_, v) = try!($e); s = s_; v }) + ($e:expr) => ({ let (s_, v) = $e?; s = s_; v }) } let offset = try_consume!(timezone_offset_zulu(s.trim_start(), colon_or_space)); let offset = try_consume!(timezone_offset_zulu(s.trim_start(), colon_or_space)); diff --git a/src/test/ui/derived-errors/issue-31997.rs b/src/test/ui/derived-errors/issue-31997.rs index cfdee26c5599c..6d7d21e367322 100644 --- a/src/test/ui/derived-errors/issue-31997.rs +++ b/src/test/ui/derived-errors/issue-31997.rs @@ -10,7 +10,7 @@ fn closure(x: F) -> Result } fn foo() -> Result<(), ()> { - try!(closure(|| bar(core::ptr::null_mut()))); //~ ERROR cannot find function `bar` in this scope + closure(|| bar(core::ptr::null_mut()))?; //~ ERROR cannot find function `bar` in this scope Ok(()) } diff --git a/src/test/ui/derived-errors/issue-31997.stderr b/src/test/ui/derived-errors/issue-31997.stderr index e9fe0d3971ee6..d9260f79f2742 100644 --- a/src/test/ui/derived-errors/issue-31997.stderr +++ b/src/test/ui/derived-errors/issue-31997.stderr @@ -1,8 +1,8 @@ error[E0425]: cannot find function `bar` in this scope - --> $DIR/issue-31997.rs:13:21 + --> $DIR/issue-31997.rs:13:16 | -LL | try!(closure(|| bar(core::ptr::null_mut()))); - | ^^^ not found in this scope +LL | closure(|| bar(core::ptr::null_mut()))?; + | ^^^ not found in this scope error: aborting due to previous error diff --git a/src/test/ui/lint/lint-qualification.rs b/src/test/ui/lint/lint-qualification.rs index 2aa4526b8169b..d458c31dc4926 100644 --- a/src/test/ui/lint/lint-qualification.rs +++ b/src/test/ui/lint/lint-qualification.rs @@ -9,7 +9,7 @@ fn main() { foo::bar(); //~ ERROR: unnecessary qualification bar(); - let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345 + let _ = || -> Result<(), ()> { Ok(())?; Ok(()) }; // issue #37345 macro_rules! m { () => { $crate::foo::bar(); // issue #37357 diff --git a/src/test/ui/macros/macro-comma-support-rpass.rs b/src/test/ui/macros/macro-comma-support-rpass.rs index 12a612c153ad6..17c27b7cda473 100644 --- a/src/test/ui/macros/macro-comma-support-rpass.rs +++ b/src/test/ui/macros/macro-comma-support-rpass.rs @@ -261,7 +261,9 @@ fn thread_local() { #[test] fn try() { fn inner() -> Result<(), ()> { + #[allow(deprecated)] try!(Ok(())); + #[allow(deprecated)] try!(Ok(()),); Ok(()) } diff --git a/src/test/ui/macros/try-macro.rs b/src/test/ui/macros/try-macro.rs index 83b30a8b7ba11..643312654493a 100644 --- a/src/test/ui/macros/try-macro.rs +++ b/src/test/ui/macros/try-macro.rs @@ -1,4 +1,5 @@ // run-pass +#[allow(deprecated)] use std::num::{ParseFloatError, ParseIntError}; fn main() { diff --git a/src/test/ui/rust-2018/try-macro.fixed b/src/test/ui/rust-2018/try-macro.fixed index 7c1692fd7fb13..a7b7d3faf5ee9 100644 --- a/src/test/ui/rust-2018/try-macro.fixed +++ b/src/test/ui/rust-2018/try-macro.fixed @@ -6,6 +6,7 @@ #![warn(rust_2018_compatibility)] #![allow(unused_variables)] #![allow(dead_code)] +#![allow(deprecated)] fn foo() -> Result { let x: Result = Ok(22); diff --git a/src/test/ui/rust-2018/try-macro.rs b/src/test/ui/rust-2018/try-macro.rs index 2089d367be698..986e158eb644e 100644 --- a/src/test/ui/rust-2018/try-macro.rs +++ b/src/test/ui/rust-2018/try-macro.rs @@ -6,6 +6,7 @@ #![warn(rust_2018_compatibility)] #![allow(unused_variables)] #![allow(dead_code)] +#![allow(deprecated)] fn foo() -> Result { let x: Result = Ok(22); diff --git a/src/test/ui/rust-2018/try-macro.stderr b/src/test/ui/rust-2018/try-macro.stderr index eb65d4150642a..fad1bb9f1b068 100644 --- a/src/test/ui/rust-2018/try-macro.stderr +++ b/src/test/ui/rust-2018/try-macro.stderr @@ -1,5 +1,5 @@ warning: `try` is a keyword in the 2018 edition - --> $DIR/try-macro.rs:12:5 + --> $DIR/try-macro.rs:13:5 | LL | try!(x); | ^^^ help: you can use a raw identifier to stay compatible: `r#try` From 90fa7901b98367d04857cb96366b2eedf70693e2 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 15 Jul 2019 17:42:08 +0000 Subject: [PATCH 018/148] Postpone deprecating try! until 1.39.0 --- src/libcore/macros.rs | 2 +- src/libstd/lib.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index ba641f7dc5c12..aa661078e7176 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -300,9 +300,9 @@ macro_rules! debug_assert_ne { /// Ok(()) /// } /// ``` -#[rustc_deprecated(since = "1.38.0", reason = "use the `?` operator instead")] #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")] #[doc(alias = "?")] macro_rules! r#try { ($expr:expr) => (match $expr { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 34956fc839450..54abf72d3075a 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -331,7 +331,8 @@ use prelude::v1::*; pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::{unreachable, unimplemented, write, writeln, todo}; -#[allow(deprecated)] +// FIXME: change this to `#[allow(deprecated)]` when we update nightly compiler. +#[allow(deprecated_in_future)] #[stable(feature = "rust1", since = "1.0.0")] pub use core::r#try; From 6842316f6ff4586465e6412edce5e6808cfcd396 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 14 Jul 2019 08:16:46 +0000 Subject: [PATCH 019/148] Allow deprecated try macro in test crates --- src/test/ui/associated-types/cache/chrono-scan.rs | 4 +++- src/test/ui/derived-errors/issue-31997.rs | 3 ++- src/test/ui/derived-errors/issue-31997.stderr | 6 +++--- src/test/ui/lint/lint-qualification.rs | 3 ++- src/test/ui/lint/lint-qualification.stderr | 2 +- src/test/ui/macros/macro-comma-support-rpass.rs | 3 +-- src/test/ui/macros/try-macro.rs | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/test/ui/associated-types/cache/chrono-scan.rs b/src/test/ui/associated-types/cache/chrono-scan.rs index 5c05240552761..964ddc9b625de 100644 --- a/src/test/ui/associated-types/cache/chrono-scan.rs +++ b/src/test/ui/associated-types/cache/chrono-scan.rs @@ -1,5 +1,7 @@ // check-pass +#![allow(deprecated)] + pub type ParseResult = Result; pub enum Item<'a> { @@ -18,7 +20,7 @@ pub fn timezone_offset_zulu(s: &str, colon: F) -> ParseResult<(&str, i32)> pub fn parse<'a, I>(mut s: &str, items: I) -> ParseResult<()> where I: Iterator> { macro_rules! try_consume { - ($e:expr) => ({ let (s_, v) = $e?; s = s_; v }) + ($e:expr) => ({ let (s_, v) = try!($e); s = s_; v }) } let offset = try_consume!(timezone_offset_zulu(s.trim_start(), colon_or_space)); let offset = try_consume!(timezone_offset_zulu(s.trim_start(), colon_or_space)); diff --git a/src/test/ui/derived-errors/issue-31997.rs b/src/test/ui/derived-errors/issue-31997.rs index 6d7d21e367322..ff619313afb5b 100644 --- a/src/test/ui/derived-errors/issue-31997.rs +++ b/src/test/ui/derived-errors/issue-31997.rs @@ -1,5 +1,6 @@ // Test that the resolve failure does not lead to downstream type errors. // See issue #31997. +#![allow(deprecated)] trait TheTrait { } @@ -10,7 +11,7 @@ fn closure(x: F) -> Result } fn foo() -> Result<(), ()> { - closure(|| bar(core::ptr::null_mut()))?; //~ ERROR cannot find function `bar` in this scope + try!(closure(|| bar(core::ptr::null_mut()))); //~ ERROR cannot find function `bar` in this scope Ok(()) } diff --git a/src/test/ui/derived-errors/issue-31997.stderr b/src/test/ui/derived-errors/issue-31997.stderr index d9260f79f2742..b53c0cda8de4d 100644 --- a/src/test/ui/derived-errors/issue-31997.stderr +++ b/src/test/ui/derived-errors/issue-31997.stderr @@ -1,8 +1,8 @@ error[E0425]: cannot find function `bar` in this scope - --> $DIR/issue-31997.rs:13:16 + --> $DIR/issue-31997.rs:14:21 | -LL | closure(|| bar(core::ptr::null_mut()))?; - | ^^^ not found in this scope +LL | try!(closure(|| bar(core::ptr::null_mut()))); + | ^^^ not found in this scope error: aborting due to previous error diff --git a/src/test/ui/lint/lint-qualification.rs b/src/test/ui/lint/lint-qualification.rs index d458c31dc4926..1b24191a111c2 100644 --- a/src/test/ui/lint/lint-qualification.rs +++ b/src/test/ui/lint/lint-qualification.rs @@ -1,4 +1,5 @@ #![deny(unused_qualifications)] +#[allow(deprecated)] mod foo { pub fn bar() {} @@ -9,7 +10,7 @@ fn main() { foo::bar(); //~ ERROR: unnecessary qualification bar(); - let _ = || -> Result<(), ()> { Ok(())?; Ok(()) }; // issue #37345 + let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345 macro_rules! m { () => { $crate::foo::bar(); // issue #37357 diff --git a/src/test/ui/lint/lint-qualification.stderr b/src/test/ui/lint/lint-qualification.stderr index 78f7e32a30cd1..125aeb3db0366 100644 --- a/src/test/ui/lint/lint-qualification.stderr +++ b/src/test/ui/lint/lint-qualification.stderr @@ -1,5 +1,5 @@ error: unnecessary qualification - --> $DIR/lint-qualification.rs:9:5 + --> $DIR/lint-qualification.rs:10:5 | LL | foo::bar(); | ^^^^^^^^ diff --git a/src/test/ui/macros/macro-comma-support-rpass.rs b/src/test/ui/macros/macro-comma-support-rpass.rs index 17c27b7cda473..50c0ef3451d3d 100644 --- a/src/test/ui/macros/macro-comma-support-rpass.rs +++ b/src/test/ui/macros/macro-comma-support-rpass.rs @@ -15,6 +15,7 @@ #![cfg_attr(core, no_std)] +#![allow(deprecated)] // for deprecated `try!()` macro #![feature(concat_idents)] #[cfg(std)] use std::fmt; @@ -261,9 +262,7 @@ fn thread_local() { #[test] fn try() { fn inner() -> Result<(), ()> { - #[allow(deprecated)] try!(Ok(())); - #[allow(deprecated)] try!(Ok(()),); Ok(()) } diff --git a/src/test/ui/macros/try-macro.rs b/src/test/ui/macros/try-macro.rs index 643312654493a..824c77d9de528 100644 --- a/src/test/ui/macros/try-macro.rs +++ b/src/test/ui/macros/try-macro.rs @@ -1,5 +1,5 @@ // run-pass -#[allow(deprecated)] +#![allow(deprecated)] // for deprecated `try!()` macro use std::num::{ParseFloatError, ParseIntError}; fn main() { From e9ee2cbc53af43dc007bfd81054fb20e57d5da67 Mon Sep 17 00:00:00 2001 From: Sayan Nandan <17377258+sntdevco@users.noreply.github.com> Date: Fri, 9 Aug 2019 12:47:27 +0530 Subject: [PATCH 020/148] Improve test output for libcore/time --- src/libcore/tests/time.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libcore/tests/time.rs b/src/libcore/tests/time.rs index 09aae4583482f..fac70c468c89d 100644 --- a/src/libcore/tests/time.rs +++ b/src/libcore/tests/time.rs @@ -2,7 +2,7 @@ use core::time::Duration; #[test] fn creation() { - assert!(Duration::from_secs(1) != Duration::from_secs(0)); + assert_ne!(Duration::from_secs(1), Duration::from_secs(0)); assert_eq!(Duration::from_secs(1) + Duration::from_secs(2), Duration::from_secs(3)); assert_eq!(Duration::from_millis(10) + Duration::from_secs(4), @@ -107,14 +107,12 @@ fn checked_sub() { #[test] #[should_panic] -#[cfg(not(miri))] // Miri does not support panics fn sub_bad1() { let _ = Duration::new(0, 0) - Duration::new(0, 1); } #[test] #[should_panic] -#[cfg(not(miri))] // Miri does not support panics fn sub_bad2() { let _ = Duration::new(0, 0) - Duration::new(1, 0); } From 623debfe9dcbb70bcb5fd891db2e861d91e1b8a9 Mon Sep 17 00:00:00 2001 From: Sayan Nandan <17377258+sntdevco@users.noreply.github.com> Date: Fri, 9 Aug 2019 12:51:34 +0530 Subject: [PATCH 021/148] Improve tests for libcore/slice --- src/libcore/tests/slice.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 31d16e0e32057..c036dd8d92f22 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -3,19 +3,19 @@ use core::result::Result::{Ok, Err}; #[test] fn test_position() { let b = [1, 2, 3, 5, 5]; - assert!(b.iter().position(|&v| v == 9) == None); - assert!(b.iter().position(|&v| v == 5) == Some(3)); - assert!(b.iter().position(|&v| v == 3) == Some(2)); - assert!(b.iter().position(|&v| v == 0) == None); + assert_eq!(b.iter().position(|&v| v == 9), None); + assert_eq!(b.iter().position(|&v| v == 5), Some(3)); + assert_eq!(b.iter().position(|&v| v == 3), Some(2)); + assert_eq!(b.iter().position(|&v| v == 0), None); } #[test] fn test_rposition() { let b = [1, 2, 3, 5, 5]; - assert!(b.iter().rposition(|&v| v == 9) == None); - assert!(b.iter().rposition(|&v| v == 5) == Some(4)); - assert!(b.iter().rposition(|&v| v == 3) == Some(2)); - assert!(b.iter().rposition(|&v| v == 0) == None); + assert_eq!(b.iter().rposition(|&v| v == 9), None); + assert_eq!(b.iter().rposition(|&v| v == 5), Some(4)); + assert_eq!(b.iter().rposition(|&v| v == 3), Some(2)); + assert_eq!(b.iter().rposition(|&v| v == 0), None); } #[test] From 33445aea509cadcd715009c79795d289268daa7c Mon Sep 17 00:00:00 2001 From: Sayan Nandan <17377258+sntdevco@users.noreply.github.com> Date: Fri, 9 Aug 2019 12:53:14 +0530 Subject: [PATCH 022/148] Improve tests for liballoc/btree/set --- src/liballoc/tests/btree/set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc/tests/btree/set.rs b/src/liballoc/tests/btree/set.rs index 4f5168f1ce572..9e03137594961 100644 --- a/src/liballoc/tests/btree/set.rs +++ b/src/liballoc/tests/btree/set.rs @@ -10,7 +10,7 @@ fn test_clone_eq() { m.insert(1); m.insert(2); - assert!(m.clone() == m); + assert_eq!(m.clone(), m); } #[test] @@ -28,7 +28,7 @@ fn test_hash() { y.insert(2); y.insert(1); - assert!(hash(&x) == hash(&y)); + assert_eq!(hash(&x), hash(&y)); } fn check(a: &[i32], b: &[i32], expected: &[i32], f: F) From c5687e3940faaa398843e8e9e9c8f9a00a267b0b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 10:07:59 +0200 Subject: [PATCH 023/148] enable flt2dec tests in Miri --- src/libcore/tests/num/flt2dec/estimator.rs | 7 +++++- src/libcore/tests/num/flt2dec/mod.rs | 16 +++++++++++-- src/libcore/tests/num/flt2dec/random.rs | 23 +++++++++++++++---- .../tests/num/flt2dec/strategy/dragon.rs | 1 + .../tests/num/flt2dec/strategy/grisu.rs | 1 + 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/libcore/tests/num/flt2dec/estimator.rs b/src/libcore/tests/num/flt2dec/estimator.rs index 2dbb8e3a5f06e..50fe61b5e2267 100644 --- a/src/libcore/tests/num/flt2dec/estimator.rs +++ b/src/libcore/tests/num/flt2dec/estimator.rs @@ -42,7 +42,12 @@ fn test_estimate_scaling_factor() { assert_almost_eq!(estimate_scaling_factor(1, -1074), -323); assert_almost_eq!(estimate_scaling_factor(0x1fffffffffffff, 971), 309); - for i in -1074..972 { + #[cfg(not(miri))] // Miri is too slow + let iter = -1074..972; + #[cfg(miri)] + let iter = (-1074..972).step_by(11); + + for i in iter { let expected = super::ldexp_f64(1.0, i).log10().ceil(); assert_almost_eq!(estimate_scaling_factor(1, i as i16), expected as i16); } diff --git a/src/libcore/tests/num/flt2dec/mod.rs b/src/libcore/tests/num/flt2dec/mod.rs index f42f500c2df1d..c41d35efced6c 100644 --- a/src/libcore/tests/num/flt2dec/mod.rs +++ b/src/libcore/tests/num/flt2dec/mod.rs @@ -1,5 +1,3 @@ -#![cfg(not(miri))] // Miri does not implement ldexp, which most tests here need - use std::prelude::v1::*; use std::{str, i16, f32, f64, fmt}; @@ -257,6 +255,7 @@ pub fn f32_shortest_sanity_test(mut f: F) where F: FnMut(&Decoded, &mut [u8]) check_shortest!(f(minf32) => b"1", -44); } +#[cfg(not(miri))] // Miri is too slow pub fn f32_exact_sanity_test(mut f: F) where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) { let minf32 = ldexp_f32(1.0, -149); @@ -362,6 +361,7 @@ pub fn f64_shortest_sanity_test(mut f: F) where F: FnMut(&Decoded, &mut [u8]) check_shortest!(f(minf64) => b"5", -323); } +#[cfg(not(miri))] // Miri is too slow pub fn f64_exact_sanity_test(mut f: F) where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) { let minf64 = ldexp_f64(1.0, -1074); @@ -553,6 +553,10 @@ pub fn to_shortest_str_test(mut f_: F) assert_eq!(to_string(f, minf64, Minus, 324, false), format!("0.{:0>323}5", "")); assert_eq!(to_string(f, minf64, Minus, 325, false), format!("0.{:0>323}50", "")); + if cfg!(miri) { // Miri is too slow + return; + } + // very large output assert_eq!(to_string(f, 1.1, Minus, 80000, false), format!("1.1{:0>79999}", "")); } @@ -807,6 +811,10 @@ pub fn to_exact_exp_str_test(mut f_: F) "1.401298464324817070923729583289916131280261941876515771757068283\ 8897910826858606014866381883621215820312500000000000000000000000e-45"); + if cfg!(miri) { // Miri is too slow + return; + } + assert_eq!(to_string(f, f64::MAX, Minus, 1, false), "2e308"); assert_eq!(to_string(f, f64::MAX, Minus, 2, false), "1.8e308"); assert_eq!(to_string(f, f64::MAX, Minus, 4, false), "1.798e308"); @@ -1040,6 +1048,10 @@ pub fn to_exact_fixed_str_test(mut f_: F) assert_eq!(to_string(f, f32::MAX, Minus, 2, false), "340282346638528859811704183484516925440.00"); + if cfg!(miri) { // Miri is too slow + return; + } + let minf32 = ldexp_f32(1.0, -149); assert_eq!(to_string(f, minf32, Minus, 0, false), "0"); assert_eq!(to_string(f, minf32, Minus, 1, false), "0.0"); diff --git a/src/libcore/tests/num/flt2dec/random.rs b/src/libcore/tests/num/flt2dec/random.rs index d56787b2819a7..3db5b4c84fc3b 100644 --- a/src/libcore/tests/num/flt2dec/random.rs +++ b/src/libcore/tests/num/flt2dec/random.rs @@ -109,8 +109,13 @@ pub fn f32_exhaustive_equivalence_test(f: F, g: G, k: usize) #[test] fn shortest_random_equivalence_test() { use core::num::flt2dec::strategy::dragon::format_shortest as fallback; - f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, 10_000); - f32_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, 10_000); + #[cfg(not(miri))] // Miri is too slow + const N: usize = 10_000; + #[cfg(miri)] + const N: usize = 20; + + f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, N); + f32_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, N); } #[test] #[ignore] // it is too expensive @@ -138,17 +143,27 @@ fn shortest_f64_hard_random_equivalence_test() { #[test] fn exact_f32_random_equivalence_test() { use core::num::flt2dec::strategy::dragon::format_exact as fallback; + #[cfg(not(miri))] // Miri is too slow + const N: usize = 1_000; + #[cfg(miri)] + const N: usize = 10; + for k in 1..21 { f32_random_equivalence_test(|d, buf| format_exact_opt(d, buf, i16::MIN), - |d, buf| fallback(d, buf, i16::MIN), k, 1_000); + |d, buf| fallback(d, buf, i16::MIN), k, N); } } #[test] fn exact_f64_random_equivalence_test() { use core::num::flt2dec::strategy::dragon::format_exact as fallback; + #[cfg(not(miri))] // Miri is too slow + const N: usize = 1_000; + #[cfg(miri)] + const N: usize = 5; + for k in 1..21 { f64_random_equivalence_test(|d, buf| format_exact_opt(d, buf, i16::MIN), - |d, buf| fallback(d, buf, i16::MIN), k, 1_000); + |d, buf| fallback(d, buf, i16::MIN), k, N); } } diff --git a/src/libcore/tests/num/flt2dec/strategy/dragon.rs b/src/libcore/tests/num/flt2dec/strategy/dragon.rs index 5e4cc23d33c8c..dc4d78bfae109 100644 --- a/src/libcore/tests/num/flt2dec/strategy/dragon.rs +++ b/src/libcore/tests/num/flt2dec/strategy/dragon.rs @@ -23,6 +23,7 @@ fn shortest_sanity_test() { } #[test] +#[cfg(not(miri))] // Miri is too slow fn exact_sanity_test() { // This test ends up running what I can only assume is some corner-ish case // of the `exp2` library function, defined in whatever C runtime we're diff --git a/src/libcore/tests/num/flt2dec/strategy/grisu.rs b/src/libcore/tests/num/flt2dec/strategy/grisu.rs index f1afd7d4bf86f..f8bdddfe2e410 100644 --- a/src/libcore/tests/num/flt2dec/strategy/grisu.rs +++ b/src/libcore/tests/num/flt2dec/strategy/grisu.rs @@ -36,6 +36,7 @@ fn shortest_sanity_test() { } #[test] +#[cfg(not(miri))] // Miri is too slow fn exact_sanity_test() { // See comments in dragon.rs's exact_sanity_test for why this test is // ignored on MSVC From c7e16c5f47ac86877ab6c52db61709349e4cf276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Tue, 6 Aug 2019 14:55:57 +0200 Subject: [PATCH 024/148] Check links on all platforms when running locally --- src/bootstrap/tool.rs | 26 ++++++++++++++++++++--- src/ci/docker/x86_64-gnu-tools/Dockerfile | 3 +++ src/tools/rustbook/Cargo.toml | 7 +++--- src/tools/rustbook/src/main.rs | 13 ++++++------ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs index 15a329a5b9152..df7eb7c455d02 100644 --- a/src/bootstrap/tool.rs +++ b/src/bootstrap/tool.rs @@ -9,7 +9,7 @@ use build_helper::t; use crate::Mode; use crate::Compiler; use crate::builder::{Step, RunConfig, ShouldRun, Builder}; -use crate::util::{exe, add_lib_path}; +use crate::util::{exe, add_lib_path, CiEnv}; use crate::compile; use crate::channel::GitInfo; use crate::channel; @@ -279,11 +279,26 @@ pub fn prepare_tool_cargo( cargo } +fn rustbook_features() -> Vec { + let mut features = Vec::new(); + + // Due to CI budged and risk of spurious failures we want to limit jobs running this check. + // At same time local builds should run it regardless of the platform. + // `CiEnv::None` means it's local build and `CHECK_LINKS` is defined in x86_64-gnu-tools to + // explicitly enable it on single job + if CiEnv::current() == CiEnv::None || env::var("CHECK_LINKS").is_ok() { + features.push("linkcheck".to_string()); + } + + features +} + macro_rules! bootstrap_tool { ($( $name:ident, $path:expr, $tool_name:expr $(,llvm_tools = $llvm:expr)* $(,is_external_tool = $external:expr)* + $(,features = $features:expr)* ; )+) => { #[derive(Copy, PartialEq, Eq, Clone)] @@ -350,7 +365,12 @@ macro_rules! bootstrap_tool { } else { SourceType::InTree }, - extra_features: Vec::new(), + extra_features: { + // FIXME(#60643): avoid this lint by using `_` + let mut _tmp = Vec::new(); + $(_tmp.extend($features);)* + _tmp + }, }).expect("expected to build -- essential tool") } } @@ -359,7 +379,7 @@ macro_rules! bootstrap_tool { } bootstrap_tool!( - Rustbook, "src/tools/rustbook", "rustbook"; + Rustbook, "src/tools/rustbook", "rustbook", features = rustbook_features(); UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen"; Tidy, "src/tools/tidy", "tidy"; Linkchecker, "src/tools/linkchecker", "linkchecker"; diff --git a/src/ci/docker/x86_64-gnu-tools/Dockerfile b/src/ci/docker/x86_64-gnu-tools/Dockerfile index f11ae7a34cb91..8035195c6ed0a 100644 --- a/src/ci/docker/x86_64-gnu-tools/Dockerfile +++ b/src/ci/docker/x86_64-gnu-tools/Dockerfile @@ -21,6 +21,9 @@ COPY x86_64-gnu-tools/checkregression.py /tmp/ COPY x86_64-gnu-tools/checktools.sh /tmp/ COPY x86_64-gnu-tools/repo.sh /tmp/ +# Run rustbook with `linkcheck` feature enabled +ENV CHECK_LINKS 1 + ENV RUST_CONFIGURE_ARGS \ --build=x86_64-unknown-linux-gnu \ --save-toolstates=/tmp/toolstates.json diff --git a/src/tools/rustbook/Cargo.toml b/src/tools/rustbook/Cargo.toml index bb10d06851b5a..a7188f0d11eac 100644 --- a/src/tools/rustbook/Cargo.toml +++ b/src/tools/rustbook/Cargo.toml @@ -5,14 +5,15 @@ version = "0.1.0" license = "MIT OR Apache-2.0" edition = "2018" +[features] +linkcheck = ["mdbook-linkcheck"] + [dependencies] clap = "2.25.0" failure = "0.1" +mdbook-linkcheck = { version = "0.3.0", optional = true } [dependencies.mdbook] version = "0.3.0" default-features = false features = ["search"] - -[target.'cfg(all(target_arch = "x86_64", target_os = "linux"))'.dependencies] -mdbook-linkcheck = "0.3.0" diff --git a/src/tools/rustbook/src/main.rs b/src/tools/rustbook/src/main.rs index 6bce7c3a978eb..95530b210afd6 100644 --- a/src/tools/rustbook/src/main.rs +++ b/src/tools/rustbook/src/main.rs @@ -8,10 +8,9 @@ use clap::{App, ArgMatches, SubCommand, AppSettings}; use mdbook::MDBook; use mdbook::errors::{Result as Result3}; -#[cfg(all(target_arch = "x86_64", target_os = "linux"))] +#[cfg(feature = "linkcheck")] use mdbook::renderer::RenderContext; - -#[cfg(all(target_arch = "x86_64", target_os = "linux"))] +#[cfg(feature = "linkcheck")] use mdbook_linkcheck::{self, errors::BrokenLinks}; use failure::Error; @@ -52,7 +51,7 @@ fn main() { if let Err(err) = linkcheck(sub_matches) { eprintln!("Error: {}", err); - #[cfg(all(target_arch = "x86_64", target_os = "linux"))] + #[cfg(feature = "linkcheck")] { if let Ok(broken_links) = err.downcast::() { for cause in broken_links.links().iter() { @@ -68,7 +67,7 @@ fn main() { }; } -#[cfg(all(target_arch = "x86_64", target_os = "linux"))] +#[cfg(feature = "linkcheck")] pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> { let book_dir = get_book_dir(args); let book = MDBook::load(&book_dir).unwrap(); @@ -78,9 +77,9 @@ pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> { mdbook_linkcheck::check_links(&render_ctx) } -#[cfg(not(all(target_arch = "x86_64", target_os = "linux")))] +#[cfg(not(feature = "linkcheck"))] pub fn linkcheck(_args: &ArgMatches<'_>) -> Result<(), Error> { - println!("mdbook-linkcheck only works on x86_64 linux targets."); + println!("mdbook-linkcheck is disabled."); Ok(()) } From 29ca428ffa753a300b880ee11e132ead54f4dbb7 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 11:18:17 +0200 Subject: [PATCH 025/148] Miri is really slow --- src/libcore/tests/num/dec2flt/mod.rs | 1 + src/libcore/tests/num/flt2dec/estimator.rs | 2 +- src/libcore/tests/num/flt2dec/random.rs | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcore/tests/num/dec2flt/mod.rs b/src/libcore/tests/num/dec2flt/mod.rs index 0e71426c64108..46eacb4200acc 100644 --- a/src/libcore/tests/num/dec2flt/mod.rs +++ b/src/libcore/tests/num/dec2flt/mod.rs @@ -77,6 +77,7 @@ fn infinity() { fn zero() { test_literal!(0.0); test_literal!(1e-325); + #[cfg(not(miri))] // Miri is too slow test_literal!(1e-326); #[cfg(not(miri))] // Miri is too slow test_literal!(1e-500); diff --git a/src/libcore/tests/num/flt2dec/estimator.rs b/src/libcore/tests/num/flt2dec/estimator.rs index 50fe61b5e2267..c51451708f3ce 100644 --- a/src/libcore/tests/num/flt2dec/estimator.rs +++ b/src/libcore/tests/num/flt2dec/estimator.rs @@ -45,7 +45,7 @@ fn test_estimate_scaling_factor() { #[cfg(not(miri))] // Miri is too slow let iter = -1074..972; #[cfg(miri)] - let iter = (-1074..972).step_by(11); + let iter = (-1074..972).step_by(37); for i in iter { let expected = super::ldexp_f64(1.0, i).log10().ceil(); diff --git a/src/libcore/tests/num/flt2dec/random.rs b/src/libcore/tests/num/flt2dec/random.rs index 3db5b4c84fc3b..d9543793397bf 100644 --- a/src/libcore/tests/num/flt2dec/random.rs +++ b/src/libcore/tests/num/flt2dec/random.rs @@ -112,7 +112,7 @@ fn shortest_random_equivalence_test() { #[cfg(not(miri))] // Miri is too slow const N: usize = 10_000; #[cfg(miri)] - const N: usize = 20; + const N: usize = 10; f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, N); f32_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, N); @@ -146,7 +146,7 @@ fn exact_f32_random_equivalence_test() { #[cfg(not(miri))] // Miri is too slow const N: usize = 1_000; #[cfg(miri)] - const N: usize = 10; + const N: usize = 3; for k in 1..21 { f32_random_equivalence_test(|d, buf| format_exact_opt(d, buf, i16::MIN), @@ -160,7 +160,7 @@ fn exact_f64_random_equivalence_test() { #[cfg(not(miri))] // Miri is too slow const N: usize = 1_000; #[cfg(miri)] - const N: usize = 5; + const N: usize = 3; for k in 1..21 { f64_random_equivalence_test(|d, buf| format_exact_opt(d, buf, i16::MIN), From 3d231acceeb6a1af8108577b65bd60745f7dcf17 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Fri, 9 Aug 2019 00:33:57 +0200 Subject: [PATCH 026/148] Add missing #![feature(associated_type_bounds)] --- src/liballoc/tests/lib.rs | 1 + src/libcore/iter/traits/collect.rs | 2 ++ src/libstd/lib.rs | 1 + 3 files changed, 4 insertions(+) diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index 6d774f3fecd92..5723a30c0f34f 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -8,6 +8,7 @@ #![feature(trusted_len)] #![feature(try_reserve)] #![feature(unboxed_closures)] +#![feature(associated_type_bounds)] use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; diff --git a/src/libcore/iter/traits/collect.rs b/src/libcore/iter/traits/collect.rs index 9f15de33425f4..015184cbba254 100644 --- a/src/libcore/iter/traits/collect.rs +++ b/src/libcore/iter/traits/collect.rs @@ -194,6 +194,8 @@ pub trait FromIterator: Sized { /// `Item`: /// /// ```rust +/// #![feature(associated_type_bounds)] +/// /// fn collect_as_strings(collection: T) -> Vec /// where /// T: IntoIterator, diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index cfee49a7b555c..10e3c34506311 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -238,6 +238,7 @@ #![feature(arbitrary_self_types)] #![feature(array_error_internals)] #![feature(asm)] +#![feature(associated_type_bounds)] #![feature(bind_by_move_pattern_guards)] #![feature(box_syntax)] #![feature(c_variadic)] From 73edef7fdc1caa72fbceb773743a2c4b31162601 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 11:43:25 +0200 Subject: [PATCH 027/148] reduce some test sizes in Miri --- src/liballoc/tests/btree/map.rs | 3 +++ src/liballoc/tests/btree/set.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/liballoc/tests/btree/map.rs b/src/liballoc/tests/btree/map.rs index 844afe870766b..266a0d055d5bc 100644 --- a/src/liballoc/tests/btree/map.rs +++ b/src/liballoc/tests/btree/map.rs @@ -689,7 +689,10 @@ fn test_split_off_empty_left() { #[test] fn test_split_off_large_random_sorted() { + #[cfg(not(miri))] // Miri is too slow let mut data = rand_data(1529); + #[cfg(miri)] + let mut data = rand_data(529); // special case with maximum height. data.sort(); diff --git a/src/liballoc/tests/btree/set.rs b/src/liballoc/tests/btree/set.rs index 989beb3b1bfd9..b3e85ce186082 100644 --- a/src/liballoc/tests/btree/set.rs +++ b/src/liballoc/tests/btree/set.rs @@ -69,6 +69,11 @@ fn test_intersection() { check_intersection(&[11, 1, 3, 77, 103, 5, -5], &[2, 11, 77, -9, -42, 5, 3], &[3, 5, 11, 77]); + + if cfg!(miri) { // Miri is too slow + return; + } + let large = (0..1000).collect::>(); check_intersection(&[], &large, &[]); check_intersection(&large, &[], &[]); @@ -98,6 +103,11 @@ fn test_difference() { check_difference(&[-5, 11, 22, 33, 40, 42], &[-12, -5, 14, 23, 34, 38, 39, 50], &[11, 22, 33, 40, 42]); + + if cfg!(miri) { // Miri is too slow + return; + } + let large = (0..1000).collect::>(); check_difference(&[], &large, &[]); check_difference(&[-1], &large, &[-1]); @@ -166,6 +176,17 @@ fn test_is_subset() { assert_eq!(is_subset(&[1, 2], &[1]), false); assert_eq!(is_subset(&[1, 2], &[1, 2]), true); assert_eq!(is_subset(&[1, 2], &[2, 3]), false); + assert_eq!(is_subset(&[-5, 11, 22, 33, 40, 42], + &[-12, -5, 14, 23, 11, 34, 22, 38, 33, 42, 39, 40]), + true); + assert_eq!(is_subset(&[-5, 11, 22, 33, 40, 42], + &[-12, -5, 14, 23, 34, 38, 22, 11]), + false); + + if cfg!(miri) { // Miri is too slow + return; + } + let large = (0..1000).collect::>(); assert_eq!(is_subset(&[], &large), true); assert_eq!(is_subset(&large, &[]), false); @@ -371,7 +392,10 @@ fn test_split_off_empty_left() { #[test] fn test_split_off_large_random_sorted() { + #[cfg(not(miri))] // Miri is too slow let mut data = rand_data(1529); + #[cfg(miri)] + let mut data = rand_data(529); // special case with maximum height. data.sort(); From 77bfd7fd1a939638a19ca30efad767c81bdd3d8b Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Fri, 9 Aug 2019 13:40:54 +0200 Subject: [PATCH 028/148] Don't use associated type bounds in docs until it is stable --- src/libcore/iter/traits/collect.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libcore/iter/traits/collect.rs b/src/libcore/iter/traits/collect.rs index 015184cbba254..25439136b8538 100644 --- a/src/libcore/iter/traits/collect.rs +++ b/src/libcore/iter/traits/collect.rs @@ -194,11 +194,10 @@ pub trait FromIterator: Sized { /// `Item`: /// /// ```rust -/// #![feature(associated_type_bounds)] -/// /// fn collect_as_strings(collection: T) -> Vec /// where -/// T: IntoIterator, +/// T: IntoIterator, +/// T::Item: std::fmt::Debug, /// { /// collection /// .into_iter() From 78caca00d7112c0216c52e86833e93a63085dcd1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 13:55:22 +0200 Subject: [PATCH 029/148] explain Miri disabling --- src/libcore/tests/slice.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 1ef77f87ab7e2..a0426c0d4fe8a 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -1153,7 +1153,7 @@ fn test_rotate_right() { } #[test] -#[cfg(not(miri))] +#[cfg(not(miri))] // Miri is too slow fn brute_force_rotate_test_0() { // In case of edge cases involving multiple algorithms let n = 300; From b81e887b24c6754df44485b82fefe92ad32b1a46 Mon Sep 17 00:00:00 2001 From: sd234678 Date: Fri, 9 Aug 2019 15:14:05 +0100 Subject: [PATCH 030/148] Remove meaningless comments in src/test --- src/test/pretty/stmt_expr_attributes.rs | 2 -- .../associated-type-projection-from-supertrait.rs | 8 -------- ...ciated-types-binding-to-type-defined-in-supertrait.rs | 6 ------ .../associated-types/associated-types-ref-from-struct.rs | 4 ---- .../ui/higher-rank-trait-bounds/hrtb-type-outlives.rs | 2 -- src/test/ui/hrtb/hrtb-conflate-regions.rs | 1 - src/test/ui/impl-trait/bound-normalization-fail.rs | 2 -- src/test/ui/impl-trait/bound-normalization-pass.rs | 4 ---- src/test/ui/issues/issue-12028.rs | 2 -- src/test/ui/issues/issue-16739.rs | 4 ---- src/test/ui/methods/method-projection.rs | 9 --------- .../regions-outlives-projection-container-hrtb.rs | 7 ------- .../regions/regions-outlives-projection-container-wc.rs | 4 ---- .../ui/regions/regions-outlives-projection-container.rs | 4 ---- .../defaultimpl/specialization-no-default.rs | 8 -------- src/test/ui/specialization/specialization-no-default.rs | 8 -------- src/test/ui/traits/traits-conditional-model-fn.rs | 6 ------ 17 files changed, 81 deletions(-) diff --git a/src/test/pretty/stmt_expr_attributes.rs b/src/test/pretty/stmt_expr_attributes.rs index 02d93238dd643..619cce685d75f 100644 --- a/src/test/pretty/stmt_expr_attributes.rs +++ b/src/test/pretty/stmt_expr_attributes.rs @@ -259,8 +259,6 @@ fn _12() { } } -///////////////// - fn foo() { } fn foo3(_: i32, _: (), _: ()) { } fn qux(_: i32) { } diff --git a/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs b/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs index 06dfe490b8bd3..7e05bcd309a4f 100644 --- a/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs +++ b/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs @@ -12,30 +12,22 @@ pub trait Car : Vehicle { fn chip_paint(&self, c: Self::Color) { } } -/////////////////////////////////////////////////////////////////////////// - struct Black; struct ModelT; impl Vehicle for ModelT { type Color = Black; } impl Car for ModelT { } -/////////////////////////////////////////////////////////////////////////// - struct Blue; struct ModelU; impl Vehicle for ModelU { type Color = Blue; } impl Car for ModelU { } -/////////////////////////////////////////////////////////////////////////// - fn dent(c: C, color: C::Color) { c.chip_paint(color) } fn a() { dent(ModelT, Black); } fn b() { dent(ModelT, Blue); } //~ ERROR mismatched types fn c() { dent(ModelU, Black); } //~ ERROR mismatched types fn d() { dent(ModelU, Blue); } -/////////////////////////////////////////////////////////////////////////// - fn e() { ModelT.chip_paint(Black); } fn f() { ModelT.chip_paint(Blue); } //~ ERROR mismatched types fn g() { ModelU.chip_paint(Black); } //~ ERROR mismatched types diff --git a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs index 653130843c8de..6b2bbbe2e4fb9 100644 --- a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs +++ b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs @@ -11,22 +11,16 @@ pub trait Car : Vehicle { fn honk(&self) { } } -/////////////////////////////////////////////////////////////////////////// - struct Black; struct ModelT; impl Vehicle for ModelT { type Color = Black; } impl Car for ModelT { } -/////////////////////////////////////////////////////////////////////////// - struct Blue; struct ModelU; impl Vehicle for ModelU { type Color = Blue; } impl Car for ModelU { } -/////////////////////////////////////////////////////////////////////////// - fn black_car>(c: C) { } diff --git a/src/test/ui/associated-types/associated-types-ref-from-struct.rs b/src/test/ui/associated-types/associated-types-ref-from-struct.rs index 3ccba289e4b0a..c89f6046e6bf2 100644 --- a/src/test/ui/associated-types/associated-types-ref-from-struct.rs +++ b/src/test/ui/associated-types/associated-types-ref-from-struct.rs @@ -9,8 +9,6 @@ trait Test { fn test(&self, value: &Self::V) -> bool; } -/////////////////////////////////////////////////////////////////////////// - struct TesterPair { tester: T, value: T::V, @@ -26,8 +24,6 @@ impl TesterPair { } } -/////////////////////////////////////////////////////////////////////////// - struct EqU32(u32); impl Test for EqU32 { type V = u32; diff --git a/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs b/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs index a8f38180cc29d..88d396101dba2 100644 --- a/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs +++ b/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs @@ -14,7 +14,6 @@ fn want_foo() { } -/////////////////////////////////////////////////////////////////////////// // Expressed as a where clause struct SomeStruct { @@ -30,7 +29,6 @@ fn one() { want_foo::>(); } -/////////////////////////////////////////////////////////////////////////// // Expressed as shorthand struct AnotherStruct { diff --git a/src/test/ui/hrtb/hrtb-conflate-regions.rs b/src/test/ui/hrtb/hrtb-conflate-regions.rs index 391303676d784..004d62ac513ff 100644 --- a/src/test/ui/hrtb/hrtb-conflate-regions.rs +++ b/src/test/ui/hrtb/hrtb-conflate-regions.rs @@ -15,7 +15,6 @@ fn want_foo1() { } -/////////////////////////////////////////////////////////////////////////// // Expressed as a where clause struct SomeStruct; diff --git a/src/test/ui/impl-trait/bound-normalization-fail.rs b/src/test/ui/impl-trait/bound-normalization-fail.rs index c33261bfd0909..44103554102df 100644 --- a/src/test/ui/impl-trait/bound-normalization-fail.rs +++ b/src/test/ui/impl-trait/bound-normalization-fail.rs @@ -7,7 +7,6 @@ // See issue 60414 -///////////////////////////////////////////// // Reduction to `impl Trait` struct Foo(T); @@ -32,7 +31,6 @@ mod impl_trait { } } -///////////////////////////////////////////// // Same with lifetimes in the trait mod lifetimes { diff --git a/src/test/ui/impl-trait/bound-normalization-pass.rs b/src/test/ui/impl-trait/bound-normalization-pass.rs index 5b634e3106e3b..b0ed4be54b899 100644 --- a/src/test/ui/impl-trait/bound-normalization-pass.rs +++ b/src/test/ui/impl-trait/bound-normalization-pass.rs @@ -8,7 +8,6 @@ // See issue 60414 -///////////////////////////////////////////// // Reduction to `impl Trait` struct Foo(T); @@ -32,7 +31,6 @@ mod impl_trait { } } -///////////////////////////////////////////// // Same with lifetimes in the trait mod lifetimes { @@ -59,7 +57,6 @@ mod lifetimes { } } -///////////////////////////////////////////// // Reduction using `impl Trait` in bindings mod impl_trait_in_bindings { @@ -80,7 +77,6 @@ mod impl_trait_in_bindings { } } -///////////////////////////////////////////// // The same applied to `type Foo = impl Bar`s mod opaque_types { diff --git a/src/test/ui/issues/issue-12028.rs b/src/test/ui/issues/issue-12028.rs index d55354529a9b4..7c2b0d69c8b25 100644 --- a/src/test/ui/issues/issue-12028.rs +++ b/src/test/ui/issues/issue-12028.rs @@ -17,8 +17,6 @@ trait StreamHasher { fn stream(&self) -> Self::S; } -////////////////////////////////////////////////////////////////////////////// - trait StreamHash: Hash { fn input_stream(&self, stream: &mut H::S); } diff --git a/src/test/ui/issues/issue-16739.rs b/src/test/ui/issues/issue-16739.rs index 54ad8fd076e4e..94da2ca5cab81 100644 --- a/src/test/ui/issues/issue-16739.rs +++ b/src/test/ui/issues/issue-16739.rs @@ -16,8 +16,6 @@ impl FnOnce<()> for Foo { extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) } } -///////////////////////////////////////////////////////////////////////// - impl FnMut<(u32,)> for Foo { extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x } } @@ -27,8 +25,6 @@ impl FnOnce<(u32,)> for Foo { extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) } } -///////////////////////////////////////////////////////////////////////// - impl FnMut<(u32,u32)> for Foo { extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y } } diff --git a/src/test/ui/methods/method-projection.rs b/src/test/ui/methods/method-projection.rs index cf33d53968b72..21d983f192ab6 100644 --- a/src/test/ui/methods/method-projection.rs +++ b/src/test/ui/methods/method-projection.rs @@ -2,9 +2,6 @@ // Test that we can use method notation to call methods based on a // projection bound from a trait. Issue #20469. -/////////////////////////////////////////////////////////////////////////// - - trait MakeString { fn make_string(&self) -> String; } @@ -21,8 +18,6 @@ impl MakeString for usize { } } -/////////////////////////////////////////////////////////////////////////// - trait Foo { type F: MakeString; @@ -33,8 +28,6 @@ fn foo(f: &F) -> String { f.get().make_string() } -/////////////////////////////////////////////////////////////////////////// - struct SomeStruct { field: isize, } @@ -47,8 +40,6 @@ impl Foo for SomeStruct { } } -/////////////////////////////////////////////////////////////////////////// - struct SomeOtherStruct { field: usize, } diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs b/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs index 407a4fdf59bb7..cee741184ca2a 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs @@ -6,9 +6,6 @@ #![allow(dead_code)] - -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait<'b> { type TheAssocType; } @@ -21,8 +18,6 @@ impl<'a,'b> TheTrait<'a> for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithHrAssoc where for<'a> T : TheTrait<'a> { @@ -37,8 +32,6 @@ fn with_assoc<'a,'b>() { //[nll]~^^ ERROR lifetime may not live long enough } -/////////////////////////////////////////////////////////////////////////// - pub trait TheSubTrait : for<'a> TheTrait<'a> { } diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.rs b/src/test/ui/regions/regions-outlives-projection-container-wc.rs index 5037ea536dae9..99965f333907b 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.rs +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.rs @@ -8,8 +8,6 @@ #![allow(dead_code)] -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait { type TheAssocType; } @@ -22,8 +20,6 @@ impl<'b> TheTrait for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithAssoc where T : TheTrait { m: [T; 0] } diff --git a/src/test/ui/regions/regions-outlives-projection-container.rs b/src/test/ui/regions/regions-outlives-projection-container.rs index 78305c0693905..3afc600becb6e 100644 --- a/src/test/ui/regions/regions-outlives-projection-container.rs +++ b/src/test/ui/regions/regions-outlives-projection-container.rs @@ -5,8 +5,6 @@ #![allow(dead_code)] #![feature(rustc_attrs)] -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait { type TheAssocType; } @@ -19,8 +17,6 @@ impl<'b> TheTrait for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithAssoc { m: [T; 0] } diff --git a/src/test/ui/specialization/defaultimpl/specialization-no-default.rs b/src/test/ui/specialization/defaultimpl/specialization-no-default.rs index 7ea79a9a7bf82..37005f839d488 100644 --- a/src/test/ui/specialization/defaultimpl/specialization-no-default.rs +++ b/src/test/ui/specialization/defaultimpl/specialization-no-default.rs @@ -3,9 +3,7 @@ // Check a number of scenarios in which one impl tries to override another, // without correctly using `default`. -//////////////////////////////////////////////////////////////////////////////// // Test 1: one layer of specialization, multiple methods, missing `default` -//////////////////////////////////////////////////////////////////////////////// trait Foo { fn foo(&self); @@ -25,9 +23,7 @@ impl Foo for u32 { fn bar(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 2: one layer of specialization, missing `default` on associated type -//////////////////////////////////////////////////////////////////////////////// trait Bar { type T; @@ -41,9 +37,7 @@ impl Bar for u8 { type T = (); //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3a: multiple layers of specialization, missing interior `default` -//////////////////////////////////////////////////////////////////////////////// trait Baz { fn baz(&self); @@ -61,10 +55,8 @@ impl Baz for i32 { fn baz(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3b: multiple layers of specialization, missing interior `default`, // redundant `default` in bottom layer. -//////////////////////////////////////////////////////////////////////////////// trait Redundant { fn redundant(&self); diff --git a/src/test/ui/specialization/specialization-no-default.rs b/src/test/ui/specialization/specialization-no-default.rs index 29afbbd9bf267..57346b26d24ec 100644 --- a/src/test/ui/specialization/specialization-no-default.rs +++ b/src/test/ui/specialization/specialization-no-default.rs @@ -3,9 +3,7 @@ // Check a number of scenarios in which one impl tries to override another, // without correctly using `default`. -//////////////////////////////////////////////////////////////////////////////// // Test 1: one layer of specialization, multiple methods, missing `default` -//////////////////////////////////////////////////////////////////////////////// trait Foo { fn foo(&self); @@ -25,9 +23,7 @@ impl Foo for u32 { fn bar(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 2: one layer of specialization, missing `default` on associated type -//////////////////////////////////////////////////////////////////////////////// trait Bar { type T; @@ -41,9 +37,7 @@ impl Bar for u8 { type T = (); //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3a: multiple layers of specialization, missing interior `default` -//////////////////////////////////////////////////////////////////////////////// trait Baz { fn baz(&self); @@ -61,10 +55,8 @@ impl Baz for i32 { fn baz(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3b: multiple layers of specialization, missing interior `default`, // redundant `default` in bottom layer. -//////////////////////////////////////////////////////////////////////////////// trait Redundant { fn redundant(&self); diff --git a/src/test/ui/traits/traits-conditional-model-fn.rs b/src/test/ui/traits/traits-conditional-model-fn.rs index 27ce6d93a8195..afdfb96394bd0 100644 --- a/src/test/ui/traits/traits-conditional-model-fn.rs +++ b/src/test/ui/traits/traits-conditional-model-fn.rs @@ -14,8 +14,6 @@ use go_trait::{Go, GoMut, GoOnce, go, go_mut, go_once}; use std::rc::Rc; use std::cell::Cell; -/////////////////////////////////////////////////////////////////////////// - struct SomeGoableThing { counter: Rc> } @@ -26,8 +24,6 @@ impl Go for SomeGoableThing { } } -/////////////////////////////////////////////////////////////////////////// - struct SomeGoOnceableThing { counter: Rc> } @@ -38,8 +34,6 @@ impl GoOnce for SomeGoOnceableThing { } } -/////////////////////////////////////////////////////////////////////////// - fn main() { let counter = Rc::new(Cell::new(0)); let mut x = SomeGoableThing { counter: counter.clone() }; From c076392143e483f60429d148ac58522e423eea9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 14:20:39 -0700 Subject: [PATCH 031/148] Tweak mismatched types error on break expressions --- src/librustc_typeck/check/expr.rs | 16 ++++++++++- src/test/ui/issues/issue-27042.stderr | 9 ++++--- src/test/ui/loops/loop-break-value.stderr | 18 ++++++++----- .../ui/loops/loop-labeled-break-value.stderr | 27 ++++++++++++------- .../ui/loops/loop-properly-diverging-2.stderr | 9 ++++--- 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index d825358beaade..c567741be3980 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -548,7 +548,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { coerce.coerce(self, &cause, e, e_ty); } else { assert!(e_ty.is_unit()); - coerce.coerce_forced_unit(self, &cause, &mut |_| (), true); + let ty = coerce.expected_ty(); + coerce.coerce_forced_unit(self, &cause, &mut |err| { + let msg = "give it a value of the expected type"; + let label = destination.label + .map(|l| format!(" {}", l.ident)) + .unwrap_or_else(String::new); + let sugg = format!("break{} {}", label, match ty.sty { + ty::Bool => "true", + ty::Char => "'a'", + ty::Int(_) | ty::Uint(_) => "42", + ty::Float(_) => "3.14159", + _ => "value", + }); + err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders); + }, false); } } else { // If `ctxt.coerce` is `None`, we can just ignore diff --git a/src/test/ui/issues/issue-27042.stderr b/src/test/ui/issues/issue-27042.stderr index 4beb752854b9c..7678984a518ba 100644 --- a/src/test/ui/issues/issue-27042.stderr +++ b/src/test/ui/issues/issue-27042.stderr @@ -12,10 +12,13 @@ error[E0308]: mismatched types --> $DIR/issue-27042.rs:6:16 | LL | loop { break }; - | ^^^^^ expected (), found i32 + | ^^^^^ + | | + | expected i32, found () + | help: give it a value of the expected type: `break 42` | - = note: expected type `()` - found type `i32` + = note: expected type `i32` + found type `()` error[E0308]: mismatched types --> $DIR/issue-27042.rs:8:9 diff --git a/src/test/ui/loops/loop-break-value.stderr b/src/test/ui/loops/loop-break-value.stderr index 1e167905ec8d7..7310790b880c2 100644 --- a/src/test/ui/loops/loop-break-value.stderr +++ b/src/test/ui/loops/loop-break-value.stderr @@ -90,10 +90,13 @@ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:4:31 | LL | let val: ! = loop { break break; }; - | ^^^^^ expected (), found ! + | ^^^^^ + | | + | expected !, found () + | help: give it a value of the expected type: `break value` | - = note: expected type `()` - found type `!` + = note: expected type `!` + found type `()` error[E0308]: mismatched types --> $DIR/loop-break-value.rs:11:19 @@ -153,10 +156,13 @@ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:90:9 | LL | break; - | ^^^^^ expected (), found integer + | ^^^^^ + | | + | expected integer, found () + | help: give it a value of the expected type: `break value` | - = note: expected type `()` - found type `{integer}` + = note: expected type `{integer}` + found type `()` error: aborting due to 16 previous errors diff --git a/src/test/ui/loops/loop-labeled-break-value.stderr b/src/test/ui/loops/loop-labeled-break-value.stderr index ad7cb4b0c2e5f..8b9468cacc1f9 100644 --- a/src/test/ui/loops/loop-labeled-break-value.stderr +++ b/src/test/ui/loops/loop-labeled-break-value.stderr @@ -2,28 +2,37 @@ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:3:29 | LL | let _: i32 = loop { break }; - | ^^^^^ expected (), found i32 + | ^^^^^ + | | + | expected i32, found () + | help: give it a value of the expected type: `break 42` | - = note: expected type `()` - found type `i32` + = note: expected type `i32` + found type `()` error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:6:37 | LL | let _: i32 = 'inner: loop { break 'inner }; - | ^^^^^^^^^^^^ expected (), found i32 + | ^^^^^^^^^^^^ + | | + | expected i32, found () + | help: give it a value of the expected type: `break 'inner 42` | - = note: expected type `()` - found type `i32` + = note: expected type `i32` + found type `()` error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:9:45 | LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } }; - | ^^^^^^^^^^^^^ expected (), found i32 + | ^^^^^^^^^^^^^ + | | + | expected i32, found () + | help: give it a value of the expected type: `break 'inner2 42` | - = note: expected type `()` - found type `i32` + = note: expected type `i32` + found type `()` error: aborting due to 3 previous errors diff --git a/src/test/ui/loops/loop-properly-diverging-2.stderr b/src/test/ui/loops/loop-properly-diverging-2.stderr index 6293fdb058a0f..3758bbf9f6f31 100644 --- a/src/test/ui/loops/loop-properly-diverging-2.stderr +++ b/src/test/ui/loops/loop-properly-diverging-2.stderr @@ -2,10 +2,13 @@ error[E0308]: mismatched types --> $DIR/loop-properly-diverging-2.rs:2:23 | LL | let x: i32 = loop { break }; - | ^^^^^ expected (), found i32 + | ^^^^^ + | | + | expected i32, found () + | help: give it a value of the expected type: `break 42` | - = note: expected type `()` - found type `i32` + = note: expected type `i32` + found type `()` error: aborting due to previous error From 799b13ada59d0663e98a02744d21d2c3b08501ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 17:24:39 -0700 Subject: [PATCH 032/148] Do not suggest using ! with break --- src/librustc_typeck/check/expr.rs | 1 + src/test/ui/loops/loop-break-value.stderr | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index c567741be3980..5c859b9fd3254 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -559,6 +559,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::Char => "'a'", ty::Int(_) | ty::Uint(_) => "42", ty::Float(_) => "3.14159", + ty::Error | ty::Never => return, _ => "value", }); err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders); diff --git a/src/test/ui/loops/loop-break-value.stderr b/src/test/ui/loops/loop-break-value.stderr index 7310790b880c2..fef5b5873068f 100644 --- a/src/test/ui/loops/loop-break-value.stderr +++ b/src/test/ui/loops/loop-break-value.stderr @@ -90,10 +90,7 @@ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:4:31 | LL | let val: ! = loop { break break; }; - | ^^^^^ - | | - | expected !, found () - | help: give it a value of the expected type: `break value` + | ^^^^^ expected !, found () | = note: expected type `!` found type `()` From 4fbbf99c509042e89572f8575c875889874edb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 22:20:42 -0700 Subject: [PATCH 033/148] Be more accurate when mentioning type of found match arms --- src/librustc/infer/error_reporting/mod.rs | 38 +++++-------------- .../ui/match/match-arm-resolving-to-never.rs | 19 ++++++++++ .../match/match-arm-resolving-to-never.stderr | 22 +++++++++++ 3 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 src/test/ui/match/match-arm-resolving-to-never.rs create mode 100644 src/test/ui/match/match-arm-resolving-to-never.stderr diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index 8d0ead5c8fe90..2ffcd2c4ace7b 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -662,19 +662,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } } _ => { + // `last_ty` can be `!`, `expected` will have better info when present. + let t = self.resolve_vars_if_possible(&match exp_found { + Some(ty::error::ExpectedFound { expected, .. }) => expected, + _ => last_ty, + }); let msg = "`match` arms have incompatible types"; err.span_label(cause.span, msg); if prior_arms.len() <= 4 { for sp in prior_arms { - err.span_label(*sp, format!( - "this is found to be of type `{}`", - self.resolve_vars_if_possible(&last_ty), - )); + err.span_label( *sp, format!("this is found to be of type `{}`", t)); } } else if let Some(sp) = prior_arms.last() { - err.span_label(*sp, format!( - "this and all prior arms are found to be of type `{}`", last_ty, - )); + err.span_label( + *sp, + format!("this and all prior arms are found to be of type `{}`", t), + ); } } }, @@ -1143,27 +1146,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } (_, false, _) => { if let Some(exp_found) = exp_found { - let (def_id, ret_ty) = match exp_found.found.sty { - ty::FnDef(def, _) => { - (Some(def), Some(self.tcx.fn_sig(def).output())) - } - _ => (None, None), - }; - - let exp_is_struct = match exp_found.expected.sty { - ty::Adt(def, _) => def.is_struct(), - _ => false, - }; - - if let (Some(def_id), Some(ret_ty)) = (def_id, ret_ty) { - if exp_is_struct && &exp_found.expected == ret_ty.skip_binder() { - let message = format!( - "did you mean `{}(/* fields */)`?", - self.tcx.def_path_str(def_id) - ); - diag.span_label(span, message); - } - } self.suggest_as_ref_where_appropriate(span, &exp_found, diag); } diff --git a/src/test/ui/match/match-arm-resolving-to-never.rs b/src/test/ui/match/match-arm-resolving-to-never.rs new file mode 100644 index 0000000000000..8f54023305e83 --- /dev/null +++ b/src/test/ui/match/match-arm-resolving-to-never.rs @@ -0,0 +1,19 @@ +enum E { + A, + B, + C, + D, + E, + F, +} + +fn main() { + match E::F { + E::A => 1, + E::B => 2, + E::C => 3, + E::D => 4, + E::E => unimplemented!(""), + E::F => "", //~ ERROR match arms have incompatible types + }; +} diff --git a/src/test/ui/match/match-arm-resolving-to-never.stderr b/src/test/ui/match/match-arm-resolving-to-never.stderr new file mode 100644 index 0000000000000..24ce97f86e760 --- /dev/null +++ b/src/test/ui/match/match-arm-resolving-to-never.stderr @@ -0,0 +1,22 @@ +error[E0308]: match arms have incompatible types + --> $DIR/match-arm-resolving-to-never.rs:17:17 + | +LL | / match E::F { +LL | | E::A => 1, +LL | | E::B => 2, +LL | | E::C => 3, +LL | | E::D => 4, +LL | | E::E => unimplemented!(""), + | | ------------------ this and all prior arms are found to be of type `{integer}` +LL | | E::F => "", + | | ^^ expected integer, found reference +LL | | }; + | |_____- `match` arms have incompatible types + | + = note: expected type `{integer}` + found type `&'static str` + = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 01a61394f8d69c1720fcdf77eaac635600ff87e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 22:22:04 -0700 Subject: [PATCH 034/148] Change wording for function without return value Fix #62677 --- src/librustc_typeck/check/mod.rs | 2 +- .../ui/block-result/consider-removing-last-semi.stderr | 4 ++-- src/test/ui/block-result/issue-11714.stderr | 2 +- src/test/ui/block-result/issue-13428.stderr | 4 ++-- .../coercion/coercion-missing-tail-expected-type.stderr | 4 ++-- src/test/ui/issues/issue-32323.stderr | 2 +- src/test/ui/issues/issue-43162.stderr | 2 +- src/test/ui/issues/issue-44023.stderr | 2 +- src/test/ui/issues/issue-6458-4.stderr | 2 +- src/test/ui/liveness/liveness-forgot-ret.stderr | 2 +- src/test/ui/liveness/liveness-missing-ret2.stderr | 2 +- .../ui/liveness/liveness-return-last-stmt-semi.stderr | 8 ++++---- src/test/ui/missing/missing-return.stderr | 2 +- src/test/ui/parser/issue-62881.stderr | 2 +- src/test/ui/parser/issue-62895.stderr | 2 +- 15 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 08033b46b8004..da9b02fcf513a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3709,7 +3709,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.consider_hint_about_removing_semicolon(blk, expected_ty, err); } if let Some(fn_span) = fn_span { - err.span_label(fn_span, "this function's body doesn't return"); + err.span_label(fn_span, "this function's body doesn't return a value"); } }, false); } diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr index 618d020ce08b5..7f8bb67c946e6 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.stderr +++ b/src/test/ui/block-result/consider-removing-last-semi.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | 0u8; LL | "bla".to_string(); | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn g() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | "this won't work".to_string(); LL | "removeme".to_string(); | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-11714.stderr b/src/test/ui/block-result/issue-11714.stderr index d73489a602df4..569e012540c4f 100644 --- a/src/test/ui/block-result/issue-11714.stderr +++ b/src/test/ui/block-result/issue-11714.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn blah() -> i32 { | ---- ^^^ expected i32, found () | | - | this function's body doesn't return + | this function's body doesn't return a value ... LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-13428.stderr b/src/test/ui/block-result/issue-13428.stderr index 18adb15c9615d..ca48910026d9a 100644 --- a/src/test/ui/block-result/issue-13428.stderr +++ b/src/test/ui/block-result/issue-13428.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value ... LL | ; | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn bar() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | "foobar".to_string() LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr index 057de5b625e87..a7ac930ba670e 100644 --- a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr +++ b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn plus_one(x: i32) -> i32 { | -------- ^^^ expected i32, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | x + 1; | - help: consider removing this semicolon | @@ -17,7 +17,7 @@ error[E0308]: mismatched types LL | fn foo() -> Result { | --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | Ok(1); | - help: consider removing this semicolon | diff --git a/src/test/ui/issues/issue-32323.stderr b/src/test/ui/issues/issue-32323.stderr index 0339fdc55b9c8..6b8bca078b5e6 100644 --- a/src/test/ui/issues/issue-32323.stderr +++ b/src/test/ui/issues/issue-32323.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | pub fn f<'a, T: Tr<'a>>() -> >::Out {} | - ^^^^^^^^^^^^^^^^^^ expected associated type, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `>::Out` found type `()` diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr index cd11959ede6cb..fd62ac30ab195 100644 --- a/src/test/ui/issues/issue-43162.stderr +++ b/src/test/ui/issues/issue-43162.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn foo() -> bool { | --- ^^^^ expected bool, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | LL | break true; | - help: consider removing this semicolon diff --git a/src/test/ui/issues/issue-44023.stderr b/src/test/ui/issues/issue-44023.stderr index 153be363c1dea..cad202129a719 100644 --- a/src/test/ui/issues/issue-44023.stderr +++ b/src/test/ui/issues/issue-44023.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { | ------------------------ ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/issues/issue-6458-4.stderr b/src/test/ui/issues/issue-6458-4.stderr index 90b493e1634c4..d4f5b2ba41f69 100644 --- a/src/test/ui/issues/issue-6458-4.stderr +++ b/src/test/ui/issues/issue-6458-4.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo(b: bool) -> Result { | --- ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | Err("bar".to_string()); | - help: consider removing this semicolon | diff --git a/src/test/ui/liveness/liveness-forgot-ret.stderr b/src/test/ui/liveness/liveness-forgot-ret.stderr index a970b80fdbbd9..6d917393663b0 100644 --- a/src/test/ui/liveness/liveness-forgot-ret.stderr +++ b/src/test/ui/liveness/liveness-forgot-ret.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; } | - ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-missing-ret2.stderr b/src/test/ui/liveness/liveness-missing-ret2.stderr index ab7d411880bba..a71b9296702b7 100644 --- a/src/test/ui/liveness/liveness-missing-ret2.stderr +++ b/src/test/ui/liveness/liveness-missing-ret2.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr index a5d9734c069ec..3a08699170643 100644 --- a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr +++ b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr @@ -5,7 +5,7 @@ LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } } | --- ^^^ - help: consider removing this semicolon | | | | | expected i32, found () - | this function's body doesn't return + | this function's body doesn't return a value ... LL | test!(); | -------- in this macro invocation @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn no_return() -> i32 {} | --------- ^^^ expected i32, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `i32` found type `()` @@ -30,7 +30,7 @@ error[E0308]: mismatched types LL | fn bar(x: u32) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't return + | this function's body doesn't return a value LL | x * 2; | - help: consider removing this semicolon | @@ -43,7 +43,7 @@ error[E0308]: mismatched types LL | fn baz(x: u64) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `u32` found type `()` diff --git a/src/test/ui/missing/missing-return.stderr b/src/test/ui/missing/missing-return.stderr index 42466e2fc6574..c7d7116a9cd7c 100644 --- a/src/test/ui/missing/missing-return.stderr +++ b/src/test/ui/missing/missing-return.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { } | - ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62881.stderr b/src/test/ui/parser/issue-62881.stderr index 85c3575fd9288..b2e2da35a2700 100644 --- a/src/test/ui/parser/issue-62881.stderr +++ b/src/test/ui/parser/issue-62881.stderr @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { fn f() -> isize {} pub f< | - ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62895.stderr b/src/test/ui/parser/issue-62895.stderr index 7def7b562ca59..6c3a8fb766c0a 100644 --- a/src/test/ui/parser/issue-62895.stderr +++ b/src/test/ui/parser/issue-62895.stderr @@ -38,7 +38,7 @@ error[E0308]: mismatched types LL | fn v() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't return + | this function's body doesn't return a value | = note: expected type `isize` found type `()` From 94fe8a3c1761acadd5043b24ad821b0b369922ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 22:29:10 -0700 Subject: [PATCH 035/148] Suggest calling function on type error when finding bare fn --- src/librustc_typeck/check/mod.rs | 43 ++++++++++++++++++++ src/test/ui/issues/issue-35241.stderr | 5 ++- src/test/ui/resolve/privacy-enum-ctor.stderr | 24 +++++++++-- src/test/ui/substs-ppaux.normal.stderr | 32 +++++++++++++-- src/test/ui/substs-ppaux.verbose.stderr | 32 +++++++++++++-- 5 files changed, 124 insertions(+), 12 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index da9b02fcf513a..6600f83393857 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3819,6 +3819,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pointing_at_return_type } + fn suggest_fn_call( + &self, + err: &mut DiagnosticBuilder<'tcx>, + expr: &hir::Expr, + expected: Ty<'tcx>, + found: Ty<'tcx>, + ) -> bool { + if let ty::FnDef(..) | ty::FnPtr(_) = &found.sty { + let sig = found.fn_sig(self.tcx); + let sig = self + .replace_bound_vars_with_fresh_vars(expr.span, infer::FnCall, &sig) + .0; + let sig = self.normalize_associated_types_in(expr.span, &sig); + if let Ok(_) = self.try_coerce(expr, sig.output(), expected, AllowTwoPhase::No) { + if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) { + err.span_suggestion(expr.span, "use parentheses to call this function", format!( + "{}({})", + code, + if sig.inputs().len() > 0 { + "..." + } else { + "" + }), if sig.inputs().len() > 0 { + Applicability::MachineApplicable + } else { + Applicability::HasPlaceholders + } + ); + return true; + } + } + } + false + } + pub fn suggest_ref_or_into( &self, err: &mut DiagnosticBuilder<'tcx>, @@ -3833,6 +3868,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { suggestion, Applicability::MachineApplicable, ); + } else if let (ty::FnDef(def_id, ..), true) = ( + &found.sty, + self.suggest_fn_call(err, expr, expected, found), + ) { + if let Some(sp) = self.tcx.hir().span_if_local(*def_id) { + let sp = self.sess().source_map().def_span(sp); + err.span_label(sp, &format!("{} defined here", found)); + } } else if !self.check_for_cast(err, expr, found, expected) { let is_struct_pat_shorthand_field = self.is_hir_id_from_struct_pattern_shorthand_field( expr.hir_id, diff --git a/src/test/ui/issues/issue-35241.stderr b/src/test/ui/issues/issue-35241.stderr index 8fda58abadb6e..248e6f0f46b19 100644 --- a/src/test/ui/issues/issue-35241.stderr +++ b/src/test/ui/issues/issue-35241.stderr @@ -1,11 +1,14 @@ error[E0308]: mismatched types --> $DIR/issue-35241.rs:3:20 | +LL | struct Foo(u32); + | ---------------- fn(u32) -> Foo {Foo} defined here +LL | LL | fn test() -> Foo { Foo } | --- ^^^ | | | | | expected struct `Foo`, found fn item - | | did you mean `Foo(/* fields */)`? + | | help: use parentheses to call this function: `Foo(...)` | expected `Foo` because of return type | = note: expected type `Foo` diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr index a1a8714ab3f38..07cbffb009406 100644 --- a/src/test/ui/resolve/privacy-enum-ctor.stderr +++ b/src/test/ui/resolve/privacy-enum-ctor.stderr @@ -195,8 +195,14 @@ LL | let _: Z = m::n::Z::Unit {}; error[E0308]: mismatched types --> $DIR/privacy-enum-ctor.rs:27:20 | +LL | Fn(u8), + | ------ fn(u8) -> m::n::Z {m::n::Z::Fn} defined here +... LL | let _: Z = Z::Fn; - | ^^^^^ expected enum `m::n::Z`, found fn item + | ^^^^^ + | | + | expected enum `m::n::Z`, found fn item + | help: use parentheses to call this function: `Z::Fn(...)` | = note: expected type `m::n::Z` found type `fn(u8) -> m::n::Z {m::n::Z::Fn}` @@ -219,8 +225,14 @@ LL | let _ = Z::Unit; error[E0308]: mismatched types --> $DIR/privacy-enum-ctor.rs:43:16 | +LL | Fn(u8), + | ------ fn(u8) -> m::E {m::E::Fn} defined here +... LL | let _: E = m::E::Fn; - | ^^^^^^^^ expected enum `m::E`, found fn item + | ^^^^^^^^ + | | + | expected enum `m::E`, found fn item + | help: use parentheses to call this function: `m::E::Fn(...)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` @@ -243,8 +255,14 @@ LL | let _: E = m::E::Unit; error[E0308]: mismatched types --> $DIR/privacy-enum-ctor.rs:51:16 | +LL | Fn(u8), + | ------ fn(u8) -> m::E {m::E::Fn} defined here +... LL | let _: E = E::Fn; - | ^^^^^ expected enum `m::E`, found fn item + | ^^^^^ + | | + | expected enum `m::E`, found fn item + | help: use parentheses to call this function: `E::Fn(...)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` diff --git a/src/test/ui/substs-ppaux.normal.stderr b/src/test/ui/substs-ppaux.normal.stderr index 123dd86b90549..b3b879ef9acbe 100644 --- a/src/test/ui/substs-ppaux.normal.stderr +++ b/src/test/ui/substs-ppaux.normal.stderr @@ -1,8 +1,14 @@ error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:16:17 | +LL | fn bar<'a, T>() where T: 'a {} + | --------------------------- fn() {>::bar::<'static, char>} defined here +... LL | let x: () = >::bar::<'static, char>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::bar::<'static, char>()` | = note: expected type `()` found type `fn() {>::bar::<'static, char>}` @@ -10,8 +16,14 @@ LL | let x: () = >::bar::<'static, char>; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:25:17 | +LL | fn bar<'a, T>() where T: 'a {} + | --------------------------- fn() {>::bar::<'static, char>} defined here +... LL | let x: () = >::bar::<'static, char>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::bar::<'static, char>()` | = note: expected type `()` found type `fn() {>::bar::<'static, char>}` @@ -19,8 +31,14 @@ LL | let x: () = >::bar::<'static, char>; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:33:17 | +LL | fn baz() {} + | -------- fn() {>::baz} defined here +... LL | let x: () = >::baz; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::baz()` | = note: expected type `()` found type `fn() {>::baz}` @@ -28,8 +46,14 @@ LL | let x: () = >::baz; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:41:17 | +LL | fn foo<'z>() where &'z (): Sized { + | -------------------------------- fn() {foo::<'static>} defined here +... LL | let x: () = foo::<'static>; - | ^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `foo::<'static>()` | = note: expected type `()` found type `fn() {foo::<'static>}` diff --git a/src/test/ui/substs-ppaux.verbose.stderr b/src/test/ui/substs-ppaux.verbose.stderr index 9167346282bab..363018db232d8 100644 --- a/src/test/ui/substs-ppaux.verbose.stderr +++ b/src/test/ui/substs-ppaux.verbose.stderr @@ -1,8 +1,14 @@ error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:16:17 | +LL | fn bar<'a, T>() where T: 'a {} + | --------------------------- fn() {>::bar::} defined here +... LL | let x: () = >::bar::<'static, char>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::bar::<'static, char>()` | = note: expected type `()` found type `fn() {>::bar::}` @@ -10,8 +16,14 @@ LL | let x: () = >::bar::<'static, char>; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:25:17 | +LL | fn bar<'a, T>() where T: 'a {} + | --------------------------- fn() {>::bar::} defined here +... LL | let x: () = >::bar::<'static, char>; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::bar::<'static, char>()` | = note: expected type `()` found type `fn() {>::bar::}` @@ -19,8 +31,14 @@ LL | let x: () = >::bar::<'static, char>; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:33:17 | +LL | fn baz() {} + | -------- fn() {>::baz} defined here +... LL | let x: () = >::baz; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `>::baz()` | = note: expected type `()` found type `fn() {>::baz}` @@ -28,8 +46,14 @@ LL | let x: () = >::baz; error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:41:17 | +LL | fn foo<'z>() where &'z (): Sized { + | -------------------------------- fn() {foo::} defined here +... LL | let x: () = foo::<'static>; - | ^^^^^^^^^^^^^^ expected (), found fn item + | ^^^^^^^^^^^^^^ + | | + | expected (), found fn item + | help: use parentheses to call this function: `foo::<'static>()` | = note: expected type `()` found type `fn() {foo::}` From 195d837f18e2efb7d621d6568212a59c29fc184e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 12:01:22 -0700 Subject: [PATCH 036/148] When suggesting fn call use an appropriate number of placeholder arguments --- src/librustc_typeck/check/mod.rs | 43 ++++++--- src/test/ui/issues/issue-35241.stderr | 2 +- src/test/ui/resolve/privacy-enum-ctor.stderr | 6 +- .../fn-or-tuple-struct-without-args.rs | 20 ++++ .../fn-or-tuple-struct-without-args.stderr | 93 +++++++++++++++++++ 5 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs create mode 100644 src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 6600f83393857..130cc8f3a6050 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3833,19 +3833,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .0; let sig = self.normalize_associated_types_in(expr.span, &sig); if let Ok(_) = self.try_coerce(expr, sig.output(), expected, AllowTwoPhase::No) { - if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) { - err.span_suggestion(expr.span, "use parentheses to call this function", format!( - "{}({})", - code, - if sig.inputs().len() > 0 { - "..." - } else { - "" - }), if sig.inputs().len() > 0 { - Applicability::MachineApplicable - } else { - Applicability::HasPlaceholders + let (mut sugg_call, applicability) = if sig.inputs().is_empty() { + (String::new(), Applicability::MachineApplicable) + } else { + ("...".to_owned(), Applicability::HasPlaceholders) + }; + let mut msg = "call this function"; + if let ty::FnDef(def_id, ..) = found.sty { + match self.tcx.hir().get_if_local(def_id) { + Some(Node::Item(hir::Item { + node: ItemKind::Fn(.., body_id), + .. + })) => { + let body = self.tcx.hir().body(*body_id); + sugg_call = body.arguments.iter() + .map(|arg| hir::print::to_string( + hir::print::NO_ANN, + |s| s.print_pat(&arg.pat), + )).collect::>().join(", "); + } + Some(Node::Ctor(hir::VariantData::Tuple(field, _))) => { + sugg_call = field.iter().map(|_| "_").collect::>().join(", "); + msg = "instatiate this tuple struct"; } + _ => {} + } + }; + if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) { + err.span_suggestion( + expr.span, + &format!("use parentheses to {}", msg), + format!("{}({})", code, sugg_call), + applicability, ); return true; } diff --git a/src/test/ui/issues/issue-35241.stderr b/src/test/ui/issues/issue-35241.stderr index 248e6f0f46b19..8befc6873857e 100644 --- a/src/test/ui/issues/issue-35241.stderr +++ b/src/test/ui/issues/issue-35241.stderr @@ -8,7 +8,7 @@ LL | fn test() -> Foo { Foo } | --- ^^^ | | | | | expected struct `Foo`, found fn item - | | help: use parentheses to call this function: `Foo(...)` + | | help: use parentheses to instatiate this tuple struct: `Foo(_)` | expected `Foo` because of return type | = note: expected type `Foo` diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr index 07cbffb009406..49092562feeba 100644 --- a/src/test/ui/resolve/privacy-enum-ctor.stderr +++ b/src/test/ui/resolve/privacy-enum-ctor.stderr @@ -202,7 +202,7 @@ LL | let _: Z = Z::Fn; | ^^^^^ | | | expected enum `m::n::Z`, found fn item - | help: use parentheses to call this function: `Z::Fn(...)` + | help: use parentheses to instatiate this tuple struct: `Z::Fn(_)` | = note: expected type `m::n::Z` found type `fn(u8) -> m::n::Z {m::n::Z::Fn}` @@ -232,7 +232,7 @@ LL | let _: E = m::E::Fn; | ^^^^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to call this function: `m::E::Fn(...)` + | help: use parentheses to instatiate this tuple struct: `m::E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` @@ -262,7 +262,7 @@ LL | let _: E = E::Fn; | ^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to call this function: `E::Fn(...)` + | help: use parentheses to instatiate this tuple struct: `E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs new file mode 100644 index 0000000000000..6758c1d5f238e --- /dev/null +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs @@ -0,0 +1,20 @@ +fn foo(a: usize, b: usize) -> usize { a } + +fn bar() -> usize { 42 } + +struct S(usize, usize); +struct V(); + +trait T { + fn baz(x: usize, y: usize) -> usize { x } + fn bat() -> usize { 42 } +} + +fn main() { + let _: usize = foo; //~ ERROR mismatched types + let _: S = S; //~ ERROR mismatched types + let _: usize = bar; //~ ERROR mismatched types + let _: V = V; //~ ERROR mismatched types + let _: usize = T::baz; //~ ERROR mismatched types + let _: usize = T::bat; //~ ERROR mismatched types +} diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr new file mode 100644 index 0000000000000..524c779979e3b --- /dev/null +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr @@ -0,0 +1,93 @@ +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:14:20 + | +LL | fn foo(a: usize, b: usize) -> usize { a } + | ----------------------------------- fn(usize, usize) -> usize {foo} defined here +... +LL | let _: usize = foo; + | ^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `foo(a, b)` + | + = note: expected type `usize` + found type `fn(usize, usize) -> usize {foo}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:15:16 + | +LL | struct S(usize, usize); + | ----------------------- fn(usize, usize) -> S {S} defined here +... +LL | let _: S = S; + | ^ + | | + | expected struct `S`, found fn item + | help: use parentheses to instatiate this tuple struct: `S(_, _)` + | + = note: expected type `S` + found type `fn(usize, usize) -> S {S}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:16:20 + | +LL | fn bar() -> usize { 42 } + | ----------------- fn() -> usize {bar} defined here +... +LL | let _: usize = bar; + | ^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `bar()` + | + = note: expected type `usize` + found type `fn() -> usize {bar}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:17:16 + | +LL | struct V(); + | ----------- fn() -> V {V} defined here +... +LL | let _: V = V; + | ^ + | | + | expected struct `V`, found fn item + | help: use parentheses to instatiate this tuple struct: `V()` + | + = note: expected type `V` + found type `fn() -> V {V}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:18:20 + | +LL | fn baz(x: usize, y: usize) -> usize { x } + | ----------------------------------- fn(usize, usize) -> usize {<_ as T>::baz} defined here +... +LL | let _: usize = T::baz; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `T::baz(...)` + | + = note: expected type `usize` + found type `fn(usize, usize) -> usize {<_ as T>::baz}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:19:20 + | +LL | fn bat() -> usize { 42 } + | ----------------- fn() -> usize {<_ as T>::bat} defined here +... +LL | let _: usize = T::bat; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `T::bat()` + | + = note: expected type `usize` + found type `fn() -> usize {<_ as T>::bat}` + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0308`. From b7f7756566c9e10983ee51bc97afe9852838299a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 12:31:24 -0700 Subject: [PATCH 037/148] Recover parser from `foo(_, _)` --- src/libsyntax/parse/parser.rs | 70 +++++++++++++------ src/test/ui/issues/issue-34334.rs | 1 + src/test/ui/issues/issue-34334.stderr | 10 ++- ...fn-or-tuple-struct-with-underscore-args.rs | 19 +++++ ...r-tuple-struct-with-underscore-args.stderr | 38 ++++++++++ 5 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.rs create mode 100644 src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d85c2df16a350..30e16592113b6 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2052,9 +2052,23 @@ impl<'a> Parser<'a> { while self.token != token::CloseDelim(token::Paren) { es.push(match self.parse_expr() { Ok(es) => es, - Err(err) => { + Err(mut err) => { // recover from parse error in tuple list - return Ok(self.recover_seq_parse_error(token::Paren, lo, Err(err))); + match self.token.kind { + token::Ident(name, false) + if name == kw::Underscore && self.look_ahead(1, |t| { + t == &token::Comma + }) => { + // Special-case handling of `Foo<(_, _, _)>` + err.emit(); + let sp = self.token.span; + self.bump(); + self.mk_expr(sp, ExprKind::Err, ThinVec::new()) + } + _ => return Ok( + self.recover_seq_parse_error(token::Paren, lo, Err(err)), + ), + } } }); recovered = self.expect_one_of( @@ -2456,9 +2470,10 @@ impl<'a> Parser<'a> { } /// Parses `a.b` or `a(13)` or `a[4]` or just `a`. - fn parse_dot_or_call_expr(&mut self, - already_parsed_attrs: Option>) - -> PResult<'a, P> { + fn parse_dot_or_call_expr( + &mut self, + already_parsed_attrs: Option>, + ) -> PResult<'a, P> { let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?; let b = self.parse_bottom_expr(); @@ -2466,16 +2481,16 @@ impl<'a> Parser<'a> { self.parse_dot_or_call_expr_with(b, span, attrs) } - fn parse_dot_or_call_expr_with(&mut self, - e0: P, - lo: Span, - mut attrs: ThinVec) - -> PResult<'a, P> { + fn parse_dot_or_call_expr_with( + &mut self, + e0: P, + lo: Span, + mut attrs: ThinVec, + ) -> PResult<'a, P> { // Stitch the list of outer attributes onto the return value. // A little bit ugly, but the best way given the current code // structure - self.parse_dot_or_call_expr_with_(e0, lo) - .map(|expr| + self.parse_dot_or_call_expr_with_(e0, lo).map(|expr| expr.map(|mut expr| { attrs.extend::>(expr.attrs.into()); expr.attrs = attrs; @@ -2483,10 +2498,7 @@ impl<'a> Parser<'a> { ExprKind::If(..) if !expr.attrs.is_empty() => { // Just point to the first attribute in there... let span = expr.attrs[0].span; - - self.span_err(span, - "attributes are not yet allowed on `if` \ - expressions"); + self.span_err(span, "attributes are not yet allowed on `if` expressions"); } _ => {} } @@ -2624,7 +2636,24 @@ impl<'a> Parser<'a> { } fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec>> { - self.parse_paren_comma_seq(|p| p.parse_expr()).map(|(r, _)| r) + self.parse_paren_comma_seq(|p| { + match p.parse_expr() { + Ok(expr) => Ok(expr), + Err(mut err) => match p.token.kind { + token::Ident(name, false) + if name == kw::Underscore && p.look_ahead(1, |t| { + t == &token::Comma + }) => { + // Special-case handling of `foo(_, _, _)` + err.emit(); + let sp = p.token.span; + p.bump(); + Ok(p.mk_expr(sp, ExprKind::Err, ThinVec::new())) + } + _ => Err(err), + }, + } + }).map(|(r, _)| r) } crate fn process_potential_macro_variable(&mut self) { @@ -2806,9 +2835,10 @@ impl<'a> Parser<'a> { /// This parses an expression accounting for associativity and precedence of the operators in /// the expression. #[inline] - fn parse_assoc_expr(&mut self, - already_parsed_attrs: Option>) - -> PResult<'a, P> { + fn parse_assoc_expr( + &mut self, + already_parsed_attrs: Option>, + ) -> PResult<'a, P> { self.parse_assoc_expr_with(0, already_parsed_attrs.into()) } diff --git a/src/test/ui/issues/issue-34334.rs b/src/test/ui/issues/issue-34334.rs index 928d217441ef2..4457d71cbb4a7 100644 --- a/src/test/ui/issues/issue-34334.rs +++ b/src/test/ui/issues/issue-34334.rs @@ -5,6 +5,7 @@ fn main () { //~| ERROR mismatched types //~| ERROR invalid left-hand side expression //~| ERROR expected expression, found reserved identifier `_` + //~| ERROR expected expression, found reserved identifier `_` let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect(); //~^ ERROR no method named `iter` found for type `()` in the current scope } diff --git a/src/test/ui/issues/issue-34334.stderr b/src/test/ui/issues/issue-34334.stderr index e8386fd8de9e5..7f89caf92abe1 100644 --- a/src/test/ui/issues/issue-34334.stderr +++ b/src/test/ui/issues/issue-34334.stderr @@ -4,6 +4,12 @@ error: expected expression, found reserved identifier `_` LL | let sr: Vec<(u32, _, _) = vec![]; | ^ expected expression +error: expected expression, found reserved identifier `_` + --> $DIR/issue-34334.rs:2:26 + | +LL | let sr: Vec<(u32, _, _) = vec![]; + | ^ expected expression + error: expected one of `,` or `>`, found `=` --> $DIR/issue-34334.rs:2:29 | @@ -36,12 +42,12 @@ LL | let sr: Vec<(u32, _, _) = vec![]; | ^^^^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid error[E0599]: no method named `iter` found for type `()` in the current scope - --> $DIR/issue-34334.rs:8:36 + --> $DIR/issue-34334.rs:9:36 | LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect(); | ^^^^ -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors Some errors have detailed explanations: E0070, E0308, E0423, E0599. For more information about an error, try `rustc --explain E0070`. diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.rs b/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.rs new file mode 100644 index 0000000000000..a8ea3faefe876 --- /dev/null +++ b/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.rs @@ -0,0 +1,19 @@ +fn foo(a: usize, b: usize) -> usize { a } + +struct S(usize, usize); + +trait T { + fn baz(x: usize, y: usize) -> usize { x } +} + +fn main() { + let _: usize = foo(_, _); + //~^ ERROR expected expression + //~| ERROR expected expression + let _: S = S(_, _); + //~^ ERROR expected expression + //~| ERROR expected expression + let _: usize = T::baz(_, _); + //~^ ERROR expected expression + //~| ERROR expected expression +} diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.stderr b/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.stderr new file mode 100644 index 0000000000000..a6d1c4b859f2f --- /dev/null +++ b/src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.stderr @@ -0,0 +1,38 @@ +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:24 + | +LL | let _: usize = foo(_, _); + | ^ expected expression + +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:27 + | +LL | let _: usize = foo(_, _); + | ^ expected expression + +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:13:18 + | +LL | let _: S = S(_, _); + | ^ expected expression + +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:13:21 + | +LL | let _: S = S(_, _); + | ^ expected expression + +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:16:27 + | +LL | let _: usize = T::baz(_, _); + | ^ expected expression + +error: expected expression, found reserved identifier `_` + --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:16:30 + | +LL | let _: usize = T::baz(_, _); + | ^ expected expression + +error: aborting due to 6 previous errors + From 0d53f699eaa7d1fec1e6013a6a42b39992c119d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 12:32:18 -0700 Subject: [PATCH 038/148] review comments --- src/librustc_typeck/check/expr.rs | 13 ++--- src/librustc_typeck/check/mod.rs | 89 +++++++++++++++++-------------- 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 5c859b9fd3254..9680f61d69903 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -550,18 +550,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { assert!(e_ty.is_unit()); let ty = coerce.expected_ty(); coerce.coerce_forced_unit(self, &cause, &mut |err| { - let msg = "give it a value of the expected type"; - let label = destination.label - .map(|l| format!(" {}", l.ident)) - .unwrap_or_else(String::new); - let sugg = format!("break{} {}", label, match ty.sty { + let val = match ty.sty { ty::Bool => "true", ty::Char => "'a'", ty::Int(_) | ty::Uint(_) => "42", ty::Float(_) => "3.14159", ty::Error | ty::Never => return, _ => "value", - }); + }; + let msg = "give it a value of the expected type"; + let label = destination.label + .map(|l| format!(" {}", l.ident)) + .unwrap_or_else(String::new); + let sugg = format!("break{} {}", label, val); err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders); }, false); } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 130cc8f3a6050..be2af2db66d59 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3819,6 +3819,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pointing_at_return_type } + /// When encountering an fn-like ctor that needs to unify with a value, check whether calling + /// the ctor would successfully solve the type mismatch and if so, suggest it: + /// ``` + /// fn foo(x: usize) -> usize { x } + /// let x: usize = foo; // suggest calling the `foo` function: `foo(42)` + /// ``` fn suggest_fn_call( &self, err: &mut DiagnosticBuilder<'tcx>, @@ -3826,48 +3832,51 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected: Ty<'tcx>, found: Ty<'tcx>, ) -> bool { - if let ty::FnDef(..) | ty::FnPtr(_) = &found.sty { - let sig = found.fn_sig(self.tcx); - let sig = self - .replace_bound_vars_with_fresh_vars(expr.span, infer::FnCall, &sig) - .0; - let sig = self.normalize_associated_types_in(expr.span, &sig); - if let Ok(_) = self.try_coerce(expr, sig.output(), expected, AllowTwoPhase::No) { - let (mut sugg_call, applicability) = if sig.inputs().is_empty() { - (String::new(), Applicability::MachineApplicable) - } else { - ("...".to_owned(), Applicability::HasPlaceholders) - }; - let mut msg = "call this function"; - if let ty::FnDef(def_id, ..) = found.sty { - match self.tcx.hir().get_if_local(def_id) { - Some(Node::Item(hir::Item { - node: ItemKind::Fn(.., body_id), - .. - })) => { - let body = self.tcx.hir().body(*body_id); - sugg_call = body.arguments.iter() - .map(|arg| hir::print::to_string( - hir::print::NO_ANN, - |s| s.print_pat(&arg.pat), - )).collect::>().join(", "); - } - Some(Node::Ctor(hir::VariantData::Tuple(field, _))) => { - sugg_call = field.iter().map(|_| "_").collect::>().join(", "); - msg = "instatiate this tuple struct"; - } - _ => {} + match found.sty { + ty::FnDef(..) | ty::FnPtr(_) => {} + _ => return false, + } + + let sig = found.fn_sig(self.tcx); + let sig = self + .replace_bound_vars_with_fresh_vars(expr.span, infer::FnCall, &sig) + .0; + let sig = self.normalize_associated_types_in(expr.span, &sig); + if let Ok(_) = self.try_coerce(expr, sig.output(), expected, AllowTwoPhase::No) { + let (mut sugg_call, applicability) = if sig.inputs().is_empty() { + (String::new(), Applicability::MachineApplicable) + } else { + ("...".to_owned(), Applicability::HasPlaceholders) + }; + let mut msg = "call this function"; + if let ty::FnDef(def_id, ..) = found.sty { + match self.tcx.hir().get_if_local(def_id) { + Some(Node::Item(hir::Item { + node: ItemKind::Fn(.., body_id), + .. + })) => { + let body = self.tcx.hir().body(*body_id); + sugg_call = body.arguments.iter() + .map(|arg| hir::print::to_string( + hir::print::NO_ANN, + |s| s.print_pat(&arg.pat), + )).collect::>().join(", "); } - }; - if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) { - err.span_suggestion( - expr.span, - &format!("use parentheses to {}", msg), - format!("{}({})", code, sugg_call), - applicability, - ); - return true; + Some(Node::Ctor(hir::VariantData::Tuple(field, _))) => { + sugg_call = field.iter().map(|_| "_").collect::>().join(", "); + msg = "instatiate this tuple struct"; + } + _ => {} } + }; + if let Ok(code) = self.sess().source_map().span_to_snippet(expr.span) { + err.span_suggestion( + expr.span, + &format!("use parentheses to {}", msg), + format!("{}({})", code, sugg_call), + applicability, + ); + return true; } } false From efa62d66e36306fba6025528aee381dd165fa8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 14:53:00 -0700 Subject: [PATCH 039/148] Tweak wording of fn without explicit return --- src/librustc_typeck/check/mod.rs | 5 ++++- .../ui/block-result/consider-removing-last-semi.stderr | 4 ++-- src/test/ui/block-result/issue-11714.stderr | 2 +- src/test/ui/block-result/issue-13428.stderr | 4 ++-- .../coercion/coercion-missing-tail-expected-type.stderr | 4 ++-- src/test/ui/issues/issue-32323.stderr | 2 +- src/test/ui/issues/issue-43162.stderr | 2 +- src/test/ui/issues/issue-44023.stderr | 2 +- src/test/ui/issues/issue-6458-4.stderr | 2 +- src/test/ui/liveness/liveness-forgot-ret.stderr | 2 +- src/test/ui/liveness/liveness-missing-ret2.stderr | 2 +- .../ui/liveness/liveness-return-last-stmt-semi.stderr | 8 ++++---- src/test/ui/missing/missing-return.stderr | 2 +- src/test/ui/parser/issue-62881.stderr | 2 +- src/test/ui/parser/issue-62895.stderr | 2 +- 15 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index be2af2db66d59..726922c8e5346 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3709,7 +3709,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.consider_hint_about_removing_semicolon(blk, expected_ty, err); } if let Some(fn_span) = fn_span { - err.span_label(fn_span, "this function's body doesn't return a value"); + err.span_label( + fn_span, + "this function's body doesn't `return` a value", + ); } }, false); } diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr index 7f8bb67c946e6..f3dfc8b12eafe 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.stderr +++ b/src/test/ui/block-result/consider-removing-last-semi.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | 0u8; LL | "bla".to_string(); | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn g() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | "this won't work".to_string(); LL | "removeme".to_string(); | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-11714.stderr b/src/test/ui/block-result/issue-11714.stderr index 569e012540c4f..021a25ece2f4d 100644 --- a/src/test/ui/block-result/issue-11714.stderr +++ b/src/test/ui/block-result/issue-11714.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn blah() -> i32 { | ---- ^^^ expected i32, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value ... LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-13428.stderr b/src/test/ui/block-result/issue-13428.stderr index ca48910026d9a..8fa6ae9c528b4 100644 --- a/src/test/ui/block-result/issue-13428.stderr +++ b/src/test/ui/block-result/issue-13428.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value ... LL | ; | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn bar() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | "foobar".to_string() LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr index a7ac930ba670e..6ecb1d3eaef4a 100644 --- a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr +++ b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn plus_one(x: i32) -> i32 { | -------- ^^^ expected i32, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | x + 1; | - help: consider removing this semicolon | @@ -17,7 +17,7 @@ error[E0308]: mismatched types LL | fn foo() -> Result { | --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | Ok(1); | - help: consider removing this semicolon | diff --git a/src/test/ui/issues/issue-32323.stderr b/src/test/ui/issues/issue-32323.stderr index 6b8bca078b5e6..376676bda7dbf 100644 --- a/src/test/ui/issues/issue-32323.stderr +++ b/src/test/ui/issues/issue-32323.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | pub fn f<'a, T: Tr<'a>>() -> >::Out {} | - ^^^^^^^^^^^^^^^^^^ expected associated type, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `>::Out` found type `()` diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr index fd62ac30ab195..22d347778c8f4 100644 --- a/src/test/ui/issues/issue-43162.stderr +++ b/src/test/ui/issues/issue-43162.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn foo() -> bool { | --- ^^^^ expected bool, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | LL | break true; | - help: consider removing this semicolon diff --git a/src/test/ui/issues/issue-44023.stderr b/src/test/ui/issues/issue-44023.stderr index cad202129a719..7760af8145b07 100644 --- a/src/test/ui/issues/issue-44023.stderr +++ b/src/test/ui/issues/issue-44023.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { | ------------------------ ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/issues/issue-6458-4.stderr b/src/test/ui/issues/issue-6458-4.stderr index d4f5b2ba41f69..e29be76087416 100644 --- a/src/test/ui/issues/issue-6458-4.stderr +++ b/src/test/ui/issues/issue-6458-4.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo(b: bool) -> Result { | --- ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | Err("bar".to_string()); | - help: consider removing this semicolon | diff --git a/src/test/ui/liveness/liveness-forgot-ret.stderr b/src/test/ui/liveness/liveness-forgot-ret.stderr index 6d917393663b0..68cbdb2cfdba6 100644 --- a/src/test/ui/liveness/liveness-forgot-ret.stderr +++ b/src/test/ui/liveness/liveness-forgot-ret.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; } | - ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-missing-ret2.stderr b/src/test/ui/liveness/liveness-missing-ret2.stderr index a71b9296702b7..62e0aa5d29d95 100644 --- a/src/test/ui/liveness/liveness-missing-ret2.stderr +++ b/src/test/ui/liveness/liveness-missing-ret2.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr index 3a08699170643..c5018af74f347 100644 --- a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr +++ b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr @@ -5,7 +5,7 @@ LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } } | --- ^^^ - help: consider removing this semicolon | | | | | expected i32, found () - | this function's body doesn't return a value + | this function's body doesn't `return` a value ... LL | test!(); | -------- in this macro invocation @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn no_return() -> i32 {} | --------- ^^^ expected i32, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `i32` found type `()` @@ -30,7 +30,7 @@ error[E0308]: mismatched types LL | fn bar(x: u32) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value LL | x * 2; | - help: consider removing this semicolon | @@ -43,7 +43,7 @@ error[E0308]: mismatched types LL | fn baz(x: u64) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `u32` found type `()` diff --git a/src/test/ui/missing/missing-return.stderr b/src/test/ui/missing/missing-return.stderr index c7d7116a9cd7c..4cd516ba81d6a 100644 --- a/src/test/ui/missing/missing-return.stderr +++ b/src/test/ui/missing/missing-return.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { } | - ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62881.stderr b/src/test/ui/parser/issue-62881.stderr index b2e2da35a2700..11b8836a89a76 100644 --- a/src/test/ui/parser/issue-62881.stderr +++ b/src/test/ui/parser/issue-62881.stderr @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { fn f() -> isize {} pub f< | - ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62895.stderr b/src/test/ui/parser/issue-62895.stderr index 6c3a8fb766c0a..d9d3baf622708 100644 --- a/src/test/ui/parser/issue-62895.stderr +++ b/src/test/ui/parser/issue-62895.stderr @@ -38,7 +38,7 @@ error[E0308]: mismatched types LL | fn v() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't return a value + | this function's body doesn't `return` a value | = note: expected type `isize` found type `()` From 52da091ee687f3d065713626a10b4cc961f04f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 14:59:24 -0700 Subject: [PATCH 040/148] Differentiate between tuple structs and tuple variants --- src/librustc/hir/map/mod.rs | 2 +- src/librustc_typeck/check/mod.rs | 29 ++++++++----- src/test/ui/resolve/privacy-enum-ctor.stderr | 6 +-- .../fn-or-tuple-struct-without-args.rs | 6 +++ .../fn-or-tuple-struct-without-args.stderr | 41 +++++++++++++++---- 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 0d477ae796822..b85738dd29a6d 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -287,7 +287,7 @@ impl<'hir> Map<'hir> { self.definitions.def_index_to_hir_id(def_id.to_def_id().index) } - fn def_kind(&self, hir_id: HirId) -> Option { + pub fn def_kind(&self, hir_id: HirId) -> Option { let node = if let Some(node) = self.find(hir_id) { node } else { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 726922c8e5346..629b8da355e23 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3839,6 +3839,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty::FnDef(..) | ty::FnPtr(_) => {} _ => return false, } + let hir = self.tcx.hir(); let sig = found.fn_sig(self.tcx); let sig = self @@ -3849,25 +3850,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (mut sugg_call, applicability) = if sig.inputs().is_empty() { (String::new(), Applicability::MachineApplicable) } else { - ("...".to_owned(), Applicability::HasPlaceholders) + ("...".to_string(), Applicability::HasPlaceholders) }; let mut msg = "call this function"; if let ty::FnDef(def_id, ..) = found.sty { - match self.tcx.hir().get_if_local(def_id) { + match hir.get_if_local(def_id) { Some(Node::Item(hir::Item { node: ItemKind::Fn(.., body_id), .. })) => { - let body = self.tcx.hir().body(*body_id); + let body = hir.body(*body_id); sugg_call = body.arguments.iter() - .map(|arg| hir::print::to_string( - hir::print::NO_ANN, - |s| s.print_pat(&arg.pat), - )).collect::>().join(", "); + .map(|arg| match &arg.pat.node { + hir::PatKind::Binding(_, _, ident, None) => ident.to_string(), + _ => "_".to_string(), + }).collect::>().join(", "); } - Some(Node::Ctor(hir::VariantData::Tuple(field, _))) => { - sugg_call = field.iter().map(|_| "_").collect::>().join(", "); - msg = "instatiate this tuple struct"; + Some(Node::Ctor(hir::VariantData::Tuple(fields, _))) => { + sugg_call = fields.iter().map(|_| "_").collect::>().join(", "); + match hir.as_local_hir_id(def_id).and_then(|hir_id| hir.def_kind(hir_id)) { + Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => { + msg = "instatiate this tuple variant"; + } + Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Struct, _)) => { + msg = "instatiate this tuple struct"; + } + _ => {} + } } _ => {} } diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr index 49092562feeba..3e924f7d161bd 100644 --- a/src/test/ui/resolve/privacy-enum-ctor.stderr +++ b/src/test/ui/resolve/privacy-enum-ctor.stderr @@ -202,7 +202,7 @@ LL | let _: Z = Z::Fn; | ^^^^^ | | | expected enum `m::n::Z`, found fn item - | help: use parentheses to instatiate this tuple struct: `Z::Fn(_)` + | help: use parentheses to instatiate this tuple variant: `Z::Fn(_)` | = note: expected type `m::n::Z` found type `fn(u8) -> m::n::Z {m::n::Z::Fn}` @@ -232,7 +232,7 @@ LL | let _: E = m::E::Fn; | ^^^^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to instatiate this tuple struct: `m::E::Fn(_)` + | help: use parentheses to instatiate this tuple variant: `m::E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` @@ -262,7 +262,7 @@ LL | let _: E = E::Fn; | ^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to instatiate this tuple struct: `E::Fn(_)` + | help: use parentheses to instatiate this tuple variant: `E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs index 6758c1d5f238e..838ef78e4fd6a 100644 --- a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs @@ -3,6 +3,10 @@ fn foo(a: usize, b: usize) -> usize { a } fn bar() -> usize { 42 } struct S(usize, usize); +enum E { + A(usize), + B { a: usize }, +} struct V(); trait T { @@ -17,4 +21,6 @@ fn main() { let _: V = V; //~ ERROR mismatched types let _: usize = T::baz; //~ ERROR mismatched types let _: usize = T::bat; //~ ERROR mismatched types + let _: E = E::A; //~ ERROR mismatched types + let _: E = E::B; //~ ERROR expected value, found struct variant `E::B` } diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr index 524c779979e3b..cb128822fcce8 100644 --- a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr @@ -1,5 +1,14 @@ +error[E0423]: expected value, found struct variant `E::B` + --> $DIR/fn-or-tuple-struct-without-args.rs:25:16 + | +LL | let _: E = E::B; + | ^^^- + | | | + | | help: a tuple variant with a similar name exists: `A` + | did you mean `E::B { /* fields */ }`? + error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:14:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:18:20 | LL | fn foo(a: usize, b: usize) -> usize { a } | ----------------------------------- fn(usize, usize) -> usize {foo} defined here @@ -14,7 +23,7 @@ LL | let _: usize = foo; found type `fn(usize, usize) -> usize {foo}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:15:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:19:16 | LL | struct S(usize, usize); | ----------------------- fn(usize, usize) -> S {S} defined here @@ -29,7 +38,7 @@ LL | let _: S = S; found type `fn(usize, usize) -> S {S}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:16:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:20:20 | LL | fn bar() -> usize { 42 } | ----------------- fn() -> usize {bar} defined here @@ -44,7 +53,7 @@ LL | let _: usize = bar; found type `fn() -> usize {bar}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:17:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:21:16 | LL | struct V(); | ----------- fn() -> V {V} defined here @@ -59,7 +68,7 @@ LL | let _: V = V; found type `fn() -> V {V}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:18:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:22:20 | LL | fn baz(x: usize, y: usize) -> usize { x } | ----------------------------------- fn(usize, usize) -> usize {<_ as T>::baz} defined here @@ -74,7 +83,7 @@ LL | let _: usize = T::baz; found type `fn(usize, usize) -> usize {<_ as T>::baz}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:19:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:23:20 | LL | fn bat() -> usize { 42 } | ----------------- fn() -> usize {<_ as T>::bat} defined here @@ -88,6 +97,22 @@ LL | let _: usize = T::bat; = note: expected type `usize` found type `fn() -> usize {<_ as T>::bat}` -error: aborting due to 6 previous errors +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:24:16 + | +LL | A(usize), + | -------- fn(usize) -> E {E::A} defined here +... +LL | let _: E = E::A; + | ^^^^ + | | + | expected enum `E`, found fn item + | help: use parentheses to instatiate this tuple variant: `E::A(_)` + | + = note: expected type `E` + found type `fn(usize) -> E {E::A}` + +error: aborting due to 8 previous errors -For more information about this error, try `rustc --explain E0308`. +Some errors have detailed explanations: E0308, E0423. +For more information about an error, try `rustc --explain E0308`. From 5a54945b6f637eadd4848874db39b49d2764839f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 15:53:32 -0700 Subject: [PATCH 041/148] Extend suggestion support for traits and foreign items --- src/librustc_typeck/check/mod.rs | 25 +++- .../fn-or-tuple-struct-without-args.rs | 21 ++- .../fn-or-tuple-struct-without-args.stderr | 132 ++++++++++++++++-- 3 files changed, 161 insertions(+), 17 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 629b8da355e23..d389563aea639 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3858,11 +3858,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(Node::Item(hir::Item { node: ItemKind::Fn(.., body_id), .. + })) | + Some(Node::ImplItem(hir::ImplItem { + node: hir::ImplItemKind::Method(_, body_id), + .. + })) | + Some(Node::TraitItem(hir::TraitItem { + node: hir::TraitItemKind::Method(.., hir::TraitMethod::Provided(body_id)), + .. })) => { let body = hir.body(*body_id); sugg_call = body.arguments.iter() .map(|arg| match &arg.pat.node { - hir::PatKind::Binding(_, _, ident, None) => ident.to_string(), + hir::PatKind::Binding(_, _, ident, None) + if ident.name != kw::SelfLower => ident.to_string(), _ => "_".to_string(), }).collect::>().join(", "); } @@ -3878,6 +3887,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => {} } } + Some(Node::ForeignItem(hir::ForeignItem { + node: hir::ForeignItemKind::Fn(_, idents, _), + .. + })) | + Some(Node::TraitItem(hir::TraitItem { + node: hir::TraitItemKind::Method(.., hir::TraitMethod::Required(idents)), + .. + })) => sugg_call = idents.iter() + .map(|ident| if ident.name != kw::SelfLower { + ident.to_string() + } else { + "_".to_string() + }).collect::>() + .join(", "), _ => {} } }; diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs index 838ef78e4fd6a..9b6b10748172b 100644 --- a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.rs @@ -11,7 +11,18 @@ struct V(); trait T { fn baz(x: usize, y: usize) -> usize { x } - fn bat() -> usize { 42 } + fn bat(x: usize) -> usize { 42 } + fn bax(x: usize) -> usize { 42 } + fn bach(x: usize) -> usize; + fn ban(&self) -> usize { 42 } + fn bal(&self) -> usize; +} + +struct X; + +impl T for X { + fn bach(x: usize) -> usize { 42 } + fn bal(&self) -> usize { 42 } } fn main() { @@ -23,4 +34,12 @@ fn main() { let _: usize = T::bat; //~ ERROR mismatched types let _: E = E::A; //~ ERROR mismatched types let _: E = E::B; //~ ERROR expected value, found struct variant `E::B` + let _: usize = X::baz; //~ ERROR mismatched types + let _: usize = X::bat; //~ ERROR mismatched types + let _: usize = X::bax; //~ ERROR mismatched types + let _: usize = X::bach; //~ ERROR mismatched types + let _: usize = X::ban; //~ ERROR mismatched types + let _: usize = X::bal; //~ ERROR mismatched types + let _: usize = X.ban; //~ ERROR attempted to take value of method + let _: usize = X.bal; //~ ERROR attempted to take value of method } diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr index cb128822fcce8..e3012144513fd 100644 --- a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr @@ -1,5 +1,5 @@ error[E0423]: expected value, found struct variant `E::B` - --> $DIR/fn-or-tuple-struct-without-args.rs:25:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:36:16 | LL | let _: E = E::B; | ^^^- @@ -8,7 +8,7 @@ LL | let _: E = E::B; | did you mean `E::B { /* fields */ }`? error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:18:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:29:20 | LL | fn foo(a: usize, b: usize) -> usize { a } | ----------------------------------- fn(usize, usize) -> usize {foo} defined here @@ -23,7 +23,7 @@ LL | let _: usize = foo; found type `fn(usize, usize) -> usize {foo}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:19:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:30:16 | LL | struct S(usize, usize); | ----------------------- fn(usize, usize) -> S {S} defined here @@ -38,7 +38,7 @@ LL | let _: S = S; found type `fn(usize, usize) -> S {S}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:20:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:31:20 | LL | fn bar() -> usize { 42 } | ----------------- fn() -> usize {bar} defined here @@ -53,7 +53,7 @@ LL | let _: usize = bar; found type `fn() -> usize {bar}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:21:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:32:16 | LL | struct V(); | ----------- fn() -> V {V} defined here @@ -68,7 +68,7 @@ LL | let _: V = V; found type `fn() -> V {V}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:22:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:33:20 | LL | fn baz(x: usize, y: usize) -> usize { x } | ----------------------------------- fn(usize, usize) -> usize {<_ as T>::baz} defined here @@ -77,28 +77,28 @@ LL | let _: usize = T::baz; | ^^^^^^ | | | expected usize, found fn item - | help: use parentheses to call this function: `T::baz(...)` + | help: use parentheses to call this function: `T::baz(x, y)` | = note: expected type `usize` found type `fn(usize, usize) -> usize {<_ as T>::baz}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:23:20 + --> $DIR/fn-or-tuple-struct-without-args.rs:34:20 | -LL | fn bat() -> usize { 42 } - | ----------------- fn() -> usize {<_ as T>::bat} defined here +LL | fn bat(x: usize) -> usize { 42 } + | ------------------------- fn(usize) -> usize {<_ as T>::bat} defined here ... LL | let _: usize = T::bat; | ^^^^^^ | | | expected usize, found fn item - | help: use parentheses to call this function: `T::bat()` + | help: use parentheses to call this function: `T::bat(x)` | = note: expected type `usize` - found type `fn() -> usize {<_ as T>::bat}` + found type `fn(usize) -> usize {<_ as T>::bat}` error[E0308]: mismatched types - --> $DIR/fn-or-tuple-struct-without-args.rs:24:16 + --> $DIR/fn-or-tuple-struct-without-args.rs:35:16 | LL | A(usize), | -------- fn(usize) -> E {E::A} defined here @@ -112,7 +112,109 @@ LL | let _: E = E::A; = note: expected type `E` found type `fn(usize) -> E {E::A}` -error: aborting due to 8 previous errors +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:37:20 + | +LL | fn baz(x: usize, y: usize) -> usize { x } + | ----------------------------------- fn(usize, usize) -> usize {::baz} defined here +... +LL | let _: usize = X::baz; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::baz(x, y)` + | + = note: expected type `usize` + found type `fn(usize, usize) -> usize {::baz}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:38:20 + | +LL | fn bat(x: usize) -> usize { 42 } + | ------------------------- fn(usize) -> usize {::bat} defined here +... +LL | let _: usize = X::bat; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::bat(x)` + | + = note: expected type `usize` + found type `fn(usize) -> usize {::bat}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:39:20 + | +LL | fn bax(x: usize) -> usize { 42 } + | ------------------------- fn(usize) -> usize {::bax} defined here +... +LL | let _: usize = X::bax; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::bax(x)` + | + = note: expected type `usize` + found type `fn(usize) -> usize {::bax}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:40:20 + | +LL | fn bach(x: usize) -> usize; + | --------------------------- fn(usize) -> usize {::bach} defined here +... +LL | let _: usize = X::bach; + | ^^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::bach(x)` + | + = note: expected type `usize` + found type `fn(usize) -> usize {::bach}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:41:20 + | +LL | fn ban(&self) -> usize { 42 } + | ---------------------- for<'r> fn(&'r X) -> usize {::ban} defined here +... +LL | let _: usize = X::ban; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::ban(_)` + | + = note: expected type `usize` + found type `for<'r> fn(&'r X) -> usize {::ban}` + +error[E0308]: mismatched types + --> $DIR/fn-or-tuple-struct-without-args.rs:42:20 + | +LL | fn bal(&self) -> usize; + | ----------------------- for<'r> fn(&'r X) -> usize {::bal} defined here +... +LL | let _: usize = X::bal; + | ^^^^^^ + | | + | expected usize, found fn item + | help: use parentheses to call this function: `X::bal(_)` + | + = note: expected type `usize` + found type `for<'r> fn(&'r X) -> usize {::bal}` + +error[E0615]: attempted to take value of method `ban` on type `X` + --> $DIR/fn-or-tuple-struct-without-args.rs:43:22 + | +LL | let _: usize = X.ban; + | ^^^ help: use parentheses to call the method: `ban()` + +error[E0615]: attempted to take value of method `bal` on type `X` + --> $DIR/fn-or-tuple-struct-without-args.rs:44:22 + | +LL | let _: usize = X.bal; + | ^^^ help: use parentheses to call the method: `bal()` + +error: aborting due to 16 previous errors -Some errors have detailed explanations: E0308, E0423. +Some errors have detailed explanations: E0308, E0423, E0615. For more information about an error, try `rustc --explain E0308`. From 33d1082d6e3cb9fe5e0fd090f52a3effa9871fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 15:55:18 -0700 Subject: [PATCH 042/148] review comment: review wording or missing return error --- src/librustc_typeck/check/mod.rs | 3 ++- .../ui/block-result/consider-removing-last-semi.stderr | 4 ++-- src/test/ui/block-result/issue-11714.stderr | 2 +- src/test/ui/block-result/issue-13428.stderr | 4 ++-- .../coercion/coercion-missing-tail-expected-type.stderr | 4 ++-- src/test/ui/issues/issue-32323.stderr | 2 +- src/test/ui/issues/issue-43162.stderr | 2 +- src/test/ui/issues/issue-44023.stderr | 2 +- src/test/ui/issues/issue-6458-4.stderr | 2 +- src/test/ui/liveness/liveness-forgot-ret.stderr | 2 +- src/test/ui/liveness/liveness-missing-ret2.stderr | 2 +- .../ui/liveness/liveness-return-last-stmt-semi.stderr | 8 ++++---- src/test/ui/missing/missing-return.stderr | 2 +- src/test/ui/parser/issue-62881.stderr | 2 +- src/test/ui/parser/issue-62895.stderr | 2 +- 15 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index d389563aea639..086f3d419576a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3711,7 +3711,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(fn_span) = fn_span { err.span_label( fn_span, - "this function's body doesn't `return` a value", + "this function implicitly returns `()` as its body has no tail \ + or `return` expression", ); } }, false); diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr index f3dfc8b12eafe..4c421e6ada405 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.stderr +++ b/src/test/ui/block-result/consider-removing-last-semi.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | 0u8; LL | "bla".to_string(); | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn g() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | "this won't work".to_string(); LL | "removeme".to_string(); | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-11714.stderr b/src/test/ui/block-result/issue-11714.stderr index 021a25ece2f4d..a0bdbd693f2cf 100644 --- a/src/test/ui/block-result/issue-11714.stderr +++ b/src/test/ui/block-result/issue-11714.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn blah() -> i32 { | ---- ^^^ expected i32, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression ... LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-13428.stderr b/src/test/ui/block-result/issue-13428.stderr index 8fa6ae9c528b4..53f8eaa1ae227 100644 --- a/src/test/ui/block-result/issue-13428.stderr +++ b/src/test/ui/block-result/issue-13428.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression ... LL | ; | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn bar() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | "foobar".to_string() LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr index 6ecb1d3eaef4a..fde41567d8002 100644 --- a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr +++ b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn plus_one(x: i32) -> i32 { | -------- ^^^ expected i32, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | x + 1; | - help: consider removing this semicolon | @@ -17,7 +17,7 @@ error[E0308]: mismatched types LL | fn foo() -> Result { | --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | Ok(1); | - help: consider removing this semicolon | diff --git a/src/test/ui/issues/issue-32323.stderr b/src/test/ui/issues/issue-32323.stderr index 376676bda7dbf..138b058f44d9e 100644 --- a/src/test/ui/issues/issue-32323.stderr +++ b/src/test/ui/issues/issue-32323.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | pub fn f<'a, T: Tr<'a>>() -> >::Out {} | - ^^^^^^^^^^^^^^^^^^ expected associated type, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `>::Out` found type `()` diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr index 22d347778c8f4..e90531bce3e86 100644 --- a/src/test/ui/issues/issue-43162.stderr +++ b/src/test/ui/issues/issue-43162.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn foo() -> bool { | --- ^^^^ expected bool, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | LL | break true; | - help: consider removing this semicolon diff --git a/src/test/ui/issues/issue-44023.stderr b/src/test/ui/issues/issue-44023.stderr index 7760af8145b07..4a27531b4be8f 100644 --- a/src/test/ui/issues/issue-44023.stderr +++ b/src/test/ui/issues/issue-44023.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { | ------------------------ ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/issues/issue-6458-4.stderr b/src/test/ui/issues/issue-6458-4.stderr index e29be76087416..4db3daede758f 100644 --- a/src/test/ui/issues/issue-6458-4.stderr +++ b/src/test/ui/issues/issue-6458-4.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo(b: bool) -> Result { | --- ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | Err("bar".to_string()); | - help: consider removing this semicolon | diff --git a/src/test/ui/liveness/liveness-forgot-ret.stderr b/src/test/ui/liveness/liveness-forgot-ret.stderr index 68cbdb2cfdba6..24106f775794c 100644 --- a/src/test/ui/liveness/liveness-forgot-ret.stderr +++ b/src/test/ui/liveness/liveness-forgot-ret.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; } | - ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-missing-ret2.stderr b/src/test/ui/liveness/liveness-missing-ret2.stderr index 62e0aa5d29d95..9502e4467499c 100644 --- a/src/test/ui/liveness/liveness-missing-ret2.stderr +++ b/src/test/ui/liveness/liveness-missing-ret2.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr index c5018af74f347..7ee4cb178a6c5 100644 --- a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr +++ b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr @@ -5,7 +5,7 @@ LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } } | --- ^^^ - help: consider removing this semicolon | | | | | expected i32, found () - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression ... LL | test!(); | -------- in this macro invocation @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn no_return() -> i32 {} | --------- ^^^ expected i32, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `i32` found type `()` @@ -30,7 +30,7 @@ error[E0308]: mismatched types LL | fn bar(x: u32) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression LL | x * 2; | - help: consider removing this semicolon | @@ -43,7 +43,7 @@ error[E0308]: mismatched types LL | fn baz(x: u64) -> u32 { | --- ^^^ expected u32, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `u32` found type `()` diff --git a/src/test/ui/missing/missing-return.stderr b/src/test/ui/missing/missing-return.stderr index 4cd516ba81d6a..541e9fa4a4016 100644 --- a/src/test/ui/missing/missing-return.stderr +++ b/src/test/ui/missing/missing-return.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { } | - ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62881.stderr b/src/test/ui/parser/issue-62881.stderr index 11b8836a89a76..40f64ec8fd0e2 100644 --- a/src/test/ui/parser/issue-62881.stderr +++ b/src/test/ui/parser/issue-62881.stderr @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { fn f() -> isize {} pub f< | - ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62895.stderr b/src/test/ui/parser/issue-62895.stderr index d9d3baf622708..4c872d0c9d367 100644 --- a/src/test/ui/parser/issue-62895.stderr +++ b/src/test/ui/parser/issue-62895.stderr @@ -38,7 +38,7 @@ error[E0308]: mismatched types LL | fn v() -> isize { | - ^^^^^ expected isize, found () | | - | this function's body doesn't `return` a value + | this function implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` From bc1a4f52e3bcb092142267c7d1e6b90b718d557e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 16:56:57 -0700 Subject: [PATCH 043/148] review comments: typo and rewording --- src/librustc_typeck/check/mod.rs | 8 ++++---- .../ui/block-result/consider-removing-last-semi.stderr | 4 ++-- src/test/ui/block-result/issue-11714.stderr | 2 +- src/test/ui/block-result/issue-13428.stderr | 4 ++-- .../coercion/coercion-missing-tail-expected-type.stderr | 4 ++-- src/test/ui/issues/issue-32323.stderr | 2 +- src/test/ui/issues/issue-35241.stderr | 2 +- src/test/ui/issues/issue-43162.stderr | 2 +- src/test/ui/issues/issue-44023.stderr | 2 +- src/test/ui/issues/issue-6458-4.stderr | 2 +- src/test/ui/liveness/liveness-forgot-ret.stderr | 2 +- src/test/ui/liveness/liveness-missing-ret2.stderr | 2 +- .../ui/liveness/liveness-return-last-stmt-semi.stderr | 8 ++++---- src/test/ui/missing/missing-return.stderr | 2 +- src/test/ui/parser/issue-62881.stderr | 2 +- src/test/ui/parser/issue-62895.stderr | 2 +- src/test/ui/resolve/privacy-enum-ctor.stderr | 6 +++--- .../ui/suggestions/fn-or-tuple-struct-without-args.stderr | 6 +++--- 18 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 086f3d419576a..4fb28db6e94fa 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3711,8 +3711,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(fn_span) = fn_span { err.span_label( fn_span, - "this function implicitly returns `()` as its body has no tail \ - or `return` expression", + "implicitly returns `()` as its body has no tail or `return` \ + expression", ); } }, false); @@ -3880,10 +3880,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sugg_call = fields.iter().map(|_| "_").collect::>().join(", "); match hir.as_local_hir_id(def_id).and_then(|hir_id| hir.def_kind(hir_id)) { Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => { - msg = "instatiate this tuple variant"; + msg = "instantiate this tuple variant"; } Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Struct, _)) => { - msg = "instatiate this tuple struct"; + msg = "instantiate this tuple struct"; } _ => {} } diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr index 4c421e6ada405..f4984ca446309 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.stderr +++ b/src/test/ui/block-result/consider-removing-last-semi.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | 0u8; LL | "bla".to_string(); | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn g() -> String { | - ^^^^^^ expected struct `std::string::String`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | "this won't work".to_string(); LL | "removeme".to_string(); | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-11714.stderr b/src/test/ui/block-result/issue-11714.stderr index a0bdbd693f2cf..cfb42c601279a 100644 --- a/src/test/ui/block-result/issue-11714.stderr +++ b/src/test/ui/block-result/issue-11714.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn blah() -> i32 { | ---- ^^^ expected i32, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression ... LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/block-result/issue-13428.stderr b/src/test/ui/block-result/issue-13428.stderr index 53f8eaa1ae227..f7cafab3d773b 100644 --- a/src/test/ui/block-result/issue-13428.stderr +++ b/src/test/ui/block-result/issue-13428.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression ... LL | ; | - help: consider removing this semicolon @@ -18,7 +18,7 @@ error[E0308]: mismatched types LL | fn bar() -> String { | --- ^^^^^^ expected struct `std::string::String`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | "foobar".to_string() LL | ; | - help: consider removing this semicolon diff --git a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr index fde41567d8002..955793e8586e7 100644 --- a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr +++ b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn plus_one(x: i32) -> i32 { | -------- ^^^ expected i32, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | x + 1; | - help: consider removing this semicolon | @@ -17,7 +17,7 @@ error[E0308]: mismatched types LL | fn foo() -> Result { | --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | Ok(1); | - help: consider removing this semicolon | diff --git a/src/test/ui/issues/issue-32323.stderr b/src/test/ui/issues/issue-32323.stderr index 138b058f44d9e..6256dc0c55022 100644 --- a/src/test/ui/issues/issue-32323.stderr +++ b/src/test/ui/issues/issue-32323.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | pub fn f<'a, T: Tr<'a>>() -> >::Out {} | - ^^^^^^^^^^^^^^^^^^ expected associated type, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `>::Out` found type `()` diff --git a/src/test/ui/issues/issue-35241.stderr b/src/test/ui/issues/issue-35241.stderr index 8befc6873857e..586146cbaa4ee 100644 --- a/src/test/ui/issues/issue-35241.stderr +++ b/src/test/ui/issues/issue-35241.stderr @@ -8,7 +8,7 @@ LL | fn test() -> Foo { Foo } | --- ^^^ | | | | | expected struct `Foo`, found fn item - | | help: use parentheses to instatiate this tuple struct: `Foo(_)` + | | help: use parentheses to instantiate this tuple struct: `Foo(_)` | expected `Foo` because of return type | = note: expected type `Foo` diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr index e90531bce3e86..6d3e8b5ba2323 100644 --- a/src/test/ui/issues/issue-43162.stderr +++ b/src/test/ui/issues/issue-43162.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn foo() -> bool { | --- ^^^^ expected bool, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | LL | break true; | - help: consider removing this semicolon diff --git a/src/test/ui/issues/issue-44023.stderr b/src/test/ui/issues/issue-44023.stderr index 4a27531b4be8f..258ffe558e9ba 100644 --- a/src/test/ui/issues/issue-44023.stderr +++ b/src/test/ui/issues/issue-44023.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { | ------------------------ ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/issues/issue-6458-4.stderr b/src/test/ui/issues/issue-6458-4.stderr index 4db3daede758f..ecf729e1032b1 100644 --- a/src/test/ui/issues/issue-6458-4.stderr +++ b/src/test/ui/issues/issue-6458-4.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn foo(b: bool) -> Result { | --- ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | Err("bar".to_string()); | - help: consider removing this semicolon | diff --git a/src/test/ui/liveness/liveness-forgot-ret.stderr b/src/test/ui/liveness/liveness-forgot-ret.stderr index 24106f775794c..4baf351f7eb2d 100644 --- a/src/test/ui/liveness/liveness-forgot-ret.stderr +++ b/src/test/ui/liveness/liveness-forgot-ret.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; } | - ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-missing-ret2.stderr b/src/test/ui/liveness/liveness-missing-ret2.stderr index 9502e4467499c..1f60560b45043 100644 --- a/src/test/ui/liveness/liveness-missing-ret2.stderr +++ b/src/test/ui/liveness/liveness-missing-ret2.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { | - ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr index 7ee4cb178a6c5..2497d93daa494 100644 --- a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr +++ b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr @@ -5,7 +5,7 @@ LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } } | --- ^^^ - help: consider removing this semicolon | | | | | expected i32, found () - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression ... LL | test!(); | -------- in this macro invocation @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn no_return() -> i32 {} | --------- ^^^ expected i32, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `i32` found type `()` @@ -30,7 +30,7 @@ error[E0308]: mismatched types LL | fn bar(x: u32) -> u32 { | --- ^^^ expected u32, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression LL | x * 2; | - help: consider removing this semicolon | @@ -43,7 +43,7 @@ error[E0308]: mismatched types LL | fn baz(x: u64) -> u32 { | --- ^^^ expected u32, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `u32` found type `()` diff --git a/src/test/ui/missing/missing-return.stderr b/src/test/ui/missing/missing-return.stderr index 541e9fa4a4016..3c8ecdcfbcbe4 100644 --- a/src/test/ui/missing/missing-return.stderr +++ b/src/test/ui/missing/missing-return.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { } | - ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62881.stderr b/src/test/ui/parser/issue-62881.stderr index 40f64ec8fd0e2..3d58b6fba0baf 100644 --- a/src/test/ui/parser/issue-62881.stderr +++ b/src/test/ui/parser/issue-62881.stderr @@ -19,7 +19,7 @@ error[E0308]: mismatched types LL | fn f() -> isize { fn f() -> isize {} pub f< | - ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/parser/issue-62895.stderr b/src/test/ui/parser/issue-62895.stderr index 4c872d0c9d367..39ce980964b77 100644 --- a/src/test/ui/parser/issue-62895.stderr +++ b/src/test/ui/parser/issue-62895.stderr @@ -38,7 +38,7 @@ error[E0308]: mismatched types LL | fn v() -> isize { | - ^^^^^ expected isize, found () | | - | this function implicitly returns `()` as its body has no tail or `return` expression + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `isize` found type `()` diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr index 3e924f7d161bd..2538bbbf8067f 100644 --- a/src/test/ui/resolve/privacy-enum-ctor.stderr +++ b/src/test/ui/resolve/privacy-enum-ctor.stderr @@ -202,7 +202,7 @@ LL | let _: Z = Z::Fn; | ^^^^^ | | | expected enum `m::n::Z`, found fn item - | help: use parentheses to instatiate this tuple variant: `Z::Fn(_)` + | help: use parentheses to instantiate this tuple variant: `Z::Fn(_)` | = note: expected type `m::n::Z` found type `fn(u8) -> m::n::Z {m::n::Z::Fn}` @@ -232,7 +232,7 @@ LL | let _: E = m::E::Fn; | ^^^^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to instatiate this tuple variant: `m::E::Fn(_)` + | help: use parentheses to instantiate this tuple variant: `m::E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` @@ -262,7 +262,7 @@ LL | let _: E = E::Fn; | ^^^^^ | | | expected enum `m::E`, found fn item - | help: use parentheses to instatiate this tuple variant: `E::Fn(_)` + | help: use parentheses to instantiate this tuple variant: `E::Fn(_)` | = note: expected type `m::E` found type `fn(u8) -> m::E {m::E::Fn}` diff --git a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr index e3012144513fd..0686b56f97ded 100644 --- a/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr +++ b/src/test/ui/suggestions/fn-or-tuple-struct-without-args.stderr @@ -32,7 +32,7 @@ LL | let _: S = S; | ^ | | | expected struct `S`, found fn item - | help: use parentheses to instatiate this tuple struct: `S(_, _)` + | help: use parentheses to instantiate this tuple struct: `S(_, _)` | = note: expected type `S` found type `fn(usize, usize) -> S {S}` @@ -62,7 +62,7 @@ LL | let _: V = V; | ^ | | | expected struct `V`, found fn item - | help: use parentheses to instatiate this tuple struct: `V()` + | help: use parentheses to instantiate this tuple struct: `V()` | = note: expected type `V` found type `fn() -> V {V}` @@ -107,7 +107,7 @@ LL | let _: E = E::A; | ^^^^ | | | expected enum `E`, found fn item - | help: use parentheses to instatiate this tuple variant: `E::A(_)` + | help: use parentheses to instantiate this tuple variant: `E::A(_)` | = note: expected type `E` found type `fn(usize) -> E {E::A}` From 740f8db85572aef58d0734fc60bc2b54862ebbb0 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Wed, 19 Jun 2019 23:15:19 +0300 Subject: [PATCH 044/148] Add FIXME-s that some types should be transparent --- src/libstd/ffi/c_str.rs | 6 ++++++ src/libstd/ffi/os_str.rs | 6 ++++++ src/libstd/path.rs | 12 ++++++++++++ src/libstd/sys_common/os_str_bytes.rs | 6 ++++++ 4 files changed, 30 insertions(+) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 5c6c43017cf64..f7ad62f4e9a8a 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -195,6 +195,12 @@ pub struct CString { /// [`from_ptr`]: #method.from_ptr #[derive(Hash)] #[stable(feature = "rust1", since = "1.0.0")] +// FIXME: +// `fn from` in `impl From<&CStr> for Box` current implementation relies +// on `CStr` being layout-compatible with `[u8]`. +// When attribute privacy is implemented, `CStr` should be annotated as `#[repr(transparent)]`. +// Anyway, `CStr` representation and layout are considered implementation detail, are +// not documented and must not be relied upon. pub struct CStr { // FIXME: this should not be represented with a DST slice but rather with // just a raw `c_char` along with some form of marker to make diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index c7c5849a00fa0..b57f80c2e002b 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -97,6 +97,12 @@ pub struct OsString { /// [`String`]: ../string/struct.String.html /// [conversions]: index.html#conversions #[stable(feature = "rust1", since = "1.0.0")] +// FIXME: +// `OsStr::from_inner` current implementation relies +// on `OsStr` being layout-compatible with `Slice`. +// When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`. +// Anyway, `OsStr` representation and layout are considered implementation detail, are +// not documented and must not be relied upon. pub struct OsStr { inner: Slice } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 126bc3754dabc..9f7a081403da3 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1123,6 +1123,12 @@ impl FusedIterator for Ancestors<'_> {} /// Which method works best depends on what kind of situation you're in. #[derive(Clone)] #[stable(feature = "rust1", since = "1.0.0")] +// FIXME: +// `PathBuf::as_mut_vec` current implementation relies +// on `PathBuf` being layout-compatible with `Vec`. +// When attribute privacy is implemented, `PathBuf` should be annotated as `#[repr(transparent)]`. +// Anyway, `PathBuf` representation and layout are considered implementation detail, are +// not documented and must not be relied upon. pub struct PathBuf { inner: OsString, } @@ -1745,6 +1751,12 @@ impl AsRef for PathBuf { /// assert_eq!(extension, Some(OsStr::new("txt"))); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +// FIXME: +// `Path::new` current implementation relies +// on `Path` being layout-compatible with `OsStr`. +// When attribute privacy is implemented, `Path` should be annotated as `#[repr(transparent)]`. +// Anyway, `Path` representation and layout are considered implementation detail, are +// not documented and must not be relied upon. pub struct Path { inner: OsStr, } diff --git a/src/libstd/sys_common/os_str_bytes.rs b/src/libstd/sys_common/os_str_bytes.rs index a4961974d89ab..d734f412bf886 100644 --- a/src/libstd/sys_common/os_str_bytes.rs +++ b/src/libstd/sys_common/os_str_bytes.rs @@ -18,6 +18,12 @@ pub(crate) struct Buf { pub inner: Vec } +// FIXME: +// `Buf::as_slice` current implementation relies +// on `Slice` being layout-compatible with `[u8]`. +// When attribute privacy is implemented, `Slice` should be annotated as `#[repr(transparent)]`. +// Anyway, `Slice` representation and layout are considered implementation detail, are +// not documented and must not be relied upon. pub(crate) struct Slice { pub inner: [u8] } From 45a5bc7619d19e58cbb1497f571e2ba987d1d53b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 9 Aug 2019 07:57:16 -0700 Subject: [PATCH 045/148] fix tests --- .../async-block-control-flow-static-semantics.stderr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr index 96927ac9632d6..f3f2d14584ef7 100644 --- a/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr +++ b/src/test/ui/async-await/async-block-control-flow-static-semantics.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn return_targets_async_block_not_fn() -> u8 { | --------------------------------- ^^ expected u8, found () | | - | this function's body doesn't return + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `u8` found type `()` @@ -57,7 +57,7 @@ error[E0308]: mismatched types LL | fn rethrow_targets_async_block_not_fn() -> Result { | ---------------------------------- ^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `std::result::Result` found type `()` @@ -68,7 +68,7 @@ error[E0308]: mismatched types LL | fn rethrow_targets_async_block_not_async_fn() -> Result { | ---------------------------------------- ^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found () | | - | this function's body doesn't return + | implicitly returns `()` as its body has no tail or `return` expression | = note: expected type `std::result::Result` found type `()` From 7c96d90c2081f3ca84b3786a125cf2c415335e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Aug 2019 18:24:00 -0700 Subject: [PATCH 046/148] More explicit diagnostic when using a `vec![]` in a pattern ``` error: unexpected `(` after qualified path --> $DIR/vec-macro-in-pattern.rs:3:14 | LL | Some(vec![x]) => (), | ^^^^^^^ | | | unexpected `(` after qualified path | in this macro invocation | use a slice pattern here instead | = help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) ``` --- src/libsyntax/ext/expand.rs | 28 +++++++++++++++---- src/libsyntax/ext/tt/macro_rules.rs | 23 +++++++++++++++ src/test/ui/proc-macro/lifetimes.stderr | 2 +- .../ui/suggestions/vec-macro-in-pattern.rs | 6 ++++ .../suggestions/vec-macro-in-pattern.stderr | 15 ++++++++++ .../ui/type/ascription/issue-47666.stderr | 1 + 6 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/suggestions/vec-macro-in-pattern.rs create mode 100644 src/test/ui/suggestions/vec-macro-in-pattern.stderr diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 964c81dd46641..36f059531d39d 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -686,12 +686,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { ); } - fn parse_ast_fragment(&mut self, - toks: TokenStream, - kind: AstFragmentKind, - path: &Path, - span: Span) - -> AstFragment { + fn parse_ast_fragment( + &mut self, + toks: TokenStream, + kind: AstFragmentKind, + path: &Path, + span: Span, + ) -> AstFragment { let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::>()); match parser.parse_ast_fragment(kind, false) { Ok(fragment) => { @@ -700,6 +701,21 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } Err(mut err) => { err.set_span(span); + match kind { + AstFragmentKind::Ty => { + err.span_label( + span, + "this macro call doesn't expand to a type", + ); + } + AstFragmentKind::Pat => { + err.span_label( + span, + "this macro call doesn't expand to a pattern", + ); + } + _ => {} + }; err.emit(); self.cx.trace_macros_diag(); kind.dummy(span) diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 7401f25641236..a9d0b739d6ac7 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -70,6 +70,29 @@ impl<'a> ParserAnyMacro<'a> { } else if !parser.sess.source_map().span_to_filename(parser.token.span).is_real() { e.span_label(site_span, "in this macro invocation"); } + match kind { + AstFragmentKind::Ty => { + e.span_label( + site_span, + "this macro call doesn't expand to a type", + ); + } + AstFragmentKind::Pat if macro_ident.name == sym::vec => { + e.span_label( + site_span, + "use a slice pattern here instead", + ); + e.help("for more information, see https://doc.rust-lang.org/edition-guide/\ + rust-2018/slice-patterns.html"); + } + AstFragmentKind::Pat => { + e.span_label( + site_span, + "this macro call doesn't expand to a pattern", + ); + } + _ => {} + }; e })); diff --git a/src/test/ui/proc-macro/lifetimes.stderr b/src/test/ui/proc-macro/lifetimes.stderr index 2356a119530bb..6e91201405cc6 100644 --- a/src/test/ui/proc-macro/lifetimes.stderr +++ b/src/test/ui/proc-macro/lifetimes.stderr @@ -2,7 +2,7 @@ error: expected type, found `'` --> $DIR/lifetimes.rs:9:10 | LL | type A = single_quote_alone!(); - | ^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^ this macro call doesn't expand to a type error: aborting due to previous error diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.rs b/src/test/ui/suggestions/vec-macro-in-pattern.rs new file mode 100644 index 0000000000000..5c42a6bdbd44e --- /dev/null +++ b/src/test/ui/suggestions/vec-macro-in-pattern.rs @@ -0,0 +1,6 @@ +fn main() { + match Some(vec![3]) { + Some(vec![x]) => (), //~ ERROR unexpected `(` after qualified path + _ => (), + } +} diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.stderr b/src/test/ui/suggestions/vec-macro-in-pattern.stderr new file mode 100644 index 0000000000000..f94cb93a520b5 --- /dev/null +++ b/src/test/ui/suggestions/vec-macro-in-pattern.stderr @@ -0,0 +1,15 @@ +error: unexpected `(` after qualified path + --> $DIR/vec-macro-in-pattern.rs:3:14 + | +LL | Some(vec![x]) => (), + | ^^^^^^^ + | | + | unexpected `(` after qualified path + | in this macro invocation + | use a slice pattern here instead + | + = help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html + = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) + +error: aborting due to previous error + diff --git a/src/test/ui/type/ascription/issue-47666.stderr b/src/test/ui/type/ascription/issue-47666.stderr index 965bbe5ea41f7..2f052341faead 100644 --- a/src/test/ui/type/ascription/issue-47666.stderr +++ b/src/test/ui/type/ascription/issue-47666.stderr @@ -6,6 +6,7 @@ LL | let _ = Option:Some(vec![0, 1]); | | | | | expected type | | in this macro invocation + | | this macro call doesn't expand to a type | help: maybe write a path separator here: `::` | = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `: ` From 3af92cbaf313e5cf245aa7c1348a580ae1a66ab2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 18:14:56 +0200 Subject: [PATCH 047/148] update Miri --- src/tools/miri | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri b/src/tools/miri index b12ebfc3de853..c1cb24969e84d 160000 --- a/src/tools/miri +++ b/src/tools/miri @@ -1 +1 @@ -Subproject commit b12ebfc3de853abf6b4260c44a71cd51323803c5 +Subproject commit c1cb24969e84dfaded2769ab5575effc8d4f5c30 From b6767b3096f47b9dc4613cd7ae45c5e89bfd1146 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Fri, 9 Aug 2019 09:24:45 -0700 Subject: [PATCH 048/148] Stop test from running on Windows. --- .../reproducible-build-2/Makefile | 16 +++ .../reproducible-build-2/linker.rs | 44 +++++++ .../reproducible-build-aux.rs | 28 +++++ .../reproducible-build.rs | 116 ++++++++++++++++++ .../reproducible-build/Makefile | 11 +- 5 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 src/test/run-make-fulldeps/reproducible-build-2/Makefile create mode 100644 src/test/run-make-fulldeps/reproducible-build-2/linker.rs create mode 100644 src/test/run-make-fulldeps/reproducible-build-2/reproducible-build-aux.rs create mode 100644 src/test/run-make-fulldeps/reproducible-build-2/reproducible-build.rs diff --git a/src/test/run-make-fulldeps/reproducible-build-2/Makefile b/src/test/run-make-fulldeps/reproducible-build-2/Makefile new file mode 100644 index 0000000000000..b96954fea0d1e --- /dev/null +++ b/src/test/run-make-fulldeps/reproducible-build-2/Makefile @@ -0,0 +1,16 @@ +-include ../tools.mk + +# ignore-musl +# ignore-windows +# Objects are reproducible but their path is not. + +all: \ + fat_lto + +fat_lto: + rm -rf $(TMPDIR) && mkdir $(TMPDIR) + $(RUSTC) reproducible-build-aux.rs + $(RUSTC) reproducible-build.rs -C lto=fat + cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a + $(RUSTC) reproducible-build.rs -C lto=fat + cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1 diff --git a/src/test/run-make-fulldeps/reproducible-build-2/linker.rs b/src/test/run-make-fulldeps/reproducible-build-2/linker.rs new file mode 100644 index 0000000000000..998d1f328596c --- /dev/null +++ b/src/test/run-make-fulldeps/reproducible-build-2/linker.rs @@ -0,0 +1,44 @@ +use std::env; +use std::path::Path; +use std::fs::File; +use std::io::{Read, Write}; + +fn main() { + let mut dst = env::current_exe().unwrap(); + dst.pop(); + dst.push("linker-arguments1"); + if dst.exists() { + dst.pop(); + dst.push("linker-arguments2"); + assert!(!dst.exists()); + } + + let mut out = String::new(); + for arg in env::args().skip(1) { + let path = Path::new(&arg); + if !path.is_file() { + out.push_str(&arg); + out.push_str("\n"); + continue + } + + let mut contents = Vec::new(); + File::open(path).unwrap().read_to_end(&mut contents).unwrap(); + + out.push_str(&format!("{}: {}\n", arg, hash(&contents))); + } + + File::create(dst).unwrap().write_all(out.as_bytes()).unwrap(); +} + +// fnv hash for now +fn hash(contents: &[u8]) -> u64 { + let mut hash = 0xcbf29ce484222325; + + for byte in contents { + hash = hash ^ (*byte as u64); + hash = hash.wrapping_mul(0x100000001b3); + } + + hash +} diff --git a/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build-aux.rs b/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build-aux.rs new file mode 100644 index 0000000000000..8105b3d2bda3d --- /dev/null +++ b/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build-aux.rs @@ -0,0 +1,28 @@ +#![crate_type="lib"] + +pub static STATIC: i32 = 1234; + +pub struct Struct { + _t1: std::marker::PhantomData, + _t2: std::marker::PhantomData, +} + +pub fn regular_fn(_: i32) {} + +pub fn generic_fn() {} + +impl Drop for Struct { + fn drop(&mut self) {} +} + +pub enum Enum { + Variant1, + Variant2(u32), + Variant3 { x: u32 } +} + +pub struct TupleStruct(pub i8, pub i16, pub i32, pub i64); + +pub trait Trait { + fn foo(&self); +} diff --git a/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build.rs b/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build.rs new file mode 100644 index 0000000000000..a6c04774c869a --- /dev/null +++ b/src/test/run-make-fulldeps/reproducible-build-2/reproducible-build.rs @@ -0,0 +1,116 @@ +// This test case makes sure that two identical invocations of the compiler +// (i.e., same code base, same compile-flags, same compiler-versions, etc.) +// produce the same output. In the past, symbol names of monomorphized functions +// were not deterministic (which we want to avoid). +// +// The test tries to exercise as many different paths into symbol name +// generation as possible: +// +// - regular functions +// - generic functions +// - methods +// - statics +// - closures +// - enum variant constructors +// - tuple struct constructors +// - drop glue +// - FnOnce adapters +// - Trait object shims +// - Fn Pointer shims + +#![allow(dead_code, warnings)] + +extern crate reproducible_build_aux; + +static STATIC: i32 = 1234; + +pub struct Struct { + x: T1, + y: T2, +} + +fn regular_fn(_: i32) {} + +fn generic_fn() {} + +impl Drop for Struct { + fn drop(&mut self) {} +} + +pub enum Enum { + Variant1, + Variant2(u32), + Variant3 { x: u32 } +} + +struct TupleStruct(i8, i16, i32, i64); + +impl TupleStruct { + pub fn bar(&self) {} +} + +trait Trait { + fn foo(&self); +} + +impl Trait for u64 { + fn foo(&self) {} +} + +impl reproducible_build_aux::Trait for TupleStruct { + fn foo(&self) {} +} + +fn main() { + regular_fn(STATIC); + generic_fn::(); + generic_fn::>(); + generic_fn::, reproducible_build_aux::Struct>(); + + let dropped = Struct { + x: "", + y: 'a', + }; + + let _ = Enum::Variant1; + let _ = Enum::Variant2(0); + let _ = Enum::Variant3 { x: 0 }; + let _ = TupleStruct(1, 2, 3, 4); + + let closure = |x| { + x + 1i32 + }; + + fn inner i32>(f: F) -> i32 { + f(STATIC) + } + + println!("{}", inner(closure)); + + let object_shim: &Trait = &0u64; + object_shim.foo(); + + fn with_fn_once_adapter(f: F) { + f(0); + } + + with_fn_once_adapter(|_:i32| { }); + + reproducible_build_aux::regular_fn(STATIC); + reproducible_build_aux::generic_fn::(); + reproducible_build_aux::generic_fn::>(); + reproducible_build_aux::generic_fn::, + reproducible_build_aux::Struct>(); + + let _ = reproducible_build_aux::Enum::Variant1; + let _ = reproducible_build_aux::Enum::Variant2(0); + let _ = reproducible_build_aux::Enum::Variant3 { x: 0 }; + let _ = reproducible_build_aux::TupleStruct(1, 2, 3, 4); + + let object_shim: &reproducible_build_aux::Trait = &TupleStruct(0, 1, 2, 3); + object_shim.foo(); + + let pointer_shim: &Fn(i32) = ®ular_fn; + + TupleStruct(1, 2, 3, 4).bar(); +} diff --git a/src/test/run-make-fulldeps/reproducible-build/Makefile b/src/test/run-make-fulldeps/reproducible-build/Makefile index 5b9c9d3d03521..a17ec212cfd58 100644 --- a/src/test/run-make-fulldeps/reproducible-build/Makefile +++ b/src/test/run-make-fulldeps/reproducible-build/Makefile @@ -10,8 +10,7 @@ all: \ link_paths \ remap_paths \ different_source_dirs \ - extern_flags \ - fat_lto + extern_flags smoke: rm -rf $(TMPDIR) && mkdir $(TMPDIR) @@ -77,11 +76,3 @@ extern_flags: --extern reproducible_build_aux=$(TMPDIR)/libbar.rlib \ --crate-type rlib cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1 - -fat_lto: - rm -rf $(TMPDIR) && mkdir $(TMPDIR) - $(RUSTC) reproducible-build-aux.rs - $(RUSTC) reproducible-build.rs -C lto=fat - cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a - $(RUSTC) reproducible-build.rs -C lto=fat - cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1 From 75c5ad2e827a077c3738dee11d9e0dc99962f384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 9 Aug 2019 09:39:30 -0700 Subject: [PATCH 049/148] review comments: use structured suggestion --- src/libsyntax/ext/expand.rs | 17 +----- src/libsyntax/ext/tt/macro_rules.rs | 53 ++++++++++++------- .../ui/suggestions/vec-macro-in-pattern.fixed | 8 +++ .../ui/suggestions/vec-macro-in-pattern.rs | 6 ++- .../suggestions/vec-macro-in-pattern.stderr | 8 +-- 5 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 src/test/ui/suggestions/vec-macro-in-pattern.fixed diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 36f059531d39d..9a3195b1165b1 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -6,6 +6,7 @@ use crate::config::StripUnconfigured; use crate::ext::base::*; use crate::ext::proc_macro::collect_derives; use crate::ext::hygiene::{ExpnId, SyntaxContext, ExpnInfo, ExpnKind}; +use crate::ext::tt::macro_rules::annotate_err_with_kind; use crate::ext::placeholders::{placeholder, PlaceholderExpander}; use crate::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err}; use crate::mut_visit::*; @@ -701,21 +702,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } Err(mut err) => { err.set_span(span); - match kind { - AstFragmentKind::Ty => { - err.span_label( - span, - "this macro call doesn't expand to a type", - ); - } - AstFragmentKind::Pat => { - err.span_label( - span, - "this macro call doesn't expand to a pattern", - ); - } - _ => {} - }; + annotate_err_with_kind(&mut err, kind, span); err.emit(); self.cx.trace_macros_diag(); kind.dummy(span) diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index a9d0b739d6ac7..b057a9ad44d0b 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -17,7 +17,7 @@ use crate::symbol::{kw, sym, Symbol}; use crate::tokenstream::{DelimSpan, TokenStream, TokenTree}; use crate::{ast, attr, attr::TransparencyError}; -use errors::FatalError; +use errors::{DiagnosticBuilder, FatalError}; use log::debug; use syntax_pos::Span; @@ -43,6 +43,18 @@ pub struct ParserAnyMacro<'a> { arm_span: Span, } +pub fn annotate_err_with_kind(err: &mut DiagnosticBuilder<'_>, kind: AstFragmentKind, span: Span) { + match kind { + AstFragmentKind::Ty => { + err.span_label(span, "this macro call doesn't expand to a type"); + } + AstFragmentKind::Pat => { + err.span_label(span, "this macro call doesn't expand to a pattern"); + } + _ => {} + }; +} + impl<'a> ParserAnyMacro<'a> { pub fn make(mut self: Box>, kind: AstFragmentKind) -> AstFragment { let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self; @@ -71,27 +83,30 @@ impl<'a> ParserAnyMacro<'a> { e.span_label(site_span, "in this macro invocation"); } match kind { - AstFragmentKind::Ty => { - e.span_label( - site_span, - "this macro call doesn't expand to a type", - ); - } AstFragmentKind::Pat if macro_ident.name == sym::vec => { - e.span_label( - site_span, - "use a slice pattern here instead", - ); + let mut suggestion = None; + if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) { + if let Some(bang) = code.find('!') { + suggestion = Some(code[bang + 1..].to_string()); + } + } + if let Some(suggestion) = suggestion { + e.span_suggestion( + site_span, + "use a slice pattern here instead", + suggestion, + Applicability::MachineApplicable, + ); + } else { + e.span_label( + site_span, + "use a slice pattern here instead", + ); + } e.help("for more information, see https://doc.rust-lang.org/edition-guide/\ - rust-2018/slice-patterns.html"); - } - AstFragmentKind::Pat => { - e.span_label( - site_span, - "this macro call doesn't expand to a pattern", - ); + rust-2018/slice-patterns.html"); } - _ => {} + _ => annotate_err_with_kind(&mut e, kind, site_span), }; e })); diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.fixed b/src/test/ui/suggestions/vec-macro-in-pattern.fixed new file mode 100644 index 0000000000000..e1695d6820a81 --- /dev/null +++ b/src/test/ui/suggestions/vec-macro-in-pattern.fixed @@ -0,0 +1,8 @@ +// run-rustfix +fn main() { + // everything after `.as_ref` should be suggested + match Some(vec![3]).as_ref().map(|v| v.as_slice()) { + Some([_x]) => (), //~ ERROR unexpected `(` after qualified path + _ => (), + } +} diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.rs b/src/test/ui/suggestions/vec-macro-in-pattern.rs index 5c42a6bdbd44e..4843629fbcf90 100644 --- a/src/test/ui/suggestions/vec-macro-in-pattern.rs +++ b/src/test/ui/suggestions/vec-macro-in-pattern.rs @@ -1,6 +1,8 @@ +// run-rustfix fn main() { - match Some(vec![3]) { - Some(vec![x]) => (), //~ ERROR unexpected `(` after qualified path + // everything after `.as_ref` should be suggested + match Some(vec![3]).as_ref().map(|v| v.as_slice()) { + Some(vec![_x]) => (), //~ ERROR unexpected `(` after qualified path _ => (), } } diff --git a/src/test/ui/suggestions/vec-macro-in-pattern.stderr b/src/test/ui/suggestions/vec-macro-in-pattern.stderr index f94cb93a520b5..59ca8ebbf6339 100644 --- a/src/test/ui/suggestions/vec-macro-in-pattern.stderr +++ b/src/test/ui/suggestions/vec-macro-in-pattern.stderr @@ -1,12 +1,12 @@ error: unexpected `(` after qualified path - --> $DIR/vec-macro-in-pattern.rs:3:14 + --> $DIR/vec-macro-in-pattern.rs:5:14 | -LL | Some(vec![x]) => (), - | ^^^^^^^ +LL | Some(vec![_x]) => (), + | ^^^^^^^^ | | | unexpected `(` after qualified path | in this macro invocation - | use a slice pattern here instead + | help: use a slice pattern here instead: `[_x]` | = help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) From d8ae1dc3a5cea4656f0b83f617793e5bad8f66f0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 9 Aug 2019 19:17:18 +0200 Subject: [PATCH 050/148] Update LLVM submodule --- src/llvm-project | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llvm-project b/src/llvm-project index f2b0c67661f19..48818e9f5d0f2 160000 --- a/src/llvm-project +++ b/src/llvm-project @@ -1 +1 @@ -Subproject commit f2b0c67661f19bb2564225d47aca424ad0449791 +Subproject commit 48818e9f5d0f2d5978a9b43ad1a2e8d0b83f6aa0 From 214028da83a6532e73047a334e8e812aa0aa2b22 Mon Sep 17 00:00:00 2001 From: sd234678 Date: Fri, 9 Aug 2019 18:43:30 +0100 Subject: [PATCH 051/148] Update stderr files with --bless --- ...ted-type-projection-from-supertrait.stderr | 8 ++-- ...nding-to-type-defined-in-supertrait.stderr | 8 ++-- src/test/ui/hrtb/hrtb-conflate-regions.stderr | 2 +- .../bound-normalization-fail.stderr | 4 +- src/test/ui/issues/issue-12028.stderr | 2 +- ...s-projection-container-hrtb.migrate.stderr | 20 +++++----- ...lives-projection-container-hrtb.nll.stderr | 4 +- ...ves-projection-container-wc.migrate.stderr | 10 ++--- ...utlives-projection-container-wc.nll.stderr | 2 +- ...gions-outlives-projection-container.stderr | 40 +++++++++---------- .../specialization-no-default.stderr | 10 ++--- .../specialization-no-default.stderr | 10 ++--- 12 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr b/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr index 06f1a1cc64c42..4ba4925ef1b37 100644 --- a/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr +++ b/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:33:23 + --> $DIR/associated-type-projection-from-supertrait.rs:27:23 | LL | fn b() { dent(ModelT, Blue); } | ^^^^ expected struct `Black`, found struct `Blue` @@ -8,7 +8,7 @@ LL | fn b() { dent(ModelT, Blue); } found type `Blue` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:34:23 + --> $DIR/associated-type-projection-from-supertrait.rs:28:23 | LL | fn c() { dent(ModelU, Black); } | ^^^^^ expected struct `Blue`, found struct `Black` @@ -17,7 +17,7 @@ LL | fn c() { dent(ModelU, Black); } found type `Black` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:40:28 + --> $DIR/associated-type-projection-from-supertrait.rs:32:28 | LL | fn f() { ModelT.chip_paint(Blue); } | ^^^^ expected struct `Black`, found struct `Blue` @@ -26,7 +26,7 @@ LL | fn f() { ModelT.chip_paint(Blue); } found type `Blue` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:41:28 + --> $DIR/associated-type-projection-from-supertrait.rs:33:28 | LL | fn g() { ModelU.chip_paint(Black); } | ^^^^^ expected struct `Blue`, found struct `Black` diff --git a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr index 4b548604983df..89c48d50cdb65 100644 --- a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr +++ b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr @@ -1,5 +1,5 @@ error[E0271]: type mismatch resolving `::Color == Blue` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:37:10 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:10 | LL | fn b() { blue_car(ModelT); } | ^^^^^^^^ expected struct `Black`, found struct `Blue` @@ -7,13 +7,13 @@ LL | fn b() { blue_car(ModelT); } = note: expected type `Black` found type `Blue` note: required by `blue_car` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:33:1 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:27:1 | LL | fn blue_car>(c: C) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0271]: type mismatch resolving `::Color == Black` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:38:10 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10 | LL | fn c() { black_car(ModelU); } | ^^^^^^^^^ expected struct `Blue`, found struct `Black` @@ -21,7 +21,7 @@ LL | fn c() { black_car(ModelU); } = note: expected type `Blue` found type `Black` note: required by `black_car` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:30:1 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:24:1 | LL | fn black_car>(c: C) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/hrtb/hrtb-conflate-regions.stderr b/src/test/ui/hrtb/hrtb-conflate-regions.stderr index 3fb6baa35e144..20265d66c6f43 100644 --- a/src/test/ui/hrtb/hrtb-conflate-regions.stderr +++ b/src/test/ui/hrtb/hrtb-conflate-regions.stderr @@ -1,5 +1,5 @@ error[E0277]: the trait bound `for<'a, 'b> SomeStruct: Foo<(&'a isize, &'b isize)>` is not satisfied - --> $DIR/hrtb-conflate-regions.rs:28:10 + --> $DIR/hrtb-conflate-regions.rs:27:10 | LL | fn b() { want_foo2::(); } | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a, 'b> Foo<(&'a isize, &'b isize)>` is not implemented for `SomeStruct` diff --git a/src/test/ui/impl-trait/bound-normalization-fail.stderr b/src/test/ui/impl-trait/bound-normalization-fail.stderr index aa306a7e08a4c..c63a51bde70f8 100644 --- a/src/test/ui/impl-trait/bound-normalization-fail.stderr +++ b/src/test/ui/impl-trait/bound-normalization-fail.stderr @@ -7,7 +7,7 @@ LL | #![feature(impl_trait_in_bindings)] = note: `#[warn(incomplete_features)]` on by default error[E0271]: type mismatch resolving ` as FooLike>::Output == ::Assoc` - --> $DIR/bound-normalization-fail.rs:29:32 + --> $DIR/bound-normalization-fail.rs:28:32 | LL | fn foo_fail() -> impl FooLike { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type @@ -17,7 +17,7 @@ LL | fn foo_fail() -> impl FooLike { = note: the return type of a function must have a statically known size error[E0271]: type mismatch resolving ` as FooLike>::Output == >::Assoc` - --> $DIR/bound-normalization-fail.rs:46:41 + --> $DIR/bound-normalization-fail.rs:44:41 | LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type diff --git a/src/test/ui/issues/issue-12028.stderr b/src/test/ui/issues/issue-12028.stderr index 64694c7a8d0b6..24aa88c3fa379 100644 --- a/src/test/ui/issues/issue-12028.stderr +++ b/src/test/ui/issues/issue-12028.stderr @@ -1,5 +1,5 @@ error[E0284]: type annotations required: cannot resolve `<_ as StreamHasher>::S == ::S` - --> $DIR/issue-12028.rs:29:14 + --> $DIR/issue-12028.rs:27:14 | LL | self.input_stream(&mut stream); | ^^^^^^^^^^^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr b/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr index d83301840088d..ed5800940ee31 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr @@ -1,33 +1,33 @@ error[E0491]: in type `&'a WithHrAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-hrtb.rs:35:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:30:12 | LL | let _: &'a WithHrAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 32:15 - --> $DIR/regions-outlives-projection-container-hrtb.rs:32:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 27:15 + --> $DIR/regions-outlives-projection-container-hrtb.rs:27:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 32:18 - --> $DIR/regions-outlives-projection-container-hrtb.rs:32:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 27:18 + --> $DIR/regions-outlives-projection-container-hrtb.rs:27:18 | LL | fn with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithHrAssocSub>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-hrtb.rs:57:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:50:12 | LL | let _: &'a WithHrAssocSub> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 53:19 - --> $DIR/regions-outlives-projection-container-hrtb.rs:53:19 +note: the pointer is valid for the lifetime 'a as defined on the function body at 46:19 + --> $DIR/regions-outlives-projection-container-hrtb.rs:46:19 | LL | fn with_assoc_sub<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 53:22 - --> $DIR/regions-outlives-projection-container-hrtb.rs:53:22 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 46:22 + --> $DIR/regions-outlives-projection-container-hrtb.rs:46:22 | LL | fn with_assoc_sub<'a,'b>() { | ^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr b/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr index 5028663ba6d04..eed9934be121d 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr @@ -1,5 +1,5 @@ error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-hrtb.rs:35:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:30:12 | LL | fn with_assoc<'a,'b>() { | -- -- lifetime `'b` defined here @@ -10,7 +10,7 @@ LL | let _: &'a WithHrAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-hrtb.rs:57:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:50:12 | LL | fn with_assoc_sub<'a,'b>() { | -- -- lifetime `'b` defined here diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr b/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr index 9e31065ca4eec..152e6c5600c4e 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr @@ -1,16 +1,16 @@ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-wc.rs:37:12 + --> $DIR/regions-outlives-projection-container-wc.rs:33:12 | LL | let _: &'a WithAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 31:15 - --> $DIR/regions-outlives-projection-container-wc.rs:31:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 27:15 + --> $DIR/regions-outlives-projection-container-wc.rs:27:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 31:18 - --> $DIR/regions-outlives-projection-container-wc.rs:31:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 27:18 + --> $DIR/regions-outlives-projection-container-wc.rs:27:18 | LL | fn with_assoc<'a,'b>() { | ^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr b/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr index 880fe17b740e4..8c54d8da0a063 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr @@ -1,5 +1,5 @@ error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-wc.rs:37:12 + --> $DIR/regions-outlives-projection-container-wc.rs:33:12 | LL | fn with_assoc<'a,'b>() { | -- -- lifetime `'b` defined here diff --git a/src/test/ui/regions/regions-outlives-projection-container.stderr b/src/test/ui/regions/regions-outlives-projection-container.stderr index b50347ac96427..3c1a98a3c018f 100644 --- a/src/test/ui/regions/regions-outlives-projection-container.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container.stderr @@ -1,67 +1,67 @@ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:40:13 + --> $DIR/regions-outlives-projection-container.rs:36:13 | LL | let _x: &'a WithAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 32:15 - --> $DIR/regions-outlives-projection-container.rs:32:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 28:15 + --> $DIR/regions-outlives-projection-container.rs:28:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 32:18 - --> $DIR/regions-outlives-projection-container.rs:32:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 28:18 + --> $DIR/regions-outlives-projection-container.rs:28:18 | LL | fn with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithoutAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:58:13 + --> $DIR/regions-outlives-projection-container.rs:54:13 | LL | let _x: &'a WithoutAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 54:18 - --> $DIR/regions-outlives-projection-container.rs:54:18 +note: the pointer is valid for the lifetime 'a as defined on the function body at 50:18 + --> $DIR/regions-outlives-projection-container.rs:50:18 | LL | fn without_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 54:21 - --> $DIR/regions-outlives-projection-container.rs:54:21 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 50:21 + --> $DIR/regions-outlives-projection-container.rs:50:21 | LL | fn without_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:67:12 + --> $DIR/regions-outlives-projection-container.rs:63:12 | LL | call::<&'a WithAssoc>>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 62:20 - --> $DIR/regions-outlives-projection-container.rs:62:20 +note: the pointer is valid for the lifetime 'a as defined on the function body at 58:20 + --> $DIR/regions-outlives-projection-container.rs:58:20 | LL | fn call_with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 62:23 - --> $DIR/regions-outlives-projection-container.rs:62:23 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 58:23 + --> $DIR/regions-outlives-projection-container.rs:58:23 | LL | fn call_with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithoutAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:74:12 + --> $DIR/regions-outlives-projection-container.rs:70:12 | LL | call::<&'a WithoutAssoc>>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 71:23 - --> $DIR/regions-outlives-projection-container.rs:71:23 +note: the pointer is valid for the lifetime 'a as defined on the function body at 67:23 + --> $DIR/regions-outlives-projection-container.rs:67:23 | LL | fn call_without_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 71:26 - --> $DIR/regions-outlives-projection-container.rs:71:26 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 67:26 + --> $DIR/regions-outlives-projection-container.rs:67:26 | LL | fn call_without_assoc<'a,'b>() { | ^^ diff --git a/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr b/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr index 91690f64d948c..13636b28b126c 100644 --- a/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr +++ b/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr @@ -1,5 +1,5 @@ error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:22:5 + --> $DIR/specialization-no-default.rs:20:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -13,7 +13,7 @@ LL | fn foo(&self) {} = note: to specialize, `foo` in the parent `impl` must be marked `default` error[E0520]: `bar` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:25:5 + --> $DIR/specialization-no-default.rs:23:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -27,7 +27,7 @@ LL | fn bar(&self) {} = note: to specialize, `bar` in the parent `impl` must be marked `default` error[E0520]: `T` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:41:5 + --> $DIR/specialization-no-default.rs:37:5 | LL | / impl Bar for T { LL | | type T = u8; @@ -40,7 +40,7 @@ LL | type T = (); = note: to specialize, `T` in the parent `impl` must be marked `default` error[E0520]: `baz` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:61:5 + --> $DIR/specialization-no-default.rs:55:5 | LL | / impl Baz for T { LL | | fn baz(&self) {} @@ -53,7 +53,7 @@ LL | fn baz(&self) {} = note: to specialize, `baz` in the parent `impl` must be marked `default` error[E0520]: `redundant` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:82:5 + --> $DIR/specialization-no-default.rs:74:5 | LL | / impl Redundant for T { LL | | fn redundant(&self) {} diff --git a/src/test/ui/specialization/specialization-no-default.stderr b/src/test/ui/specialization/specialization-no-default.stderr index c39986de38dc2..992e9abbd4ce2 100644 --- a/src/test/ui/specialization/specialization-no-default.stderr +++ b/src/test/ui/specialization/specialization-no-default.stderr @@ -1,5 +1,5 @@ error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:22:5 + --> $DIR/specialization-no-default.rs:20:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -13,7 +13,7 @@ LL | fn foo(&self) {} = note: to specialize, `foo` in the parent `impl` must be marked `default` error[E0520]: `bar` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:25:5 + --> $DIR/specialization-no-default.rs:23:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -27,7 +27,7 @@ LL | fn bar(&self) {} = note: to specialize, `bar` in the parent `impl` must be marked `default` error[E0520]: `T` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:41:5 + --> $DIR/specialization-no-default.rs:37:5 | LL | / impl Bar for T { LL | | type T = u8; @@ -40,7 +40,7 @@ LL | type T = (); = note: to specialize, `T` in the parent `impl` must be marked `default` error[E0520]: `baz` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:61:5 + --> $DIR/specialization-no-default.rs:55:5 | LL | / impl Baz for T { LL | | fn baz(&self) {} @@ -53,7 +53,7 @@ LL | fn baz(&self) {} = note: to specialize, `baz` in the parent `impl` must be marked `default` error[E0520]: `redundant` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:82:5 + --> $DIR/specialization-no-default.rs:74:5 | LL | / impl Redundant for T { LL | | fn redundant(&self) {} From 4dd96d2b0fc87e7081009ab26ba396e3638a4ada Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Aug 2019 20:04:18 +0200 Subject: [PATCH 052/148] check against more collisions for TypeId of fn pointer --- src/test/ui/typeid-intrinsic.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/test/ui/typeid-intrinsic.rs b/src/test/ui/typeid-intrinsic.rs index c2611158e65ef..5bc4e0c217f40 100644 --- a/src/test/ui/typeid-intrinsic.rs +++ b/src/test/ui/typeid-intrinsic.rs @@ -78,10 +78,20 @@ pub fn main() { assert_eq!(TypeId::of::(), other1::id_u32_iterator()); assert_eq!(other1::id_i32_iterator(), other2::id_i32_iterator()); assert_eq!(other1::id_u32_iterator(), other2::id_u32_iterator()); - assert!(other1::id_i32_iterator() != other1::id_u32_iterator()); - assert!(TypeId::of::() != TypeId::of::()); + assert_ne!(other1::id_i32_iterator(), other1::id_u32_iterator()); + assert_ne!(TypeId::of::(), TypeId::of::()); // Check fn pointer against collisions - assert!(TypeId::of:: A) -> A>() != - TypeId::of:: A, A) -> A>()); + assert_ne!( + TypeId::of:: A) -> A>(), + TypeId::of:: A, A) -> A>() + ); + assert_ne!( + TypeId::of:: fn(&'a i32) -> &'a i32>(), + TypeId::of:: fn(&'a i32) -> &'static i32>() + ); + assert_ne!( + TypeId::of:: fn(&'a i32, &'b i32) -> &'a i32>(), + TypeId::of:: fn(&'b i32, &'a i32) -> &'a i32>() + ); } From b9865d9e3fdd87a5b7e45153effef3055d85f70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 9 Aug 2019 12:52:02 -0700 Subject: [PATCH 053/148] Mention that tuple structs are private if their fields are --- src/librustc_resolve/lib.rs | 31 +++++- src/test/ui/privacy/privacy5.stderr | 96 +++++++++++++++++++ .../ui/resolve/privacy-struct-ctor.stderr | 12 +++ .../ui/rfc-2008-non-exhaustive/struct.stderr | 2 + .../ui/rfc-2008-non-exhaustive/variant.stderr | 6 ++ 5 files changed, 144 insertions(+), 3 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index e7feccf506957..ce2bc79ff6034 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -50,7 +50,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind}; use syntax::ast::{Label, Local, Mutability, Pat, PatKind, Path}; use syntax::ast::{QSelf, TraitItem, TraitItemKind, TraitRef, Ty, TyKind}; use syntax::ptr::P; -use syntax::{span_err, struct_span_err, unwrap_or, walk_list}; +use syntax::{struct_span_err, unwrap_or, walk_list}; use syntax_pos::{Span, DUMMY_SP, MultiSpan}; use errors::{Applicability, DiagnosticBuilder, DiagnosticId}; @@ -4789,8 +4789,33 @@ impl<'a> Resolver<'a> { let mut reported_spans = FxHashSet::default(); for &PrivacyError(dedup_span, ident, binding) in &self.privacy_errors { if reported_spans.insert(dedup_span) { - span_err!(self.session, ident.span, E0603, "{} `{}` is private", - binding.descr(), ident.name); + let mut err = struct_span_err!( + self.session, + ident.span, + E0603, + "{} `{}` is private", + binding.descr(), + ident.name, + ); + // FIXME: use the ctor's `def_id` to check wether any of the fields is not visible + match binding.kind { + NameBindingKind::Res(Res::Def(DefKind::Ctor( + CtorOf::Struct, + CtorKind::Fn, + ), _def_id), _) => { + err.note("a tuple struct constructor is private if any of its fields \ + is private"); + } + NameBindingKind::Res(Res::Def(DefKind::Ctor( + CtorOf::Variant, + CtorKind::Fn, + ), _def_id), _) => { + err.note("a tuple variant constructor is private if any of its fields \ + is private"); + } + _ => {} + } + err.emit(); } } } diff --git a/src/test/ui/privacy/privacy5.stderr b/src/test/ui/privacy/privacy5.stderr index 7568c346b2489..532d1ac1e2fb8 100644 --- a/src/test/ui/privacy/privacy5.stderr +++ b/src/test/ui/privacy/privacy5.stderr @@ -3,288 +3,384 @@ error[E0603]: tuple struct `A` is private | LL | let a = a::A(()); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:52:16 | LL | let b = a::B(2); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:53:16 | LL | let c = a::C(2, 3); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:56:12 | LL | let a::A(()) = a; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:57:12 | LL | let a::A(_) = a; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:58:18 | LL | match a { a::A(()) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:59:18 | LL | match a { a::A(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:61:12 | LL | let a::B(_) = b; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:62:12 | LL | let a::B(_b) = b; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:63:18 | LL | match b { a::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:64:18 | LL | match b { a::B(_b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:65:18 | LL | match b { a::B(1) => {} a::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:65:32 | LL | match b { a::B(1) => {} a::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:68:12 | LL | let a::C(_, _) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:69:12 | LL | let a::C(_a, _) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:70:12 | LL | let a::C(_, _b) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:71:12 | LL | let a::C(_a, _b) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:72:18 | LL | match c { a::C(_, _) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:73:18 | LL | match c { a::C(_a, _) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:74:18 | LL | match c { a::C(_, _b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:75:18 | LL | match c { a::C(_a, _b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:83:17 | LL | let a2 = a::A; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:84:17 | LL | let b2 = a::B; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:85:17 | LL | let c2 = a::C; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:90:20 | LL | let a = other::A(()); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:91:20 | LL | let b = other::B(2); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:92:20 | LL | let c = other::C(2, 3); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:95:16 | LL | let other::A(()) = a; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:96:16 | LL | let other::A(_) = a; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:97:22 | LL | match a { other::A(()) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:98:22 | LL | match a { other::A(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:100:16 | LL | let other::B(_) = b; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:101:16 | LL | let other::B(_b) = b; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:102:22 | LL | match b { other::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:103:22 | LL | match b { other::B(_b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:104:22 | LL | match b { other::B(1) => {} other::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:104:40 | LL | match b { other::B(1) => {} other::B(_) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:107:16 | LL | let other::C(_, _) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:108:16 | LL | let other::C(_a, _) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:109:16 | LL | let other::C(_, _b) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:110:16 | LL | let other::C(_a, _b) = c; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:111:22 | LL | match c { other::C(_, _) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:112:22 | LL | match c { other::C(_a, _) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:113:22 | LL | match c { other::C(_, _b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:114:22 | LL | match c { other::C(_a, _b) => {} } | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `A` is private --> $DIR/privacy5.rs:122:21 | LL | let a2 = other::A; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `B` is private --> $DIR/privacy5.rs:123:21 | LL | let b2 = other::B; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `C` is private --> $DIR/privacy5.rs:124:21 | LL | let c2 = other::C; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error: aborting due to 48 previous errors diff --git a/src/test/ui/resolve/privacy-struct-ctor.stderr b/src/test/ui/resolve/privacy-struct-ctor.stderr index 9bf7d19117448..72d62fe45ce74 100644 --- a/src/test/ui/resolve/privacy-struct-ctor.stderr +++ b/src/test/ui/resolve/privacy-struct-ctor.stderr @@ -34,36 +34,48 @@ error[E0603]: tuple struct `Z` is private | LL | n::Z; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `S` is private --> $DIR/privacy-struct-ctor.rs:29:8 | LL | m::S; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `S` is private --> $DIR/privacy-struct-ctor.rs:31:19 | LL | let _: S = m::S(2); | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `Z` is private --> $DIR/privacy-struct-ctor.rs:35:11 | LL | m::n::Z; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `S` is private --> $DIR/privacy-struct-ctor.rs:41:16 | LL | xcrate::m::S; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: tuple struct `Z` is private --> $DIR/privacy-struct-ctor.rs:45:19 | LL | xcrate::m::n::Z; | ^ + | + = note: a tuple struct constructor is private if any of its fields is private error: aborting due to 10 previous errors diff --git a/src/test/ui/rfc-2008-non-exhaustive/struct.stderr b/src/test/ui/rfc-2008-non-exhaustive/struct.stderr index 96040f11b525f..d75a376286fcc 100644 --- a/src/test/ui/rfc-2008-non-exhaustive/struct.stderr +++ b/src/test/ui/rfc-2008-non-exhaustive/struct.stderr @@ -15,6 +15,8 @@ error[E0603]: tuple struct `TupleStruct` is private | LL | let ts_explicit = structs::TupleStruct(640, 480); | ^^^^^^^^^^^ + | + = note: a tuple struct constructor is private if any of its fields is private error[E0603]: unit struct `UnitStruct` is private --> $DIR/struct.rs:32:32 diff --git a/src/test/ui/rfc-2008-non-exhaustive/variant.stderr b/src/test/ui/rfc-2008-non-exhaustive/variant.stderr index d9d6ea21b8bd4..ac0025ec75807 100644 --- a/src/test/ui/rfc-2008-non-exhaustive/variant.stderr +++ b/src/test/ui/rfc-2008-non-exhaustive/variant.stderr @@ -3,6 +3,8 @@ error[E0603]: tuple variant `Tuple` is private | LL | let variant_tuple = NonExhaustiveVariants::Tuple(640); | ^^^^^ + | + = note: a tuple variant constructor is private if any of its fields is private error[E0603]: unit variant `Unit` is private --> $DIR/variant.rs:14:47 @@ -21,12 +23,16 @@ error[E0603]: tuple variant `Tuple` is private | LL | NonExhaustiveVariants::Tuple(fe_tpl) => "", | ^^^^^ + | + = note: a tuple variant constructor is private if any of its fields is private error[E0603]: tuple variant `Tuple` is private --> $DIR/variant.rs:26:35 | LL | if let NonExhaustiveVariants::Tuple(fe_tpl) = variant_struct { | ^^^^^ + | + = note: a tuple variant constructor is private if any of its fields is private error[E0639]: cannot create non-exhaustive variant using struct expression --> $DIR/variant.rs:8:26 From cbcc7dd182b9bf67d664508b82284c5539ef8819 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 28 Jul 2019 01:51:21 +0300 Subject: [PATCH 054/148] Give built-in macros stable addresses in the standard library --- src/libcore/clone.rs | 8 + src/libcore/cmp.rs | 32 +++ src/libcore/default.rs | 8 + src/libcore/fmt/mod.rs | 15 ++ src/libcore/hash/mod.rs | 15 ++ src/libcore/macros.rs | 185 ++++++------------ src/libcore/marker.rs | 8 + src/libcore/prelude/v1.rs | 38 ++-- src/libstd/lib.rs | 51 +++-- src/test/ui/imports/issue-53512.rs | 3 +- src/test/ui/imports/issue-53512.stderr | 8 +- .../ui/macros/builtin-prelude-no-accidents.rs | 8 + .../builtin-prelude-no-accidents.stderr | 21 ++ src/test/ui/macros/builtin-std-paths-fail.rs | 21 ++ .../ui/macros/builtin-std-paths-fail.stderr | 75 +++++++ src/test/ui/macros/builtin-std-paths.rs | 32 +++ 16 files changed, 364 insertions(+), 164 deletions(-) create mode 100644 src/test/ui/macros/builtin-prelude-no-accidents.rs create mode 100644 src/test/ui/macros/builtin-prelude-no-accidents.stderr create mode 100644 src/test/ui/macros/builtin-std-paths-fail.rs create mode 100644 src/test/ui/macros/builtin-std-paths-fail.stderr create mode 100644 src/test/ui/macros/builtin-std-paths.rs diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 9e32acb97d360..0c99356390bda 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -133,6 +133,14 @@ pub trait Clone : Sized { } } +/// Derive macro generating an impl of the trait `Clone`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics, derive_clone_copy)] +pub macro Clone($item:item) { /* compiler built-in */ } + // FIXME(aburka): these structs are used solely by #[derive] to // assert that every component of a type implements Clone or Copy. // diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f9613556a1ebc..38a52d97da212 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -200,6 +200,14 @@ pub trait PartialEq { fn ne(&self, other: &Rhs) -> bool { !self.eq(other) } } +/// Derive macro generating an impl of the trait `PartialEq`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics)] +pub macro PartialEq($item:item) { /* compiler built-in */ } + /// Trait for equality comparisons which are [equivalence relations]( /// https://en.wikipedia.org/wiki/Equivalence_relation). /// @@ -256,6 +264,14 @@ pub trait Eq: PartialEq { fn assert_receiver_is_total_eq(&self) {} } +/// Derive macro generating an impl of the trait `Eq`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics, derive_eq)] +pub macro Eq($item:item) { /* compiler built-in */ } + // FIXME: this struct is used solely by #[derive] to // assert that every component of a type implements Eq. // @@ -600,6 +616,14 @@ pub trait Ord: Eq + PartialOrd { } } +/// Derive macro generating an impl of the trait `Ord`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics)] +pub macro Ord($item:item) { /* compiler built-in */ } + #[stable(feature = "rust1", since = "1.0.0")] impl Eq for Ordering {} @@ -842,6 +866,14 @@ pub trait PartialOrd: PartialEq { } } +/// Derive macro generating an impl of the trait `PartialOrd`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics)] +pub macro PartialOrd($item:item) { /* compiler built-in */ } + /// Compares and returns the minimum of two values. /// /// Returns the first argument if the comparison determines them to be equal. diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 5ad05b3824764..8d95e9de15849 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -115,6 +115,14 @@ pub trait Default: Sized { fn default() -> Self; } +/// Derive macro generating an impl of the trait `Default`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics)] +pub macro Default($item:item) { /* compiler built-in */ } + macro_rules! default_impl { ($t:ty, $v:expr, $doc:tt) => { #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 4a7c6af7adab7..0ea01d4b84a2c 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -545,6 +545,21 @@ pub trait Debug { fn fmt(&self, f: &mut Formatter<'_>) -> Result; } +// Separate module to reexport the macro `Debug` from prelude without the trait `Debug`. +#[cfg(not(bootstrap))] +pub(crate) mod macros { + /// Derive macro generating an impl of the trait `Debug`. + #[rustc_builtin_macro] + #[rustc_macro_transparency = "semitransparent"] + #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] + #[allow_internal_unstable(core_intrinsics)] + pub macro Debug($item:item) { /* compiler built-in */ } +} +#[cfg(not(bootstrap))] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[doc(inline)] +pub use macros::Debug; + /// Format trait for an empty format, `{}`. /// /// `Display` is similar to [`Debug`][debug], but `Display` is for user-facing diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 38e3864284240..c4cbf40a93a15 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -198,6 +198,21 @@ pub trait Hash { } } +// Separate module to reexport the macro `Hash` from prelude without the trait `Hash`. +#[cfg(not(bootstrap))] +pub(crate) mod macros { + /// Derive macro generating an impl of the trait `Hash`. + #[rustc_builtin_macro] + #[rustc_macro_transparency = "semitransparent"] + #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] + #[allow_internal_unstable(core_intrinsics)] + pub macro Hash($item:item) { /* compiler built-in */ } +} +#[cfg(not(bootstrap))] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[doc(inline)] +pub use macros::Hash; + /// A trait for hashing an arbitrary stream of bytes. /// /// Instances of `Hasher` usually represent state that is changed while hashing diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 667b35d0f775a..f9dc53874acb1 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -714,9 +714,9 @@ pub(crate) mod builtin { /// [`panic!`]: ../std/macro.panic.html #[stable(feature = "compile_error_macro", since = "1.20.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro compile_error { - ($msg:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! compile_error { + ($msg:expr) => ({ /* compiler built-in */ }); ($msg:expr,) => ({ /* compiler built-in */ }) } @@ -768,8 +768,10 @@ pub(crate) mod builtin { #[stable(feature = "rust1", since = "1.0.0")] #[allow_internal_unstable(fmt_internals)] #[rustc_builtin_macro] - pub macro format_args { - ($fmt:expr) => ({ /* compiler built-in */ }), + #[macro_export] + #[rustc_macro_transparency = "opaque"] + macro_rules! format_args { + ($fmt:expr) => ({ /* compiler built-in */ }); ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) } @@ -779,8 +781,10 @@ pub(crate) mod builtin { language use and is subject to change")] #[allow_internal_unstable(fmt_internals)] #[rustc_builtin_macro] - pub macro format_args_nl { - ($fmt:expr) => ({ /* compiler built-in */ }), + #[macro_export] + #[rustc_macro_transparency = "opaque"] + macro_rules! format_args_nl { + ($fmt:expr) => ({ /* compiler built-in */ }); ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) } @@ -817,9 +821,9 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro env { - ($name:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! env { + ($name:expr) => ({ /* compiler built-in */ }); ($name:expr,) => ({ /* compiler built-in */ }) } @@ -844,9 +848,9 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro option_env { - ($name:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! option_env { + ($name:expr) => ({ /* compiler built-in */ }); ($name:expr,) => ({ /* compiler built-in */ }) } @@ -877,9 +881,9 @@ pub(crate) mod builtin { #[unstable(feature = "concat_idents", issue = "29599", reason = "`concat_idents` is not stable enough for use and is subject to change")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro concat_idents { - ($($e:ident),+) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! concat_idents { + ($($e:ident),+) => ({ /* compiler built-in */ }); ($($e:ident,)+) => ({ /* compiler built-in */ }) } @@ -900,9 +904,9 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro concat { - ($($e:expr),*) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! concat { + ($($e:expr),*) => ({ /* compiler built-in */ }); ($($e:expr,)*) => ({ /* compiler built-in */ }) } @@ -929,8 +933,8 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro line() { /* compiler built-in */ } + #[macro_export] + macro_rules! line { () => { /* compiler built-in */ } } /// Expands to the column number at which it was invoked. /// @@ -955,15 +959,15 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro column() { /* compiler built-in */ } + #[macro_export] + macro_rules! column { () => { /* compiler built-in */ } } /// Same as `column`, but less likely to be shadowed. #[unstable(feature = "__rust_unstable_column", issue = "0", reason = "internal implementation detail of the `panic` macro")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro __rust_unstable_column() { /* compiler built-in */ } + #[macro_export] + macro_rules! __rust_unstable_column { () => { /* compiler built-in */ } } /// Expands to the file name in which it was invoked. /// @@ -987,8 +991,8 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro file() { /* compiler built-in */ } + #[macro_export] + macro_rules! file { () => { /* compiler built-in */ } } /// Stringifies its arguments. /// @@ -1007,8 +1011,8 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro stringify($($t:tt)*) { /* compiler built-in */ } + #[macro_export] + macro_rules! stringify { ($($t:tt)*) => { /* compiler built-in */ } } /// Includes a utf8-encoded file as a string. /// @@ -1042,9 +1046,9 @@ pub(crate) mod builtin { /// Compiling 'main.rs' and running the resulting binary will print "adiós". #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro include_str { - ($file:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! include_str { + ($file:expr) => ({ /* compiler built-in */ }); ($file:expr,) => ({ /* compiler built-in */ }) } @@ -1080,9 +1084,9 @@ pub(crate) mod builtin { /// Compiling 'main.rs' and running the resulting binary will print "adiós". #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro include_bytes { - ($file:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! include_bytes { + ($file:expr) => ({ /* compiler built-in */ }); ($file:expr,) => ({ /* compiler built-in */ }) } @@ -1105,8 +1109,8 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro module_path() { /* compiler built-in */ } + #[macro_export] + macro_rules! module_path { () => { /* compiler built-in */ } } /// Evaluates boolean combinations of configuration flags at compile-time. /// @@ -1130,8 +1134,8 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro cfg($($cfg:tt)*) { /* compiler built-in */ } + #[macro_export] + macro_rules! cfg { ($($cfg:tt)*) => { /* compiler built-in */ } } /// Parses a file as an expression or an item according to the context. /// @@ -1174,9 +1178,9 @@ pub(crate) mod builtin { /// "🙈🙊🙉🙈🙊🙉". #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro include { - ($file:expr) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! include { + ($file:expr) => ({ /* compiler built-in */ }); ($file:expr,) => ({ /* compiler built-in */ }) } @@ -1227,10 +1231,10 @@ pub(crate) mod builtin { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro assert { - ($cond:expr) => ({ /* compiler built-in */ }), - ($cond:expr,) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! assert { + ($cond:expr) => ({ /* compiler built-in */ }); + ($cond:expr,) => ({ /* compiler built-in */ }); ($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ }) } @@ -1238,34 +1242,34 @@ pub(crate) mod builtin { #[unstable(feature = "asm", issue = "29722", reason = "inline assembly is not stable enough for use and is subject to change")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro asm("assembly template" - : $("output"(operand),)* - : $("input"(operand),)* - : $("clobbers",)* - : $("options",)*) { /* compiler built-in */ } + #[macro_export] + macro_rules! asm { ("assembly template" + : $("output"(operand),)* + : $("input"(operand),)* + : $("clobbers",)* + : $("options",)*) => { /* compiler built-in */ } } /// Module-level inline assembly. #[unstable(feature = "global_asm", issue = "35119", reason = "`global_asm!` is not stable enough for use and is subject to change")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro global_asm("assembly") { /* compiler built-in */ } + #[macro_export] + macro_rules! global_asm { ("assembly") => { /* compiler built-in */ } } /// Prints passed tokens into the standard output. #[unstable(feature = "log_syntax", issue = "29598", reason = "`log_syntax!` is not stable enough for use and is subject to change")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro log_syntax($($arg:tt)*) { /* compiler built-in */ } + #[macro_export] + macro_rules! log_syntax { ($($arg:tt)*) => { /* compiler built-in */ } } /// Enables or disables tracing functionality used for debugging other macros. #[unstable(feature = "trace_macros", issue = "29598", reason = "`trace_macros` is not stable enough for use and is subject to change")] #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - pub macro trace_macros { - (true) => ({ /* compiler built-in */ }), + #[macro_export] + macro_rules! trace_macros { + (true) => ({ /* compiler built-in */ }); (false) => ({ /* compiler built-in */ }) } @@ -1299,69 +1303,6 @@ pub(crate) mod builtin { #[rustc_macro_transparency = "semitransparent"] pub macro global_allocator($item:item) { /* compiler built-in */ } - /// Derive macro generating an impl of the trait `Clone`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics, derive_clone_copy)] - pub macro Clone($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Copy`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics, derive_clone_copy)] - pub macro Copy($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Debug`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro Debug($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Default`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro Default($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Eq`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics, derive_eq)] - pub macro Eq($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Hash`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro Hash($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `Ord`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro Ord($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `PartialEq`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro PartialEq($item:item) { /* compiler built-in */ } - - /// Derive macro generating an impl of the trait `PartialOrd`. - #[rustc_builtin_macro] - #[rustc_macro_transparency = "semitransparent"] - #[stable(feature = "rust1", since = "1.0.0")] - #[allow_internal_unstable(core_intrinsics)] - pub macro PartialOrd($item:item) { /* compiler built-in */ } - /// Unstable implementation detail of the `rustc` compiler, do not use. #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 79a188dbac99d..78a273611650c 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -288,6 +288,14 @@ pub trait Copy : Clone { // Empty. } +/// Derive macro generating an impl of the trait `Copy`. +#[cfg(not(bootstrap))] +#[rustc_builtin_macro] +#[rustc_macro_transparency = "semitransparent"] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow_internal_unstable(core_intrinsics, derive_clone_copy)] +pub macro Copy($item:item) { /* compiler built-in */ } + /// Types for which it is safe to share references between threads. /// /// This trait is automatically implemented when the compiler determines diff --git a/src/libcore/prelude/v1.rs b/src/libcore/prelude/v1.rs index c503d8c701938..84cf85f339c99 100644 --- a/src/libcore/prelude/v1.rs +++ b/src/libcore/prelude/v1.rs @@ -48,24 +48,20 @@ pub use crate::result::Result::{self, Ok, Err}; // Re-exported built-in macros #[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] -#[allow(deprecated)] #[doc(no_inline)] -pub use crate::macros::builtin::{ - Clone, - Copy, - Debug, - Default, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - RustcDecodable, - RustcEncodable, +pub use crate::fmt::macros::Debug; +#[cfg(not(bootstrap))] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[doc(no_inline)] +pub use crate::hash::macros::Hash; + +#[cfg(not(bootstrap))] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[doc(no_inline)] +pub use crate::{ __rust_unstable_column, asm, assert, - bench, cfg, column, compile_error, @@ -75,7 +71,6 @@ pub use crate::macros::builtin::{ file, format_args, format_args_nl, - global_allocator, global_asm, include, include_bytes, @@ -85,7 +80,18 @@ pub use crate::macros::builtin::{ module_path, option_env, stringify, + trace_macros, +}; + +#[cfg(not(bootstrap))] +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[allow(deprecated)] +#[doc(no_inline)] +pub use crate::macros::builtin::{ + RustcDecodable, + RustcEncodable, + bench, + global_allocator, test, test_case, - trace_macros, }; diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 54abf72d3075a..e03626fb7f565 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -326,16 +326,6 @@ use prelude::v1::*; // Access to Bencher, etc. #[cfg(test)] extern crate test; -// Re-export a few macros from core -#[stable(feature = "rust1", since = "1.0.0")] -pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne}; -#[stable(feature = "rust1", since = "1.0.0")] -pub use core::{unreachable, unimplemented, write, writeln, todo}; -// FIXME: change this to `#[allow(deprecated)]` when we update nightly compiler. -#[allow(deprecated_in_future)] -#[stable(feature = "rust1", since = "1.0.0")] -pub use core::r#try; - #[allow(unused_imports)] // macros from `alloc` are not used on all platforms #[macro_use] extern crate alloc as alloc_crate; @@ -520,33 +510,52 @@ mod std_detect; #[cfg(not(test))] pub use std_detect::detect; -// Document built-in macros in the crate root for consistency with libcore and existing tradition. -// FIXME: Attribute and derive macros are not reexported because rustdoc renders them -// as reexports rather than as macros, and that's not what we want. -#[cfg(rustdoc)] +// Re-export macros defined in libcore. +#[stable(feature = "rust1", since = "1.0.0")] +#[allow(deprecated_in_future)] +pub use core::{ + // Stable + assert_eq, + assert_ne, + debug_assert_eq, + debug_assert_ne, + debug_assert, + r#try, + unimplemented, + unreachable, + write, + writeln, + // Unstable + todo, +}; + +// Re-export built-in macros defined through libcore. +#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] -pub use crate::prelude::v1::{ - __rust_unstable_column, - asm, +pub use core::{ + // Stable assert, cfg, column, compile_error, concat, - concat_idents, env, file, format_args, - format_args_nl, - global_asm, include, include_bytes, include_str, line, - log_syntax, module_path, option_env, stringify, + // Unstable + __rust_unstable_column, + asm, + concat_idents, + format_args_nl, + global_asm, + log_syntax, trace_macros, }; diff --git a/src/test/ui/imports/issue-53512.rs b/src/test/ui/imports/issue-53512.rs index 615b36a0b21ec..67470f854cedf 100644 --- a/src/test/ui/imports/issue-53512.rs +++ b/src/test/ui/imports/issue-53512.rs @@ -1,6 +1,7 @@ // Macro from prelude is shadowed by non-existent import recovered as `Res::Err`. -use std::assert; //~ ERROR unresolved import `std::assert` +mod m {} +use m::assert; //~ ERROR unresolved import `m::assert` fn main() { assert!(true); diff --git a/src/test/ui/imports/issue-53512.stderr b/src/test/ui/imports/issue-53512.stderr index f902fc488882f..05fe111b38bc6 100644 --- a/src/test/ui/imports/issue-53512.stderr +++ b/src/test/ui/imports/issue-53512.stderr @@ -1,8 +1,8 @@ -error[E0432]: unresolved import `std::assert` - --> $DIR/issue-53512.rs:3:5 +error[E0432]: unresolved import `m::assert` + --> $DIR/issue-53512.rs:4:5 | -LL | use std::assert; - | ^^^^^^^^^^^ no `assert` in the root +LL | use m::assert; + | ^^^^^^^^^ no `assert` in `m` error: aborting due to previous error diff --git a/src/test/ui/macros/builtin-prelude-no-accidents.rs b/src/test/ui/macros/builtin-prelude-no-accidents.rs new file mode 100644 index 0000000000000..ac82f343acc02 --- /dev/null +++ b/src/test/ui/macros/builtin-prelude-no-accidents.rs @@ -0,0 +1,8 @@ +// Names of public modules in libstd and libcore don't accidentally get into prelude +// because macros with the same names are in prelude. + +fn main() { + env::current_dir; //~ ERROR use of undeclared type or module `env` + type A = panic::PanicInfo; //~ ERROR use of undeclared type or module `panic` + type B = vec::Vec; //~ ERROR use of undeclared type or module `vec` +} diff --git a/src/test/ui/macros/builtin-prelude-no-accidents.stderr b/src/test/ui/macros/builtin-prelude-no-accidents.stderr new file mode 100644 index 0000000000000..914e906df58b3 --- /dev/null +++ b/src/test/ui/macros/builtin-prelude-no-accidents.stderr @@ -0,0 +1,21 @@ +error[E0433]: failed to resolve: use of undeclared type or module `env` + --> $DIR/builtin-prelude-no-accidents.rs:5:5 + | +LL | env::current_dir; + | ^^^ use of undeclared type or module `env` + +error[E0433]: failed to resolve: use of undeclared type or module `panic` + --> $DIR/builtin-prelude-no-accidents.rs:6:14 + | +LL | type A = panic::PanicInfo; + | ^^^^^ use of undeclared type or module `panic` + +error[E0433]: failed to resolve: use of undeclared type or module `vec` + --> $DIR/builtin-prelude-no-accidents.rs:7:14 + | +LL | type B = vec::Vec; + | ^^^ use of undeclared type or module `vec` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0433`. diff --git a/src/test/ui/macros/builtin-std-paths-fail.rs b/src/test/ui/macros/builtin-std-paths-fail.rs new file mode 100644 index 0000000000000..33de3d5184b1f --- /dev/null +++ b/src/test/ui/macros/builtin-std-paths-fail.rs @@ -0,0 +1,21 @@ +#[derive( + core::RustcDecodable, //~ ERROR could not find `RustcDecodable` in `core` + core::RustcDecodable, //~ ERROR could not find `RustcDecodable` in `core` +)] +#[core::bench] //~ ERROR could not find `bench` in `core` +#[core::global_allocator] //~ ERROR could not find `global_allocator` in `core` +#[core::test_case] //~ ERROR could not find `test_case` in `core` +#[core::test] //~ ERROR could not find `test` in `core` +struct Core; + +#[derive( + std::RustcDecodable, //~ ERROR could not find `RustcDecodable` in `std` + std::RustcDecodable, //~ ERROR could not find `RustcDecodable` in `std` +)] +#[std::bench] //~ ERROR could not find `bench` in `std` +#[std::global_allocator] //~ ERROR could not find `global_allocator` in `std` +#[std::test_case] //~ ERROR could not find `test_case` in `std` +#[std::test] //~ ERROR could not find `test` in `std` +struct Std; + +fn main() {} diff --git a/src/test/ui/macros/builtin-std-paths-fail.stderr b/src/test/ui/macros/builtin-std-paths-fail.stderr new file mode 100644 index 0000000000000..6de689076b849 --- /dev/null +++ b/src/test/ui/macros/builtin-std-paths-fail.stderr @@ -0,0 +1,75 @@ +error[E0433]: failed to resolve: could not find `bench` in `core` + --> $DIR/builtin-std-paths-fail.rs:5:9 + | +LL | #[core::bench] + | ^^^^^ could not find `bench` in `core` + +error[E0433]: failed to resolve: could not find `global_allocator` in `core` + --> $DIR/builtin-std-paths-fail.rs:6:9 + | +LL | #[core::global_allocator] + | ^^^^^^^^^^^^^^^^ could not find `global_allocator` in `core` + +error[E0433]: failed to resolve: could not find `test_case` in `core` + --> $DIR/builtin-std-paths-fail.rs:7:9 + | +LL | #[core::test_case] + | ^^^^^^^^^ could not find `test_case` in `core` + +error[E0433]: failed to resolve: could not find `test` in `core` + --> $DIR/builtin-std-paths-fail.rs:8:9 + | +LL | #[core::test] + | ^^^^ could not find `test` in `core` + +error[E0433]: failed to resolve: could not find `RustcDecodable` in `core` + --> $DIR/builtin-std-paths-fail.rs:2:11 + | +LL | core::RustcDecodable, + | ^^^^^^^^^^^^^^ could not find `RustcDecodable` in `core` + +error[E0433]: failed to resolve: could not find `RustcDecodable` in `core` + --> $DIR/builtin-std-paths-fail.rs:3:11 + | +LL | core::RustcDecodable, + | ^^^^^^^^^^^^^^ could not find `RustcDecodable` in `core` + +error[E0433]: failed to resolve: could not find `bench` in `std` + --> $DIR/builtin-std-paths-fail.rs:15:8 + | +LL | #[std::bench] + | ^^^^^ could not find `bench` in `std` + +error[E0433]: failed to resolve: could not find `global_allocator` in `std` + --> $DIR/builtin-std-paths-fail.rs:16:8 + | +LL | #[std::global_allocator] + | ^^^^^^^^^^^^^^^^ could not find `global_allocator` in `std` + +error[E0433]: failed to resolve: could not find `test_case` in `std` + --> $DIR/builtin-std-paths-fail.rs:17:8 + | +LL | #[std::test_case] + | ^^^^^^^^^ could not find `test_case` in `std` + +error[E0433]: failed to resolve: could not find `test` in `std` + --> $DIR/builtin-std-paths-fail.rs:18:8 + | +LL | #[std::test] + | ^^^^ could not find `test` in `std` + +error[E0433]: failed to resolve: could not find `RustcDecodable` in `std` + --> $DIR/builtin-std-paths-fail.rs:12:10 + | +LL | std::RustcDecodable, + | ^^^^^^^^^^^^^^ could not find `RustcDecodable` in `std` + +error[E0433]: failed to resolve: could not find `RustcDecodable` in `std` + --> $DIR/builtin-std-paths-fail.rs:13:10 + | +LL | std::RustcDecodable, + | ^^^^^^^^^^^^^^ could not find `RustcDecodable` in `std` + +error: aborting due to 12 previous errors + +For more information about this error, try `rustc --explain E0433`. diff --git a/src/test/ui/macros/builtin-std-paths.rs b/src/test/ui/macros/builtin-std-paths.rs new file mode 100644 index 0000000000000..2083f9ba3dc34 --- /dev/null +++ b/src/test/ui/macros/builtin-std-paths.rs @@ -0,0 +1,32 @@ +// check-pass + +#[derive( + core::clone::Clone, + core::marker::Copy, + core::fmt::Debug, + core::default::Default, + core::cmp::Eq, + core::hash::Hash, + core::cmp::Ord, + core::cmp::PartialEq, + core::cmp::PartialOrd, +)] +struct Core; + +#[derive( + std::clone::Clone, + std::marker::Copy, + std::fmt::Debug, + std::default::Default, + std::cmp::Eq, + std::hash::Hash, + std::cmp::Ord, + std::cmp::PartialEq, + std::cmp::PartialOrd, +)] +struct Std; + +fn main() { + core::column!(); + std::column!(); +} From 1ee37cd87245bb9e21a500bc1dc4185f3afea8a1 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 9 Aug 2019 19:25:34 -0400 Subject: [PATCH 055/148] Cleanup historical stability comments These weren't removed by ccbcc720a679ae76155a8 most likely by accident, let's clean them up now. --- src/librustc_driver/lib.rs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 77b7ef96d3f6c..e9d85a53d1e42 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -957,14 +957,11 @@ fn print_flag_list(cmdline_opt: &str, /// otherwise returns `None`. /// /// The compiler's handling of options is a little complicated as it ties into -/// our stability story, and it's even *more* complicated by historical -/// accidents. The current intention of each compiler option is to have one of -/// three modes: +/// our stability story. The current intention of each compiler option is to +/// have one of two modes: /// /// 1. An option is stable and can be used everywhere. -/// 2. An option is unstable, but was historically allowed on the stable -/// channel. -/// 3. An option is unstable, and can only be used on nightly. +/// 2. An option is unstable, and can only be used on nightly. /// /// Like unstable library and language features, however, unstable options have /// always required a form of "opt in" to indicate that you're using them. This @@ -1007,19 +1004,13 @@ pub fn handle_options(args: &[String]) -> Option { // this option that was passed. // * If we're a nightly compiler, then unstable options are now unlocked, so // we're good to go. - // * Otherwise, if we're a truly unstable option then we generate an error + // * Otherwise, if we're an unstable option then we generate an error // (unstable option being used on stable) - // * If we're a historically stable-but-should-be-unstable option then we - // emit a warning that we're going to turn this into an error soon. nightly_options::check_nightly_options(&matches, &config::rustc_optgroups()); if matches.opt_present("h") || matches.opt_present("help") { - // Only show unstable options in --help if we *really* accept unstable - // options, which catches the case where we got `-Z unstable-options` on - // the stable channel of Rust which was accidentally allowed - // historically. - usage(matches.opt_present("verbose"), - nightly_options::is_unstable_enabled(&matches)); + // Only show unstable options in --help if we accept unstable options. + usage(matches.opt_present("verbose"), nightly_options::is_unstable_enabled(&matches)); return None; } From 352c6d036f874728511a9e761cd745559999a967 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 10 Aug 2019 08:47:22 +0200 Subject: [PATCH 056/148] .gitignore: Readd `/tmp/` It is produced during `./x.py test` --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a0b491f42789a..3b7c613e65ef9 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ __pycache__/ /src/libcore/unicode/UnicodeData.txt /src/libcore/unicode/downloaded /target/ +/tmp/ tags tags.* TAGS From 87a8c5706deae871def85a044156362f2dafef10 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 10 Aug 2019 10:01:03 +0200 Subject: [PATCH 057/148] Explain why `/tmp/` is ignored --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3b7c613e65ef9..a7dd5a13b6f48 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ __pycache__/ /src/libcore/unicode/UnicodeData.txt /src/libcore/unicode/downloaded /target/ +# Generated by compiletest for incremental: /tmp/ tags tags.* From 83b837a7f911ea56ca3c466a43a7c3958eb24b13 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 10 Aug 2019 10:39:40 +0200 Subject: [PATCH 058/148] .gitignore: Explain why `/obj/` is ignored --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a7dd5a13b6f48..81a472451d777 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ __pycache__/ /inst/ /llvm/ /mingw-build/ +# Created by default with `src/ci/docker/run.sh`: /obj/ /rustllvm/ /src/libcore/unicode/DerivedCoreProperties.txt From e2e8746acc1d4fd236552a59f54b732680b4524e Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 5 Aug 2019 21:18:50 +0300 Subject: [PATCH 059/148] resolve: Move late resolution into a separate visitor Move `Resolver` fields specific to late resolution to the new visitor. The `current_module` field from `Resolver` is replaced with two `current_module`s in `LateResolutionVisitor` and `BuildReducedGraphVisitor`. Outside of those visitors `current_module` is replaced by passing `parent_scope` to more functions and using the parent module from it. Visibility resolution no longer have access to later resolution methods and has to use early resolution, so its diagnostics in case of errors regress slightly. --- src/librustc_resolve/build_reduced_graph.rs | 111 +++-- src/librustc_resolve/check_unused.rs | 4 - src/librustc_resolve/diagnostics.rs | 30 +- src/librustc_resolve/lib.rs | 417 +++++++++++------- src/librustc_resolve/macros.rs | 61 ++- src/librustc_resolve/resolve_imports.rs | 53 +-- src/test/ui/hygiene/privacy-early.rs | 17 + src/test/ui/hygiene/privacy-early.stderr | 21 + src/test/ui/resolve/resolve-bad-visibility.rs | 4 +- .../ui/resolve/resolve-bad-visibility.stderr | 21 +- .../ui/resolve/visibility-indeterminate.rs | 5 + .../resolve/visibility-indeterminate.stderr | 19 + src/test/ui/span/visibility-ty-params.stderr | 14 +- 13 files changed, 477 insertions(+), 300 deletions(-) create mode 100644 src/test/ui/hygiene/privacy-early.rs create mode 100644 src/test/ui/hygiene/privacy-early.stderr create mode 100644 src/test/ui/resolve/visibility-indeterminate.rs create mode 100644 src/test/ui/resolve/visibility-indeterminate.stderr diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 9d01f33002940..668daaba643a8 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -19,6 +19,7 @@ use rustc::middle::cstore::CrateStore; use rustc_metadata::cstore::LoadedMacro; use std::cell::Cell; +use std::ops::{Deref, DerefMut}; use std::ptr; use rustc_data_structures::sync::Lrc; @@ -115,7 +116,7 @@ impl<'a> Resolver<'a> { parent_prefix: &[Segment], nested: bool, // The whole `use` item - parent_scope: ParentScope<'a>, + parent_scope: &ParentScope<'a>, item: &Item, vis: ty::Visibility, root_span: Span, @@ -249,7 +250,7 @@ impl<'a> Resolver<'a> { root_span, item.id, vis, - parent_scope, + parent_scope.clone(), ); } ast::UseTreeKind::Glob => { @@ -266,7 +267,7 @@ impl<'a> Resolver<'a> { root_span, item.id, vis, - parent_scope, + parent_scope.clone(), ); } ast::UseTreeKind::Nested(ref items) => { @@ -297,7 +298,7 @@ impl<'a> Resolver<'a> { // This particular use tree tree, id, &prefix, true, // The whole `use` item - parent_scope.clone(), item, vis, root_span, + parent_scope, item, vis, root_span, ); } @@ -327,14 +328,16 @@ impl<'a> Resolver<'a> { } } } +} +impl<'a> BuildReducedGraphVisitor<'_, 'a> { /// Constructs the reduced graph for one item. - fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScope<'a>) { + fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: &ParentScope<'a>) { let parent = parent_scope.module; let expansion = parent_scope.expansion; let ident = item.ident.gensym_if_underscore(); let sp = item.span; - let vis = self.resolve_visibility(&item.vis); + let vis = self.resolve_visibility(&item.vis, parent_scope); match item.node { ItemKind::Use(ref use_tree) => { @@ -361,7 +364,9 @@ impl<'a> Resolver<'a> { } else if orig_name == Some(kw::SelfLower) { self.graph_root } else { - let crate_id = self.crate_loader.process_extern_crate(item, &self.definitions); + let crate_id = self.resolver.crate_loader.process_extern_crate( + item, &self.resolver.definitions + ); self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX }) }; @@ -372,13 +377,13 @@ impl<'a> Resolver<'a> { } } - let used = self.process_legacy_macro_imports(item, module, &parent_scope); + let used = self.process_legacy_macro_imports(item, module, parent_scope); let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas); let directive = self.arenas.alloc_import_directive(ImportDirective { root_id: item.id, id: item.id, - parent_scope, + parent_scope: parent_scope.clone(), imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))), subclass: ImportDirectiveSubclass::ExternCrate { source: orig_name, @@ -395,7 +400,7 @@ impl<'a> Resolver<'a> { }); self.potentially_unused_imports.push(directive); let imported_binding = self.import(binding, directive); - if ptr::eq(self.current_module, self.graph_root) { + if ptr::eq(parent, self.graph_root) { if let Some(entry) = self.extern_prelude.get(&ident.modern()) { if expansion != ExpnId::root() && orig_name.is_some() && entry.extern_crate_item.is_none() { @@ -455,7 +460,7 @@ impl<'a> Resolver<'a> { // Functions introducing procedural macros reserve a slot // in the macro namespace as well (see #52225). - self.define_macro(item, expansion, &mut LegacyScope::Empty); + self.define_macro(item, parent_scope); } // These items live in the type namespace. @@ -511,8 +516,8 @@ impl<'a> Resolver<'a> { // Record field names for error reporting. let field_names = struct_def.fields().iter().filter_map(|field| { - let field_vis = self.resolve_visibility(&field.vis); - if ctor_vis.is_at_least(field_vis, &*self) { + let field_vis = self.resolve_visibility(&field.vis, parent_scope); + if ctor_vis.is_at_least(field_vis, &*self.resolver) { ctor_vis = field_vis; } field.ident.map(|ident| ident.name) @@ -538,7 +543,7 @@ impl<'a> Resolver<'a> { // Record field names for error reporting. let field_names = vdata.fields().iter().filter_map(|field| { - self.resolve_visibility(&field.vis); + self.resolve_visibility(&field.vis, parent_scope); field.ident.map(|ident| ident.name) }).collect(); let item_def_id = self.definitions.local_def_id(item.id); @@ -614,7 +619,13 @@ impl<'a> Resolver<'a> { ForeignItemKind::Macro(_) => unreachable!(), }; let parent = self.current_module; - let vis = self.resolve_visibility(&item.vis); + let parent_scope = &ParentScope { + module: self.current_module, + expansion: self.expansion, + legacy: self.current_legacy_scope, + derives: Vec::new(), + }; + let vis = self.resolver.resolve_visibility(&item.vis, parent_scope); self.define(parent, item.ident, ns, (res, vis, item.span, expn_id)); } @@ -630,7 +641,9 @@ impl<'a> Resolver<'a> { self.current_module = module; // Descend into the block. } } +} +impl<'a> Resolver<'a> { /// Builds the reduced graph for a single item in an external crate. fn build_reduced_graph_for_external_crate_res( &mut self, @@ -804,7 +817,9 @@ impl<'a> Resolver<'a> { self.session.struct_span_err(span, &msg).note(note).emit(); } } +} +impl<'a> BuildReducedGraphVisitor<'_, 'a> { /// Returns `true` if we should consider the underlying `extern crate` to be used. fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>, parent_scope: &ParentScope<'a>) -> bool { @@ -873,7 +888,7 @@ impl<'a> Resolver<'a> { ModuleOrUniformRoot::Module(module), ident, MacroNS, - None, + parent_scope, false, ident.span, ); @@ -918,22 +933,36 @@ impl<'a> Resolver<'a> { pub struct BuildReducedGraphVisitor<'a, 'b> { pub resolver: &'a mut Resolver<'b>, + pub current_module: Module<'b>, pub current_legacy_scope: LegacyScope<'b>, pub expansion: ExpnId, } +impl<'b> Deref for BuildReducedGraphVisitor<'_, 'b> { + type Target = Resolver<'b>; + fn deref(&self) -> &Self::Target { + self.resolver + } +} + +impl<'b> DerefMut for BuildReducedGraphVisitor<'_, 'b> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.resolver + } +} + impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> { let invoc_id = id.placeholder_to_expn_id(); - self.resolver.current_module.unresolved_invocations.borrow_mut().insert(invoc_id); + self.current_module.unresolved_invocations.borrow_mut().insert(invoc_id); - let invocation_data = self.resolver.arenas.alloc_invocation_data(InvocationData { - module: self.resolver.current_module, + let invocation_data = self.arenas.alloc_invocation_data(InvocationData { + module: self.current_module, parent_legacy_scope: self.current_legacy_scope, output_legacy_scope: Cell::new(None), }); - let old_invocation_data = self.resolver.invocations.insert(invoc_id, invocation_data); + let old_invocation_data = self.invocations.insert(invoc_id, invocation_data); assert!(old_invocation_data.is_none(), "invocation data is reset for an invocation"); invocation_data @@ -959,30 +988,30 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> { method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty); fn visit_item(&mut self, item: &'a Item) { + let parent_scope = &ParentScope { + module: self.current_module, + expansion: self.expansion, + legacy: self.current_legacy_scope, + derives: Vec::new(), + }; let macro_use = match item.node { ItemKind::MacroDef(..) => { - self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope); + self.current_legacy_scope = self.resolver.define_macro(item, parent_scope); return } ItemKind::Mac(..) => { self.current_legacy_scope = LegacyScope::Invocation(self.visit_invoc(item.id)); return } - ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs), + ItemKind::Mod(..) => self.contains_macro_use(&item.attrs), _ => false, }; - let orig_current_module = self.resolver.current_module; + let orig_current_module = self.current_module; let orig_current_legacy_scope = self.current_legacy_scope; - let parent_scope = ParentScope { - module: self.resolver.current_module, - expansion: self.expansion, - legacy: self.current_legacy_scope, - derives: Vec::new(), - }; - self.resolver.build_reduced_graph_for_item(item, parent_scope); + self.build_reduced_graph_for_item(item, parent_scope); visit::walk_item(self, item); - self.resolver.current_module = orig_current_module; + self.current_module = orig_current_module; if !macro_use { self.current_legacy_scope = orig_current_legacy_scope; } @@ -1002,21 +1031,21 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> { return; } - self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion); + self.build_reduced_graph_for_foreign_item(foreign_item, self.expansion); visit::walk_foreign_item(self, foreign_item); } fn visit_block(&mut self, block: &'a Block) { - let orig_current_module = self.resolver.current_module; + let orig_current_module = self.current_module; let orig_current_legacy_scope = self.current_legacy_scope; - self.resolver.build_reduced_graph_for_block(block, self.expansion); + self.build_reduced_graph_for_block(block, self.expansion); visit::walk_block(self, block); - self.resolver.current_module = orig_current_module; + self.current_module = orig_current_module; self.current_legacy_scope = orig_current_legacy_scope; } fn visit_trait_item(&mut self, item: &'a TraitItem) { - let parent = self.resolver.current_module; + let parent = self.current_module; if let TraitItemKind::Macro(_) = item.node { self.visit_invoc(item.id); @@ -1024,12 +1053,12 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> { } // Add the item to the trait info. - let item_def_id = self.resolver.definitions.local_def_id(item.id); + let item_def_id = self.definitions.local_def_id(item.id); let (res, ns) = match item.node { TraitItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS), TraitItemKind::Method(ref sig, _) => { if sig.decl.has_self() { - self.resolver.has_self.insert(item_def_id); + self.has_self.insert(item_def_id); } (Res::Def(DefKind::Method, item_def_id), ValueNS) } @@ -1040,9 +1069,9 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> { let vis = ty::Visibility::Public; self.resolver.define(parent, item.ident, ns, (res, vis, item.span, self.expansion)); - self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor + self.current_module = parent.parent.unwrap(); // nearest normal ancestor visit::walk_trait_item(self, item); - self.resolver.current_module = parent; + self.current_module = parent; } fn visit_token(&mut self, t: Token) { @@ -1058,7 +1087,7 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> { fn visit_attribute(&mut self, attr: &'a ast::Attribute) { if !attr.is_sugared_doc && is_builtin_attr(attr) { let parent_scope = ParentScope { - module: self.resolver.current_module.nearest_item_scope(), + module: self.current_module.nearest_item_scope(), expansion: self.expansion, legacy: self.current_legacy_scope, // Let's hope discerning built-in attributes from derive helpers is not necessary diff --git a/src/librustc_resolve/check_unused.rs b/src/librustc_resolve/check_unused.rs index 4fee15c59b33d..d733a32c9c38e 100644 --- a/src/librustc_resolve/check_unused.rs +++ b/src/librustc_resolve/check_unused.rs @@ -270,10 +270,6 @@ pub fn check_crate(resolver: &mut Resolver<'_>, krate: &ast::Crate) { } } - for (id, span) in resolver.unused_labels.iter() { - resolver.session.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label"); - } - let mut visitor = UnusedImportCheckVisitor { resolver, unused_imports: Default::default(), diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index aeb6f23da5aa6..c1fe7188f6dda 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -18,7 +18,7 @@ use syntax_pos::{BytePos, Span}; use crate::resolve_imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver}; use crate::{is_self_type, is_self_value, path_names_to_string, KNOWN_TOOLS}; -use crate::{CrateLint, LegacyScope, Module, ModuleKind, ModuleOrUniformRoot}; +use crate::{CrateLint, LateResolutionVisitor, LegacyScope, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{PathResult, PathSource, ParentScope, Resolver, RibKind, Scope, ScopeSet, Segment}; type Res = def::Res; @@ -78,7 +78,7 @@ fn add_module_candidates( } } -impl<'a> Resolver<'a> { +impl<'a> LateResolutionVisitor<'a, '_> { /// Handles error reporting for `smart_resolve_path_fragment` function. /// Creates base error and amends it with one short label and possibly some longer helps/notes. pub(crate) fn smart_resolve_report_errors( @@ -112,7 +112,7 @@ impl<'a> Resolver<'a> { (String::new(), "the crate root".to_string()) } else { let mod_path = &path[..path.len() - 1]; - let mod_prefix = match self.resolve_path_without_parent_scope( + let mod_prefix = match self.resolve_path( mod_path, Some(TypeNS), false, span, CrateLint::No ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => @@ -288,7 +288,9 @@ impl<'a> Resolver<'a> { } (err, candidates) } +} +impl<'a> Resolver<'a> { fn followed_by_brace(&self, span: Span) -> (bool, Option<(Span, String)>) { // HACK(estebank): find a better way to figure out that this was a // parser issue where a struct literal is being used on an expression @@ -338,7 +340,9 @@ impl<'a> Resolver<'a> { } return (followed_by_brace, closing_brace) } +} +impl<'a> LateResolutionVisitor<'a, '_> { /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment` /// function. /// Returns `true` if able to provide context-dependent help. @@ -457,7 +461,7 @@ impl<'a> Resolver<'a> { (Res::Def(DefKind::Struct, def_id), _) if ns == ValueNS => { if let Some((ctor_def, ctor_vis)) = self.struct_constructors.get(&def_id).cloned() { - let accessible_ctor = self.is_accessible(ctor_vis); + let accessible_ctor = self.is_accessible_from(ctor_vis, self.current_module); if is_expected(ctor_def) && !accessible_ctor { err.span_label( span, @@ -532,11 +536,12 @@ impl<'a> Resolver<'a> { // Look for associated items in the current trait. if let Some((module, _)) = self.current_trait_ref { + let parent_scope = &self.parent_scope(); if let Ok(binding) = self.resolve_ident_in_module( ModuleOrUniformRoot::Module(module), ident, ns, - None, + parent_scope, false, module.span, ) { @@ -553,7 +558,9 @@ impl<'a> Resolver<'a> { None } +} +impl<'a> Resolver<'a> { /// Lookup typo candidate in scope for a macro or import. fn early_lookup_typo_candidate( &mut self, @@ -569,9 +576,10 @@ impl<'a> Resolver<'a> { let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper); if filter_fn(res) { for derive in &parent_scope.derives { - let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope }; + let parent_scope = + &ParentScope { derives: Vec::new(), ..*parent_scope }; if let Ok((Some(ext), _)) = this.resolve_macro_path( - derive, Some(MacroKind::Derive), &parent_scope, false, false + derive, Some(MacroKind::Derive), parent_scope, false, false ) { suggestions.extend(ext.helper_attrs.iter().map(|name| { TypoSuggestion::from_res(*name, res) @@ -682,7 +690,9 @@ impl<'a> Resolver<'a> { _ => None, } } +} +impl<'a> LateResolutionVisitor<'a, '_> { fn lookup_typo_candidate( &mut self, path: &[Segment], @@ -750,7 +760,7 @@ impl<'a> Resolver<'a> { } else { // Search in module. let mod_path = &path[..path.len() - 1]; - if let PathResult::Module(module) = self.resolve_path_without_parent_scope( + if let PathResult::Module(module) = self.resolve_path( mod_path, Some(TypeNS), false, span, CrateLint::No ) { if let ModuleOrUniformRoot::Module(module) = module { @@ -774,7 +784,9 @@ impl<'a> Resolver<'a> { _ => None, } } +} +impl<'a> Resolver<'a> { fn lookup_import_candidates_from_module(&mut self, lookup_ident: Ident, namespace: Namespace, @@ -969,7 +981,7 @@ impl<'a> Resolver<'a> { ) { let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind); let suggestion = self.early_lookup_typo_candidate( - ScopeSet::Macro(macro_kind), &parent_scope, ident, is_expected + ScopeSet::Macro(macro_kind), parent_scope, ident, is_expected ); add_typo_suggestion(err, suggestion, ident.span); diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index ce2bc79ff6034..e11413fcda9ea 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -59,6 +59,7 @@ use log::debug; use std::cell::{Cell, RefCell}; use std::{cmp, fmt, iter, mem, ptr}; +use std::ops::{Deref, DerefMut}; use std::collections::BTreeSet; use std::mem::replace; use rustc_data_structures::ptr_key::PtrKey; @@ -537,35 +538,22 @@ enum PathSource<'a> { TupleStruct, // `m::A::B` in `::B::C`. TraitItem(Namespace), - // Path in `pub(path)` - Visibility, } impl<'a> PathSource<'a> { fn namespace(self) -> Namespace { match self { - PathSource::Type | PathSource::Trait(_) | PathSource::Struct | - PathSource::Visibility => TypeNS, + PathSource::Type | PathSource::Trait(_) | PathSource::Struct => TypeNS, PathSource::Expr(..) | PathSource::Pat | PathSource::TupleStruct => ValueNS, PathSource::TraitItem(ns) => ns, } } - fn global_by_default(self) -> bool { - match self { - PathSource::Visibility => true, - PathSource::Type | PathSource::Expr(..) | PathSource::Pat | - PathSource::Struct | PathSource::TupleStruct | - PathSource::Trait(_) | PathSource::TraitItem(..) => false, - } - } - fn defer_to_typeck(self) -> bool { match self { PathSource::Type | PathSource::Expr(..) | PathSource::Pat | PathSource::Struct | PathSource::TupleStruct => true, - PathSource::Trait(_) | PathSource::TraitItem(..) | - PathSource::Visibility => false, + PathSource::Trait(_) | PathSource::TraitItem(..) => false, } } @@ -576,7 +564,6 @@ impl<'a> PathSource<'a> { PathSource::Pat => "unit struct/variant or constant", PathSource::Struct => "struct, variant or union type", PathSource::TupleStruct => "tuple struct/variant", - PathSource::Visibility => "module", PathSource::TraitItem(ns) => match ns { TypeNS => "associated type", ValueNS => "method or associated constant", @@ -655,10 +642,6 @@ impl<'a> PathSource<'a> { Res::Def(DefKind::AssocTy, _) if ns == TypeNS => true, _ => false, }, - PathSource::Visibility => match res { - Res::Def(DefKind::Mod, _) => true, - _ => false, - }, } } @@ -675,8 +658,6 @@ impl<'a> PathSource<'a> { __diagnostic_used!(E0574); __diagnostic_used!(E0575); __diagnostic_used!(E0576); - __diagnostic_used!(E0577); - __diagnostic_used!(E0578); match (self, has_unexpected_resolution) { (PathSource::Trait(_), true) => "E0404", (PathSource::Trait(_), false) => "E0405", @@ -690,8 +671,6 @@ impl<'a> PathSource<'a> { (PathSource::Pat, false) | (PathSource::TupleStruct, false) => "E0531", (PathSource::TraitItem(..), true) => "E0575", (PathSource::TraitItem(..), false) => "E0576", - (PathSource::Visibility, true) => "E0577", - (PathSource::Visibility, false) => "E0578", } } } @@ -801,8 +780,80 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder { } } +struct LateResolutionVisitor<'a, 'b> { + resolver: &'b mut Resolver<'a>, + + /// The module that represents the current item scope. + current_module: Module<'a>, + + /// The current set of local scopes for types and values. + /// FIXME #4948: Reuse ribs to avoid allocation. + ribs: PerNS>>, + + /// The current set of local scopes, for labels. + label_ribs: Vec>, + + /// The trait that the current context can refer to. + current_trait_ref: Option<(Module<'a>, TraitRef)>, + + /// The current trait's associated types' ident, used for diagnostic suggestions. + current_trait_assoc_types: Vec, + + /// The current self type if inside an impl (used for better errors). + current_self_type: Option, + + /// The current self item if inside an ADT (used for better errors). + current_self_item: Option, + + /// A list of labels as of yet unused. Labels will be removed from this map when + /// they are used (in a `break` or `continue` statement) + unused_labels: FxHashMap, + + /// Only used for better errors on `fn(): fn()`. + current_type_ascription: Vec, +} + +impl<'a, 'b> LateResolutionVisitor<'a, '_> { + fn new(resolver: &'b mut Resolver<'a>) -> LateResolutionVisitor<'a, 'b> { + let graph_root = resolver.graph_root; + LateResolutionVisitor { + resolver, + current_module: graph_root, + ribs: PerNS { + value_ns: vec![Rib::new(ModuleRibKind(graph_root))], + type_ns: vec![Rib::new(ModuleRibKind(graph_root))], + macro_ns: vec![Rib::new(ModuleRibKind(graph_root))], + }, + label_ribs: Vec::new(), + current_trait_ref: None, + current_trait_assoc_types: Vec::new(), + current_self_type: None, + current_self_item: None, + unused_labels: Default::default(), + current_type_ascription: Vec::new(), + } + } + + fn parent_scope(&self) -> ParentScope<'a> { + ParentScope { module: self.current_module, ..self.dummy_parent_scope() } + } +} + +impl<'a> Deref for LateResolutionVisitor<'a, '_> { + type Target = Resolver<'a>; + fn deref(&self) -> &Self::Target { + self.resolver + } +} + +impl<'a> DerefMut for LateResolutionVisitor<'a, '_> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.resolver + } +} + /// Walks the whole crate in DFS order, visiting each item, resolving names as it goes. -impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> { +impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> { fn visit_item(&mut self, item: &'tcx Item) { self.resolve_item(item); } @@ -1587,28 +1638,6 @@ pub struct Resolver<'a> { /// All non-determined imports. indeterminate_imports: Vec<&'a ImportDirective<'a>>, - /// The module that represents the current item scope. - current_module: Module<'a>, - - /// The current set of local scopes for types and values. - /// FIXME #4948: Reuse ribs to avoid allocation. - ribs: PerNS>>, - - /// The current set of local scopes, for labels. - label_ribs: Vec>, - - /// The trait that the current context can refer to. - current_trait_ref: Option<(Module<'a>, TraitRef)>, - - /// The current trait's associated types' ident, used for diagnostic suggestions. - current_trait_assoc_types: Vec, - - /// The current self type if inside an impl (used for better errors). - current_self_type: Option, - - /// The current self item if inside an ADT (used for better errors). - current_self_item: Option, - /// FIXME: Refactor things so that these fields are passed through arguments and not resolver. /// We are resolving a last import segment during import validation. last_import_segment: bool, @@ -1655,10 +1684,6 @@ pub struct Resolver<'a> { pub maybe_unused_trait_imports: NodeSet, pub maybe_unused_extern_crates: Vec<(NodeId, Span)>, - /// A list of labels as of yet unused. Labels will be removed from this map when - /// they are used (in a `break` or `continue` statement) - pub unused_labels: FxHashMap, - /// Privacy errors are delayed until the end in order to deduplicate them. privacy_errors: Vec>, /// Ambiguity errors are delayed for deduplication. @@ -1703,9 +1728,6 @@ pub struct Resolver<'a> { /// it's not used during normal resolution, only for better error reporting. struct_constructors: DefIdMap<(Res, ty::Visibility)>, - /// Only used for better errors on `fn(): fn()`. - current_type_ascription: Vec, - injected_crate: Option>, /// Features enabled for this crate. @@ -1872,8 +1894,8 @@ impl<'a> Resolver<'a> { let span = path.span; let path = Segment::from_path(&path); // FIXME(Manishearth): intra-doc links won't get warned of epoch changes. - match self.resolve_path_without_parent_scope(&path, Some(namespace), true, - span, CrateLint::No) { + let parent_scope = &self.dummy_parent_scope(); + match self.resolve_path(&path, Some(namespace), parent_scope, true, span, CrateLint::No) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => Ok(module.res().unwrap()), PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => @@ -1969,18 +1991,6 @@ impl<'a> Resolver<'a> { determined_imports: Vec::new(), indeterminate_imports: Vec::new(), - current_module: graph_root, - ribs: PerNS { - value_ns: vec![Rib::new(ModuleRibKind(graph_root))], - type_ns: vec![Rib::new(ModuleRibKind(graph_root))], - macro_ns: vec![Rib::new(ModuleRibKind(graph_root))], - }, - label_ribs: Vec::new(), - - current_trait_ref: None, - current_trait_assoc_types: Vec::new(), - current_self_type: None, - current_self_item: None, last_import_segment: false, blacklisted_binding: None, @@ -2002,8 +2012,6 @@ impl<'a> Resolver<'a> { maybe_unused_trait_imports: Default::default(), maybe_unused_extern_crates: Vec::new(), - unused_labels: FxHashMap::default(), - privacy_errors: Vec::new(), ambiguity_errors: Vec::new(), use_injections: Vec::new(), @@ -2036,7 +2044,6 @@ impl<'a> Resolver<'a> { unused_macros: Default::default(), proc_macro_stubs: Default::default(), special_derives: Default::default(), - current_type_ascription: Vec::new(), injected_crate: None, active_features: features.declared_lib_features.iter().map(|(feat, ..)| *feat) @@ -2089,10 +2096,13 @@ impl<'a> Resolver<'a> { /// Entry point to crate resolution. pub fn resolve_crate(&mut self, krate: &Crate) { ImportResolver { resolver: self }.finalize_imports(); - self.current_module = self.graph_root; - self.finalize_current_module_macro_resolutions(); - visit::walk_crate(self, krate); + self.finalize_current_module_macro_resolutions(self.graph_root); + let mut late_resolution_visitor = LateResolutionVisitor::new(self); + visit::walk_crate(&mut late_resolution_visitor, krate); + for (id, span) in late_resolution_visitor.unused_labels.iter() { + self.session.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label"); + } check_unused::check_crate(self, krate); self.report_errors(krate); @@ -2287,7 +2297,9 @@ impl<'a> Resolver<'a> { None } +} +impl<'a> Resolver<'a> { /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope. /// More specifically, we proceed up the hierarchy of scopes and return the binding for /// `ident` in the first scope that defines it (or None if no scopes define it). @@ -2308,8 +2320,10 @@ impl<'a> Resolver<'a> { fn resolve_ident_in_lexical_scope(&mut self, mut ident: Ident, ns: Namespace, + parent_scope: &ParentScope<'a>, record_used_id: Option, - path_span: Span) + path_span: Span, + ribs: &[Rib<'a>]) -> Option> { assert!(ns == TypeNS || ns == ValueNS); if ident.name == kw::Invalid { @@ -2331,23 +2345,23 @@ impl<'a> Resolver<'a> { // Walk backwards up the ribs in scope. let record_used = record_used_id.is_some(); let mut module = self.graph_root; - for i in (0 .. self.ribs[ns].len()).rev() { - debug!("walk rib\n{:?}", self.ribs[ns][i].bindings); + for i in (0 .. ribs.len()).rev() { + debug!("walk rib\n{:?}", ribs[i].bindings); // Use the rib kind to determine whether we are resolving parameters // (modern hygiene) or local variables (legacy hygiene). - let rib_ident = if let AssocItemRibKind | ItemRibKind = self.ribs[ns][i].kind { + let rib_ident = if let AssocItemRibKind | ItemRibKind = ribs[i].kind { modern_ident } else { ident }; - if let Some(res) = self.ribs[ns][i].bindings.get(&rib_ident).cloned() { + if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() { // The ident resolves to a type parameter or local variable. return Some(LexicalScopeBinding::Res( - self.validate_res_from_ribs(ns, i, res, record_used, path_span), + self.validate_res_from_ribs(i, res, record_used, path_span, ribs), )); } - module = match self.ribs[ns][i].kind { + module = match ribs[i].kind { ModuleRibKind(module) => module, MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => { // If an invocation of this macro created `ident`, give up on `ident` @@ -2358,10 +2372,12 @@ impl<'a> Resolver<'a> { _ => continue, }; + let item = self.resolve_ident_in_module_unadjusted( ModuleOrUniformRoot::Module(module), ident, ns, + parent_scope, record_used, path_span, ); @@ -2386,16 +2402,15 @@ impl<'a> Resolver<'a> { self.hygienic_lexical_parent(module, &mut ident.span) }; module = unwrap_or!(opt_module, break); - let orig_current_module = self.current_module; - self.current_module = module; // Lexical resolutions can never be a privacy error. + let adjusted_parent_scope = &ParentScope { module, ..parent_scope.clone() }; let result = self.resolve_ident_in_module_unadjusted( ModuleOrUniformRoot::Module(module), ident, ns, + adjusted_parent_scope, record_used, path_span, ); - self.current_module = orig_current_module; match result { Ok(binding) => { @@ -2433,6 +2448,7 @@ impl<'a> Resolver<'a> { ModuleOrUniformRoot::Module(prelude), ident, ns, + parent_scope, false, path_span, ) { @@ -2498,7 +2514,7 @@ impl<'a> Resolver<'a> { module: ModuleOrUniformRoot<'a>, ident: Ident, ns: Namespace, - parent_scope: Option<&ParentScope<'a>>, + parent_scope: &ParentScope<'a>, record_used: bool, path_span: Span ) -> Result<&'a NameBinding<'a>, Determinacy> { @@ -2512,15 +2528,18 @@ impl<'a> Resolver<'a> { module: ModuleOrUniformRoot<'a>, mut ident: Ident, ns: Namespace, - parent_scope: Option<&ParentScope<'a>>, + parent_scope: &ParentScope<'a>, record_used: bool, path_span: Span ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> { - let orig_current_module = self.current_module; + let tmp_parent_scope; + let mut adjusted_parent_scope = parent_scope; match module { - ModuleOrUniformRoot::Module(module) => { - if let Some(def) = ident.span.modernize_and_adjust(module.expansion) { - self.current_module = self.macro_def_scope(def); + ModuleOrUniformRoot::Module(m) => { + if let Some(def) = ident.span.modernize_and_adjust(m.expansion) { + tmp_parent_scope = + ParentScope { module: self.macro_def_scope(def), ..parent_scope.clone() }; + adjusted_parent_scope = &tmp_parent_scope; } } ModuleOrUniformRoot::ExternPrelude => { @@ -2532,9 +2551,8 @@ impl<'a> Resolver<'a> { } } let result = self.resolve_ident_in_module_unadjusted_ext( - module, ident, ns, parent_scope, false, record_used, path_span, + module, ident, ns, adjusted_parent_scope, false, record_used, path_span, ); - self.current_module = orig_current_module; result } @@ -2587,7 +2605,9 @@ impl<'a> Resolver<'a> { } module } +} +impl<'a> LateResolutionVisitor<'a, '_> { // AST resolution // // We maintain a list of value ribs and type ribs. @@ -2606,8 +2626,32 @@ impl<'a> Resolver<'a> { // generate a fake "implementation scope" containing all the // implementations thus found, for compatibility with old resolve pass. + fn resolve_ident_in_lexical_scope(&mut self, + ident: Ident, + ns: Namespace, + record_used_id: Option, + path_span: Span) + -> Option> { + self.resolver.resolve_ident_in_lexical_scope( + ident, ns, &self.parent_scope(), record_used_id, path_span, &self.ribs[ns] + ) + } + + fn resolve_path( + &mut self, + path: &[Segment], + opt_ns: Option, // `None` indicates a module path in import + record_used: bool, + path_span: Span, + crate_lint: CrateLint, + ) -> PathResult<'a> { + self.resolver.resolve_path_with_ribs( + path, opt_ns, &self.parent_scope(), record_used, path_span, crate_lint, &self.ribs + ) + } + pub fn with_scope(&mut self, id: NodeId, f: F) -> T - where F: FnOnce(&mut Resolver<'_>) -> T + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) -> T { let id = self.definitions.local_def_id(id); let module = self.module_map.get(&id).cloned(); // clones a reference @@ -2617,7 +2661,7 @@ impl<'a> Resolver<'a> { self.ribs[ValueNS].push(Rib::new(ModuleRibKind(module))); self.ribs[TypeNS].push(Rib::new(ModuleRibKind(module))); - self.finalize_current_module_macro_resolutions(); + self.resolver.finalize_current_module_macro_resolutions(self.current_module); let ret = f(self); self.current_module = orig_module; @@ -2827,7 +2871,7 @@ impl<'a> Resolver<'a> { } fn with_generic_param_rib<'b, F>(&'b mut self, generic_params: GenericParameters<'a, 'b>, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { debug!("with_generic_param_rib"); match generic_params { @@ -2901,7 +2945,7 @@ impl<'a> Resolver<'a> { } fn with_label_rib(&mut self, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { self.label_ribs.push(Rib::new(NormalRibKind)); f(self); @@ -2909,7 +2953,7 @@ impl<'a> Resolver<'a> { } fn with_item_rib(&mut self, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { self.ribs[ValueNS].push(Rib::new(ItemRibKind)); self.ribs[TypeNS].push(Rib::new(ItemRibKind)); @@ -2919,7 +2963,7 @@ impl<'a> Resolver<'a> { } fn with_constant_rib(&mut self, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { debug!("with_constant_rib"); self.ribs[ValueNS].push(Rib::new(ConstantItemRibKind)); @@ -2930,7 +2974,7 @@ impl<'a> Resolver<'a> { } fn with_current_self_type(&mut self, self_type: &Ty, f: F) -> T - where F: FnOnce(&mut Resolver<'_>) -> T + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) -> T { // Handle nested impls (inside fn bodies) let previous_value = replace(&mut self.current_self_type, Some(self_type.clone())); @@ -2940,7 +2984,7 @@ impl<'a> Resolver<'a> { } fn with_current_self_item(&mut self, self_item: &Item, f: F) -> T - where F: FnOnce(&mut Resolver<'_>) -> T + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) -> T { let previous_value = replace(&mut self.current_self_item, Some(self_item.id)); let result = f(self); @@ -2950,7 +2994,7 @@ impl<'a> Resolver<'a> { /// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412. fn with_trait_items(&mut self, trait_items: &Vec, f: F) -> T - where F: FnOnce(&mut Resolver<'_>) -> T + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) -> T { let trait_assoc_types = replace( &mut self.current_trait_assoc_types, @@ -2966,7 +3010,7 @@ impl<'a> Resolver<'a> { /// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`). fn with_optional_trait_ref(&mut self, opt_trait_ref: Option<&TraitRef>, f: F) -> T - where F: FnOnce(&mut Resolver<'_>, Option) -> T + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>, Option) -> T { let mut new_val = None; let mut new_id = None; @@ -2984,7 +3028,7 @@ impl<'a> Resolver<'a> { new_id = Some(res.def_id()); let span = trait_ref.path.span; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path_without_parent_scope( + self.resolve_path( &path, Some(TypeNS), false, @@ -3003,7 +3047,7 @@ impl<'a> Resolver<'a> { } fn with_self_rib(&mut self, self_res: Res, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { let mut self_type_rib = Rib::new(NormalRibKind); @@ -3015,7 +3059,7 @@ impl<'a> Resolver<'a> { } fn with_self_struct_ctor_rib(&mut self, impl_id: DefId, f: F) - where F: FnOnce(&mut Resolver<'_>) + where F: FnOnce(&mut LateResolutionVisitor<'_, '_>) { let self_res = Res::SelfCtor(impl_id); let mut self_type_rib = Rib::new(NormalRibKind); @@ -3053,8 +3097,9 @@ impl<'a> Resolver<'a> { this.with_self_struct_ctor_rib(item_def_id, |this| { debug!("resolve_implementation with_self_struct_ctor_rib"); for impl_item in impl_items { - this.resolve_visibility(&impl_item.vis); - + this.resolver.resolve_visibility( + &impl_item.vis, &this.parent_scope() + ); // We also need a new scope for the impl item type parameters. let generic_params = HasGenericParams(&impl_item.generics, AssocItemRibKind); @@ -3129,11 +3174,12 @@ impl<'a> Resolver<'a> { // If there is a TraitRef in scope for an impl, then the method must be in the // trait. if let Some((module, _)) = self.current_trait_ref { + let parent_scope = &self.parent_scope(); if self.resolve_ident_in_module( ModuleOrUniformRoot::Module(module), ident, ns, - None, + parent_scope, false, span, ).is_err() { @@ -3282,7 +3328,7 @@ impl<'a> Resolver<'a> { self.ribs[ValueNS].push(Rib::new(ModuleRibKind(anonymous_module))); self.ribs[TypeNS].push(Rib::new(ModuleRibKind(anonymous_module))); self.current_module = anonymous_module; - self.finalize_current_module_macro_resolutions(); + self.resolver.finalize_current_module_macro_resolutions(self.current_module); } else { self.ribs[ValueNS].push(Rib::new(NormalRibKind)); } @@ -3497,7 +3543,6 @@ impl<'a> Resolver<'a> { ns, span, source.defer_to_typeck(), - source.global_by_default(), crate_lint, ) { Some(partial_res) if partial_res.unresolved_segments() == 0 => { @@ -3510,7 +3555,8 @@ impl<'a> Resolver<'a> { if let Res::Def(DefKind::Struct, def_id) = partial_res.base_res() { if let Some((ctor_res, ctor_vis)) = self.struct_constructors.get(&def_id).cloned() { - if is_expected(ctor_res) && self.is_accessible(ctor_vis) { + if is_expected(ctor_res) && + self.is_accessible_from(ctor_vis, self.current_module) { let lint = lint::builtin::LEGACY_CONSTRUCTOR_VISIBILITY; self.session.buffer_lint(lint, id, span, "private struct constructors are not usable through \ @@ -3540,8 +3586,7 @@ impl<'a> Resolver<'a> { let cl = CrateLint::No; let ns = Some(ns); if let PathResult::Module(_) | PathResult::NonModule(_) = - self.resolve_path_without_parent_scope(&std_path, ns, false, span, cl) - { + self.resolve_path(&std_path, ns, false, span, cl) { // check if we wrote `str::from_utf8` instead of `std::str::from_utf8` let item_span = path.iter().last().map(|segment| segment.ident.span) .unwrap_or(span); @@ -3678,13 +3723,12 @@ impl<'a> Resolver<'a> { primary_ns: Namespace, span: Span, defer_to_typeck: bool, - global_by_default: bool, crate_lint: CrateLint, ) -> Option { let mut fin_res = None; for (i, ns) in [primary_ns, TypeNS, ValueNS].iter().cloned().enumerate() { if i == 0 || ns != primary_ns { - match self.resolve_qpath(id, qself, path, ns, span, global_by_default, crate_lint) { + match self.resolve_qpath(id, qself, path, ns, span, crate_lint) { // If defer_to_typeck, then resolution > no resolution, // otherwise full resolution > partial resolution > no resolution. Some(partial_res) if partial_res.unresolved_segments() == 0 || @@ -3700,10 +3744,9 @@ impl<'a> Resolver<'a> { if qself.is_none() { let path_seg = |seg: &Segment| ast::PathSegment::from_ident(seg.ident); let path = Path { segments: path.iter().map(path_seg).collect(), span }; - let parent_scope = - ParentScope { module: self.current_module, ..self.dummy_parent_scope() }; + let parent_scope = &self.parent_scope(); if let Ok((_, res)) = - self.resolve_macro_path(&path, None, &parent_scope, false, false) { + self.resolve_macro_path(&path, None, parent_scope, false, false) { return Some(PartialRes::new(res)); } } @@ -3719,18 +3762,15 @@ impl<'a> Resolver<'a> { path: &[Segment], ns: Namespace, span: Span, - global_by_default: bool, crate_lint: CrateLint, ) -> Option { debug!( - "resolve_qpath(id={:?}, qself={:?}, path={:?}, \ - ns={:?}, span={:?}, global_by_default={:?})", + "resolve_qpath(id={:?}, qself={:?}, path={:?}, ns={:?}, span={:?})", id, qself, path, ns, span, - global_by_default, ); if let Some(qself) = qself { @@ -3779,13 +3819,7 @@ impl<'a> Resolver<'a> { )); } - let result = match self.resolve_path_without_parent_scope( - &path, - Some(ns), - true, - span, - crate_lint, - ) { + let result = match self.resolve_path(&path, Some(ns), true, span, crate_lint) { PathResult::NonModule(path_res) => path_res, PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => { PartialRes::new(module.res().unwrap()) @@ -3820,11 +3854,11 @@ impl<'a> Resolver<'a> { PathResult::Indeterminate => bug!("indetermined path result in resolve_qpath"), }; - if path.len() > 1 && !global_by_default && result.base_res() != Res::Err && + if path.len() > 1 && result.base_res() != Res::Err && path[0].ident.name != kw::PathRoot && path[0].ident.name != kw::DollarCrate { let unqualified_result = { - match self.resolve_path_without_parent_scope( + match self.resolve_path( &[*path.last().unwrap()], Some(ns), false, @@ -3845,23 +3879,24 @@ impl<'a> Resolver<'a> { Some(result) } +} - fn resolve_path_without_parent_scope( +impl<'a> Resolver<'a> { + fn resolve_path( &mut self, path: &[Segment], opt_ns: Option, // `None` indicates a module path in import + parent_scope: &ParentScope<'a>, record_used: bool, path_span: Span, crate_lint: CrateLint, ) -> PathResult<'a> { - // Macro and import paths must have full parent scope available during resolution, - // other paths will do okay with parent module alone. - assert!(opt_ns != None && opt_ns != Some(MacroNS)); - let parent_scope = ParentScope { module: self.current_module, ..self.dummy_parent_scope() }; - self.resolve_path(path, opt_ns, &parent_scope, record_used, path_span, crate_lint) + self.resolve_path_with_ribs( + path, opt_ns, parent_scope, record_used, path_span, crate_lint, &Default::default() + ) } - fn resolve_path( + fn resolve_path_with_ribs( &mut self, path: &[Segment], opt_ns: Option, // `None` indicates a module path in import @@ -3869,11 +3904,11 @@ impl<'a> Resolver<'a> { record_used: bool, path_span: Span, crate_lint: CrateLint, + ribs: &PerNS>>, ) -> PathResult<'a> { let mut module = None; let mut allow_super = true; let mut second_binding = None; - self.current_module = parent_scope.module; debug!( "resolve_path(path={:?}, opt_ns={:?}, record_used={:?}, \ @@ -3910,7 +3945,7 @@ impl<'a> Resolver<'a> { if allow_super && name == kw::Super { let mut ctxt = ident.span.ctxt().modern(); let self_module = match i { - 0 => Some(self.resolve_self(&mut ctxt, self.current_module)), + 0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)), _ => match module { Some(ModuleOrUniformRoot::Module(module)) => Some(module), _ => None, @@ -3935,7 +3970,7 @@ impl<'a> Resolver<'a> { if name == kw::SelfLower { let mut ctxt = ident.span.ctxt().modern(); module = Some(ModuleOrUniformRoot::Module( - self.resolve_self(&mut ctxt, self.current_module))); + self.resolve_self(&mut ctxt, parent_scope.module))); continue; } if name == kw::PathRoot && ident.span.rust_2018() { @@ -3980,7 +4015,9 @@ impl<'a> Resolver<'a> { } let binding = if let Some(module) = module { - self.resolve_ident_in_module(module, ident, ns, None, record_used, path_span) + self.resolve_ident_in_module( + module, ident, ns, parent_scope, record_used, path_span + ) } else if opt_ns.is_none() || opt_ns == Some(MacroNS) { assert!(ns == TypeNS); let scopes = if opt_ns.is_none() { ScopeSet::Import(ns) } else { ScopeSet::Module }; @@ -3989,7 +4026,9 @@ impl<'a> Resolver<'a> { } else { let record_used_id = if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None }; - match self.resolve_ident_in_lexical_scope(ident, ns, record_used_id, path_span) { + match self.resolve_ident_in_lexical_scope( + ident, ns, parent_scope, record_used_id, path_span, &ribs[ns] + ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Ok(binding), // we found a local variable or type param @@ -4176,17 +4215,17 @@ impl<'a> Resolver<'a> { // Validate a local resolution (from ribs). fn validate_res_from_ribs( &mut self, - ns: Namespace, rib_index: usize, res: Res, record_used: bool, span: Span, + all_ribs: &[Rib<'a>], ) -> Res { debug!("validate_res_from_ribs({:?})", res); - let ribs = &self.ribs[ns][rib_index + 1..]; + let ribs = &all_ribs[rib_index + 1..]; // An invalid forward use of a type parameter from a previous default. - if let ForwardTyParamBanRibKind = self.ribs[ns][rib_index].kind { + if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind { if record_used { resolve_error(self, span, ResolutionError::ForwardDeclaredTyParam); } @@ -4195,7 +4234,7 @@ impl<'a> Resolver<'a> { } // An invalid use of a type parameter as the type of a const parameter. - if let TyParamAsConstParamTy = self.ribs[ns][rib_index].kind { + if let TyParamAsConstParamTy = all_ribs[rib_index].kind { if record_used { resolve_error(self, span, ResolutionError::ConstParamDependentOnTypeParam); } @@ -4288,9 +4327,11 @@ impl<'a> Resolver<'a> { } res } +} +impl<'a> LateResolutionVisitor<'a, '_> { fn with_resolved_label(&mut self, label: OptionRun"#, - url, test_escaped, channel, edition_string - )) - }); - - let tooltip = if ignore { - Some(("This example is not tested".to_owned(), "ignore")) - } else if compile_fail { - Some(("This example deliberately fails to compile".to_owned(), "compile_fail")) - } else if explicit_edition { - Some((format!("This code runs with edition {}", edition), "edition")) + // insert newline to clearly separate it from the + // previous block so we can shorten the html output + let mut s = String::from("\n"); + let playground_button = self.playground.as_ref().and_then(|playground| { + let krate = &playground.crate_name; + let url = &playground.url; + if url.is_empty() { + return None; + } + let test = origtext.lines() + .map(|l| map_line(l).for_code()) + .collect::>>().join("\n"); + let krate = krate.as_ref().map(|s| &**s); + let (test, _) = test::make_test(&test, krate, false, + &Default::default(), edition); + let channel = if test.contains("#![feature(") { + "&version=nightly" } else { - None + "" }; - if let Some((s1, s2)) = tooltip { - s.push_str(&highlight::render_with_highlighting( - &text, - Some(&format!("rust-example-rendered{}", - if ignore { " ignore" } - else if compile_fail { " compile_fail" } - else if explicit_edition { " edition " } - else { "" })), - playground_button.as_ref().map(String::as_str), - Some((s1.as_str(), s2)))); - Some(Event::Html(s.into())) - } else { - s.push_str(&highlight::render_with_highlighting( - &text, - Some(&format!("rust-example-rendered{}", - if ignore { " ignore" } - else if compile_fail { " compile_fail" } - else if explicit_edition { " edition " } - else { "" })), - playground_button.as_ref().map(String::as_str), - None)); - Some(Event::Html(s.into())) + let edition_string = format!("&edition={}", edition); + + // These characters don't need to be escaped in a URI. + // FIXME: use a library function for percent encoding. + fn dont_escape(c: u8) -> bool { + (b'a' <= c && c <= b'z') || + (b'A' <= c && c <= b'Z') || + (b'0' <= c && c <= b'9') || + c == b'-' || c == b'_' || c == b'.' || + c == b'~' || c == b'!' || c == b'\'' || + c == b'(' || c == b')' || c == b'*' } - }) + let mut test_escaped = String::new(); + for b in test.bytes() { + if dont_escape(b) { + test_escaped.push(char::from(b)); + } else { + write!(test_escaped, "%{:02X}", b).unwrap(); + } + } + Some(format!( + r#"Run"#, + url, test_escaped, channel, edition_string + )) + }); + + let tooltip = if ignore { + Some(("This example is not tested".to_owned(), "ignore")) + } else if compile_fail { + Some(("This example deliberately fails to compile".to_owned(), "compile_fail")) + } else if explicit_edition { + Some((format!("This code runs with edition {}", edition), "edition")) + } else { + None + }; + + if let Some((s1, s2)) = tooltip { + s.push_str(&highlight::render_with_highlighting( + &text, + Some(&format!("rust-example-rendered{}", + if ignore { " ignore" } + else if compile_fail { " compile_fail" } + else if explicit_edition { " edition " } + else { "" })), + playground_button.as_ref().map(String::as_str), + Some((s1.as_str(), s2)))); + Some(Event::Html(s.into())) + } else { + s.push_str(&highlight::render_with_highlighting( + &text, + Some(&format!("rust-example-rendered{}", + if ignore { " ignore" } + else if compile_fail { " compile_fail" } + else if explicit_edition { " edition " } + else { "" })), + playground_button.as_ref().map(String::as_str), + None)); + Some(Event::Html(s.into())) + } } } @@ -676,7 +693,7 @@ impl LangString { impl<'a> fmt::Display for Markdown<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let Markdown(md, links, ref ids, codes, edition) = *self; + let Markdown(md, links, ref ids, codes, edition, playground) = *self; let mut ids = ids.borrow_mut(); // This is actually common enough to special-case @@ -695,7 +712,7 @@ impl<'a> fmt::Display for Markdown<'a> { let p = HeadingLinks::new(p, None, &mut ids); let p = LinkReplacer::new(p, links); - let p = CodeBlocks::new(p, codes, edition); + let p = CodeBlocks::new(p, codes, edition, playground); let p = Footnotes::new(p); html::push_html(&mut s, p); @@ -705,7 +722,7 @@ impl<'a> fmt::Display for Markdown<'a> { impl<'a> fmt::Display for MarkdownWithToc<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let MarkdownWithToc(md, ref ids, codes, edition) = *self; + let MarkdownWithToc(md, ref ids, codes, edition, playground) = *self; let mut ids = ids.borrow_mut(); let p = Parser::new_ext(md, opts()); @@ -716,7 +733,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> { { let p = HeadingLinks::new(p, Some(&mut toc), &mut ids); - let p = CodeBlocks::new(p, codes, edition); + let p = CodeBlocks::new(p, codes, edition, playground); let p = Footnotes::new(p); html::push_html(&mut s, p); } @@ -729,7 +746,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> { impl<'a> fmt::Display for MarkdownHtml<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let MarkdownHtml(md, ref ids, codes, edition) = *self; + let MarkdownHtml(md, ref ids, codes, edition, playground) = *self; let mut ids = ids.borrow_mut(); // This is actually common enough to special-case @@ -745,7 +762,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> { let mut s = String::with_capacity(md.len() * 3 / 2); let p = HeadingLinks::new(p, None, &mut ids); - let p = CodeBlocks::new(p, codes, edition); + let p = CodeBlocks::new(p, codes, edition, playground); let p = Footnotes::new(p); html::push_html(&mut s, p); diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index eb88c72da9eeb..8a7dfebdbbc1a 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -170,6 +170,7 @@ struct Context { /// The map used to ensure all generated 'id=' attributes are unique. id_map: Rc>, pub shared: Arc, + playground: Option, } struct SharedContext { @@ -574,9 +575,11 @@ pub fn run(mut krate: clean::Crate, }; // If user passed in `--playground-url` arg, we fill in crate name here + let mut playground = None; if let Some(url) = playground_url { - markdown::PLAYGROUND.with(|slot| { - *slot.borrow_mut() = Some((Some(krate.name.clone()), url)); + playground = Some(markdown::Playground { + crate_name: Some(krate.name.clone()), + url, }); } @@ -592,9 +595,9 @@ pub fn run(mut krate: clean::Crate, scx.layout.logo = s.to_string(); } (sym::html_playground_url, Some(s)) => { - markdown::PLAYGROUND.with(|slot| { - let name = krate.name.clone(); - *slot.borrow_mut() = Some((Some(name), s.to_string())); + playground = Some(markdown::Playground { + crate_name: Some(krate.name.clone()), + url: s.to_string(), }); } (sym::issue_tracker_base_url, Some(s)) => { @@ -618,6 +621,7 @@ pub fn run(mut krate: clean::Crate, edition, id_map: Rc::new(RefCell::new(id_map)), shared: Arc::new(scx), + playground, }; // Crawl the crate to build various caches used for the output @@ -2592,7 +2596,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>, if is_hidden { " hidden" } else { "" }, prefix, Markdown(md_text, &links, RefCell::new(&mut ids), - cx.codes, cx.edition)) + cx.codes, cx.edition, &cx.playground)) } fn document_short( @@ -2957,7 +2961,8 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { if let Some(note) = note { let mut ids = cx.id_map.borrow_mut(); - let html = MarkdownHtml(¬e, RefCell::new(&mut ids), error_codes, cx.edition); + let html = MarkdownHtml( + ¬e, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground); message.push_str(&format!(": {}", html)); } stability.push(format!("

{}
", message)); @@ -3006,7 +3011,13 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { message = format!( "
{}{}
", message, - MarkdownHtml(&unstable_reason, RefCell::new(&mut ids), error_codes, cx.edition) + MarkdownHtml( + &unstable_reason, + RefCell::new(&mut ids), + error_codes, + cx.edition, + &cx.playground, + ) ); } @@ -4237,7 +4248,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt let mut ids = cx.id_map.borrow_mut(); write!(w, "
{}
", Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids), - cx.codes, cx.edition))?; + cx.codes, cx.edition, &cx.playground))?; } } diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 50a647f244db5..b7dd6c30f09f9 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -60,9 +60,10 @@ pub fn render( }; let playground_url = options.markdown_playground_url .or(options.playground_url); - if let Some(playground) = playground_url { - markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); }); - } + let playground = playground_url.map(|url| markdown::Playground { + crate_name: None, + url, + }); let mut out = match File::create(&output) { Err(e) => { @@ -82,9 +83,9 @@ pub fn render( let mut ids = IdMap::new(); let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()); let text = if !options.markdown_no_toc { - MarkdownWithToc(text, RefCell::new(&mut ids), error_codes, edition).to_string() + MarkdownWithToc(text, RefCell::new(&mut ids), error_codes, edition, &playground).to_string() } else { - Markdown(text, &[], RefCell::new(&mut ids), error_codes, edition).to_string() + Markdown(text, &[], RefCell::new(&mut ids), error_codes, edition, &playground).to_string() }; let err = write!( diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index c31a5069e4673..33987b0b54213 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -16,7 +16,7 @@ use std::cell::RefCell; use syntax::edition::DEFAULT_EDITION; use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata}; -use rustdoc::html::markdown::{Markdown, IdMap, ErrorCodes, PLAYGROUND}; +use rustdoc::html::markdown::{Markdown, IdMap, ErrorCodes, Playground}; use rustc_serialize::json; enum OutputFormat { @@ -95,9 +95,13 @@ impl Formatter for HTMLFormatter { match info.description { Some(ref desc) => { let mut id_map = self.0.borrow_mut(); + let playground = Playground { + crate_name: None, + url: String::from("https://play.rust-lang.org/"), + }; write!(output, "{}", Markdown(desc, &[], RefCell::new(&mut id_map), - ErrorCodes::Yes, DEFAULT_EDITION))? + ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)))? }, None => write!(output, "

No description.

\n")?, } @@ -260,9 +264,6 @@ fn parse_args() -> (OutputFormat, PathBuf) { fn main() { env_logger::init(); - PLAYGROUND.with(|slot| { - *slot.borrow_mut() = Some((None, String::from("https://play.rust-lang.org/"))); - }); let (format, dst) = parse_args(); let result = syntax::with_default_globals(move || { main_with_result(format, &dst) From c250b5fd033ebd8257cca8ee537e752355a151c3 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 10 Aug 2019 18:27:17 -0400 Subject: [PATCH 133/148] Remove fmt::Display impls on Markdown structs These impls prevent ergonomic use of the config (e.g., forcing us to use RefCell) despite all usecases for these structs only using their Display impls once. --- src/librustdoc/externalfiles.rs | 4 +- src/librustdoc/html/markdown.rs | 57 +++++++++++-------------- src/librustdoc/html/render.rs | 10 ++--- src/tools/error_index_generator/main.rs | 2 +- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index d920b7c4c9169..5d953eec31ec3 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -36,7 +36,7 @@ impl ExternalHtml { load_external_files(md_before_content, diag) .map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), - codes, edition, playground)))) + codes, edition, playground).to_string()))) ) .and_then(|(ih, bc)| load_external_files(after_content, diag) @@ -46,7 +46,7 @@ impl ExternalHtml { load_external_files(md_after_content, diag) .map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), - codes, edition, playground)))) + codes, edition, playground).to_string()))) ) .map(|(ih, bc, ac)| ExternalHtml { diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 73233a2289ccd..753832305f9ea 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1,9 +1,6 @@ //! Markdown formatting for rustdoc. //! -//! This module implements markdown formatting through the pulldown-cmark -//! rust-library. This module exposes all of the -//! functionality through a unit struct, `Markdown`, which has an implementation -//! of `fmt::Display`. Example usage: +//! This module implements markdown formatting through the pulldown-cmark library. //! //! ``` //! #![feature(rustc_private)] @@ -16,8 +13,9 @@ //! //! let s = "My *markdown* _text_"; //! let mut id_map = IdMap::new(); -//! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map), -//! ErrorCodes::Yes, Edition::Edition2015, None)); +//! let md = Markdown(s, &[], RefCell::new(&mut id_map), +//! ErrorCodes::Yes, Edition::Edition2015, None); +//! let html = md.to_string(); //! // ... something using html //! ``` @@ -27,7 +25,7 @@ use rustc_data_structures::fx::FxHashMap; use std::cell::RefCell; use std::collections::VecDeque; use std::default::Default; -use std::fmt::{self, Write}; +use std::fmt::Write; use std::borrow::Cow; use std::ops::Range; use std::str; @@ -46,9 +44,8 @@ fn opts() -> Options { Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES } -/// A tuple struct that has the `fmt::Display` trait implemented. -/// When formatted, this struct will emit the HTML corresponding to the rendered -/// version of the contained markdown string. +/// When `to_string` is called, this struct will emit the HTML corresponding to +/// the rendered version of the contained markdown string. pub struct Markdown<'a>( pub &'a str, /// A list of link replacements. @@ -691,13 +688,13 @@ impl LangString { } } -impl<'a> fmt::Display for Markdown<'a> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let Markdown(md, links, ref ids, codes, edition, playground) = *self; +impl Markdown<'_> { + pub fn to_string(self) -> String { + let Markdown(md, links, ids, codes, edition, playground) = self; let mut ids = ids.borrow_mut(); // This is actually common enough to special-case - if md.is_empty() { return Ok(()) } + if md.is_empty() { return String::new(); } let replacer = |_: &str, s: &str| { if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) { Some((replace.clone(), s.to_owned())) @@ -716,13 +713,13 @@ impl<'a> fmt::Display for Markdown<'a> { let p = Footnotes::new(p); html::push_html(&mut s, p); - fmt.write_str(&s) + s } } -impl<'a> fmt::Display for MarkdownWithToc<'a> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let MarkdownWithToc(md, ref ids, codes, edition, playground) = *self; +impl MarkdownWithToc<'_> { + pub fn to_string(self) -> String { + let MarkdownWithToc(md, ref ids, codes, edition, playground) = self; let mut ids = ids.borrow_mut(); let p = Parser::new_ext(md, opts()); @@ -738,19 +735,17 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> { html::push_html(&mut s, p); } - write!(fmt, "", toc.into_toc())?; - - fmt.write_str(&s) + format!("{}", toc.into_toc(), s) } } -impl<'a> fmt::Display for MarkdownHtml<'a> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let MarkdownHtml(md, ref ids, codes, edition, playground) = *self; +impl MarkdownHtml<'_> { + pub fn to_string(self) -> String { + let MarkdownHtml(md, ref ids, codes, edition, playground) = self; let mut ids = ids.borrow_mut(); // This is actually common enough to special-case - if md.is_empty() { return Ok(()) } + if md.is_empty() { return String::new(); } let p = Parser::new_ext(md, opts()); // Treat inline HTML as plain text. @@ -766,15 +761,15 @@ impl<'a> fmt::Display for MarkdownHtml<'a> { let p = Footnotes::new(p); html::push_html(&mut s, p); - fmt.write_str(&s) + s } } -impl<'a> fmt::Display for MarkdownSummaryLine<'a> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let MarkdownSummaryLine(md, links) = *self; +impl MarkdownSummaryLine<'_> { + pub fn to_string(self) -> String { + let MarkdownSummaryLine(md, links) = self; // This is actually common enough to special-case - if md.is_empty() { return Ok(()) } + if md.is_empty() { return String::new(); } let replacer = |_: &str, s: &str| { if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) { @@ -790,7 +785,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> { html::push_html(&mut s, LinkReplacer::new(SummaryLine::new(p), links)); - fmt.write_str(&s) + s } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 8a7dfebdbbc1a..fc4e25e3d7ff6 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2596,7 +2596,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>, if is_hidden { " hidden" } else { "" }, prefix, Markdown(md_text, &links, RefCell::new(&mut ids), - cx.codes, cx.edition, &cx.playground)) + cx.codes, cx.edition, &cx.playground).to_string()) } fn document_short( @@ -2866,7 +2866,7 @@ fn item_module(w: &mut fmt::Formatter<'_>, cx: &Context, ", name = *myitem.name.as_ref().unwrap(), stab_tags = stability_tags(myitem), - docs = MarkdownSummaryLine(doc_value, &myitem.links()), + docs = MarkdownSummaryLine(doc_value, &myitem.links()).to_string(), class = myitem.type_(), add = add, stab = stab.unwrap_or_else(|| String::new()), @@ -2963,7 +2963,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { let mut ids = cx.id_map.borrow_mut(); let html = MarkdownHtml( ¬e, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground); - message.push_str(&format!(": {}", html)); + message.push_str(&format!(": {}", html.to_string())); } stability.push(format!("
{}
", message)); } @@ -3017,7 +3017,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { error_codes, cx.edition, &cx.playground, - ) + ).to_string() ); } @@ -4248,7 +4248,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt let mut ids = cx.id_map.borrow_mut(); write!(w, "
{}
", Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids), - cx.codes, cx.edition, &cx.playground))?; + cx.codes, cx.edition, &cx.playground).to_string())?; } } diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index 33987b0b54213..89b545fb7e413 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -101,7 +101,7 @@ impl Formatter for HTMLFormatter { }; write!(output, "{}", Markdown(desc, &[], RefCell::new(&mut id_map), - ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)))? + ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())? }, None => write!(output, "

No description.

\n")?, } From 1aa0964b543e0e21cadee2fed6a7725b594e92be Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 10 Aug 2019 18:36:04 -0400 Subject: [PATCH 134/148] Drop RefCell from IdMap in markdown rendering --- src/librustdoc/externalfiles.rs | 6 ++---- src/librustdoc/html/markdown.rs | 19 +++++++------------ src/librustdoc/html/markdown/tests.rs | 12 ++++++------ src/librustdoc/html/render.rs | 9 ++++----- src/librustdoc/markdown.rs | 5 ++--- src/tools/error_index_generator/main.rs | 2 +- 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index 5d953eec31ec3..8254bc800ca46 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -6,8 +6,6 @@ use crate::syntax::feature_gate::UnstableFeatures; use crate::syntax::edition::Edition; use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground}; -use std::cell::RefCell; - #[derive(Clone, Debug)] pub struct ExternalHtml { /// Content that will be included inline in the section of a @@ -35,7 +33,7 @@ impl ExternalHtml { .and_then(|(ih, bc)| load_external_files(md_before_content, diag) .map(|m_bc| (ih, - format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), + format!("{}{}", bc, Markdown(&m_bc, &[], id_map, codes, edition, playground).to_string()))) ) .and_then(|(ih, bc)| @@ -45,7 +43,7 @@ impl ExternalHtml { .and_then(|(ih, bc, ac)| load_external_files(md_after_content, diag) .map(|m_ac| (ih, bc, - format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), + format!("{}{}", ac, Markdown(&m_ac, &[], id_map, codes, edition, playground).to_string()))) ) .map(|(ih, bc, ac)| diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 753832305f9ea..5a7deb651b00d 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -9,12 +9,10 @@ //! //! use syntax::edition::Edition; //! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes}; -//! use std::cell::RefCell; //! //! let s = "My *markdown* _text_"; //! let mut id_map = IdMap::new(); -//! let md = Markdown(s, &[], RefCell::new(&mut id_map), -//! ErrorCodes::Yes, Edition::Edition2015, None); +//! let md = Markdown(s, &[], &mut id_map, ErrorCodes::Yes, Edition::Edition2015, &None); //! let html = md.to_string(); //! // ... something using html //! ``` @@ -51,7 +49,7 @@ pub struct Markdown<'a>( /// A list of link replacements. pub &'a [(String, String)], /// The current list of used header IDs. - pub RefCell<&'a mut IdMap>, + pub &'a mut IdMap, /// Whether to allow the use of explicit error codes in doctest lang strings. pub ErrorCodes, /// Default edition to use when parsing doctests (to add a `fn main`). @@ -61,7 +59,7 @@ pub struct Markdown<'a>( /// A tuple struct like `Markdown` that renders the markdown with a table of contents. pub struct MarkdownWithToc<'a>( pub &'a str, - pub RefCell<&'a mut IdMap>, + pub &'a mut IdMap, pub ErrorCodes, pub Edition, pub &'a Option, @@ -69,7 +67,7 @@ pub struct MarkdownWithToc<'a>( /// A tuple struct like `Markdown` that renders the markdown escaping HTML tags. pub struct MarkdownHtml<'a>( pub &'a str, - pub RefCell<&'a mut IdMap>, + pub &'a mut IdMap, pub ErrorCodes, pub Edition, pub &'a Option, @@ -690,8 +688,7 @@ impl LangString { impl Markdown<'_> { pub fn to_string(self) -> String { - let Markdown(md, links, ids, codes, edition, playground) = self; - let mut ids = ids.borrow_mut(); + let Markdown(md, links, mut ids, codes, edition, playground) = self; // This is actually common enough to special-case if md.is_empty() { return String::new(); } @@ -719,8 +716,7 @@ impl Markdown<'_> { impl MarkdownWithToc<'_> { pub fn to_string(self) -> String { - let MarkdownWithToc(md, ref ids, codes, edition, playground) = self; - let mut ids = ids.borrow_mut(); + let MarkdownWithToc(md, mut ids, codes, edition, playground) = self; let p = Parser::new_ext(md, opts()); @@ -741,8 +737,7 @@ impl MarkdownWithToc<'_> { impl MarkdownHtml<'_> { pub fn to_string(self) -> String { - let MarkdownHtml(md, ref ids, codes, edition, playground) = self; - let mut ids = ids.borrow_mut(); + let MarkdownHtml(md, mut ids, codes, edition, playground) = self; // This is actually common enough to special-case if md.is_empty() { return String::new(); } diff --git a/src/librustdoc/html/markdown/tests.rs b/src/librustdoc/html/markdown/tests.rs index 681f363544a61..a95c29038d46f 100644 --- a/src/librustdoc/html/markdown/tests.rs +++ b/src/librustdoc/html/markdown/tests.rs @@ -73,8 +73,8 @@ fn test_lang_string_parse() { fn test_header() { fn t(input: &str, expect: &str) { let mut map = IdMap::new(); - let output = Markdown(input, &[], RefCell::new(&mut map), - ErrorCodes::Yes, DEFAULT_EDITION).to_string(); + let output = Markdown( + input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); assert_eq!(output, expect, "original: {}", input); } @@ -96,8 +96,8 @@ fn test_header() { fn test_header_ids_multiple_blocks() { let mut map = IdMap::new(); fn t(map: &mut IdMap, input: &str, expect: &str) { - let output = Markdown(input, &[], RefCell::new(map), - ErrorCodes::Yes, DEFAULT_EDITION).to_string(); + let output = Markdown(input, &[], map, + ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); assert_eq!(output, expect, "original: {}", input); } @@ -134,8 +134,8 @@ fn test_plain_summary_line() { fn test_markdown_html_escape() { fn t(input: &str, expect: &str) { let mut idmap = IdMap::new(); - let output = MarkdownHtml(input, RefCell::new(&mut idmap), - ErrorCodes::Yes, DEFAULT_EDITION).to_string(); + let output = MarkdownHtml(input, &mut idmap, + ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); assert_eq!(output, expect, "original: {}", input); } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index fc4e25e3d7ff6..ea97cea942820 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2595,7 +2595,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>, write!(w, "
{}{}
", if is_hidden { " hidden" } else { "" }, prefix, - Markdown(md_text, &links, RefCell::new(&mut ids), + Markdown(md_text, &links, &mut ids, cx.codes, cx.edition, &cx.playground).to_string()) } @@ -2961,8 +2961,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { if let Some(note) = note { let mut ids = cx.id_map.borrow_mut(); - let html = MarkdownHtml( - ¬e, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground); + let html = MarkdownHtml(¬e, &mut ids, error_codes, cx.edition, &cx.playground); message.push_str(&format!(": {}", html.to_string())); } stability.push(format!("
{}
", message)); @@ -3013,7 +3012,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec { message, MarkdownHtml( &unstable_reason, - RefCell::new(&mut ids), + &mut ids, error_codes, cx.edition, &cx.playground, @@ -4247,7 +4246,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) { let mut ids = cx.id_map.borrow_mut(); write!(w, "
{}
", - Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids), + Markdown(&*dox, &i.impl_item.links(), &mut ids, cx.codes, cx.edition, &cx.playground).to_string())?; } } diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index b7dd6c30f09f9..eaaae3261c728 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -1,7 +1,6 @@ use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; -use std::cell::RefCell; use errors; use testing; @@ -83,9 +82,9 @@ pub fn render( let mut ids = IdMap::new(); let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()); let text = if !options.markdown_no_toc { - MarkdownWithToc(text, RefCell::new(&mut ids), error_codes, edition, &playground).to_string() + MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).to_string() } else { - Markdown(text, &[], RefCell::new(&mut ids), error_codes, edition, &playground).to_string() + Markdown(text, &[], &mut ids, error_codes, edition, &playground).to_string() }; let err = write!( diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index 89b545fb7e413..a9d1d9997f6ef 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -100,7 +100,7 @@ impl Formatter for HTMLFormatter { url: String::from("https://play.rust-lang.org/"), }; write!(output, "{}", - Markdown(desc, &[], RefCell::new(&mut id_map), + Markdown(desc, &[], &mut id_map, ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())? }, None => write!(output, "

No description.

\n")?, From 3b8a24d193a3b2d9e7a888338d0c2bb478892ec2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 10 Aug 2019 18:39:50 -0400 Subject: [PATCH 135/148] Reduce nesting in externalfiles implementation Utilize `?` instead of and_then/map operators --- src/librustdoc/externalfiles.rs | 42 +++++++++++---------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index 8254bc800ca46..56f1191feed0b 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -25,34 +25,20 @@ impl ExternalHtml { id_map: &mut IdMap, edition: Edition, playground: &Option) -> Option { let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()); - load_external_files(in_header, diag) - .and_then(|ih| - load_external_files(before_content, diag) - .map(|bc| (ih, bc)) - ) - .and_then(|(ih, bc)| - load_external_files(md_before_content, diag) - .map(|m_bc| (ih, - format!("{}{}", bc, Markdown(&m_bc, &[], id_map, - codes, edition, playground).to_string()))) - ) - .and_then(|(ih, bc)| - load_external_files(after_content, diag) - .map(|ac| (ih, bc, ac)) - ) - .and_then(|(ih, bc, ac)| - load_external_files(md_after_content, diag) - .map(|m_ac| (ih, bc, - format!("{}{}", ac, Markdown(&m_ac, &[], id_map, - codes, edition, playground).to_string()))) - ) - .map(|(ih, bc, ac)| - ExternalHtml { - in_header: ih, - before_content: bc, - after_content: ac, - } - ) + let ih = load_external_files(in_header, diag)?; + let bc = load_external_files(before_content, diag)?; + let m_bc = load_external_files(md_before_content, diag)?; + let bc = format!("{}{}", bc, Markdown(&m_bc, &[], id_map, + codes, edition, playground).to_string()); + let ac = load_external_files(after_content, diag)?; + let m_ac = load_external_files(md_after_content, diag)?; + let ac = format!("{}{}", ac, Markdown(&m_ac, &[], id_map, + codes, edition, playground).to_string()); + Some(ExternalHtml { + in_header: ih, + before_content: bc, + after_content: ac, + }) } } From b204232fd35ff021b4c71fb4d307731014e0345b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 11 Aug 2019 16:51:36 +0200 Subject: [PATCH 136/148] Derive Debug for NativeLibrary and NativeLibraryKind --- src/librustc/middle/cstore.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 5a580dfa420b3..d37b2367ae77e 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -87,7 +87,7 @@ pub enum LinkagePreference { RequireStatic, } -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, HashStable)] pub enum NativeLibraryKind { /// native static library (.a archive) @@ -100,7 +100,7 @@ pub enum NativeLibraryKind { NativeUnknown, } -#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)] +#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)] pub struct NativeLibrary { pub kind: NativeLibraryKind, pub name: Option, From 43de341f19e702f6efa2fe30c07dd5099b1a8efb Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 11 Aug 2019 10:54:38 -0400 Subject: [PATCH 137/148] Copy ty::Instance instead of passing by reference ty::Instance is small and Copy, we should not be adding additional indirection. --- src/librustc/ty/layout.rs | 4 ++-- src/librustc_codegen_ssa/mir/block.rs | 6 +++--- src/librustc_mir/interpret/snapshot.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index a9d1fd1fffc92..19c753bc30436 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2518,7 +2518,7 @@ where + HasTyCtxt<'tcx> + HasParamEnv<'tcx>, { - fn of_instance(cx: &C, instance: &ty::Instance<'tcx>) -> Self; + fn of_instance(cx: &C, instance: ty::Instance<'tcx>) -> Self; fn new(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self; fn new_vtable(cx: &C, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self; fn new_internal( @@ -2538,7 +2538,7 @@ where + HasTyCtxt<'tcx> + HasParamEnv<'tcx>, { - fn of_instance(cx: &C, instance: &ty::Instance<'tcx>) -> Self { + fn of_instance(cx: &C, instance: ty::Instance<'tcx>) -> Self { let sig = instance.fn_sig(cx.tcx()); let sig = cx .tcx() diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 006ebcbdec672..ce98979cc0c64 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -337,7 +337,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } _ => { (bx.get_fn(drop_fn), - FnType::of_instance(&bx, &drop_fn)) + FnType::of_instance(&bx, drop_fn)) } }; helper.do_call(self, &mut bx, fn_ty, drop_fn, args, @@ -435,7 +435,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { // Obtain the panic entry point. let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item); let instance = ty::Instance::mono(bx.tcx(), def_id); - let fn_ty = FnType::of_instance(&bx, &instance); + let fn_ty = FnType::of_instance(&bx, instance); let llfn = bx.get_fn(instance); // Codegen the actual panic invoke/call. @@ -552,7 +552,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let def_id = common::langcall(bx.tcx(), Some(span), "", lang_items::PanicFnLangItem); let instance = ty::Instance::mono(bx.tcx(), def_id); - let fn_ty = FnType::of_instance(&bx, &instance); + let fn_ty = FnType::of_instance(&bx, instance); let llfn = bx.get_fn(instance); // Codegen the actual panic invoke/call. diff --git a/src/librustc_mir/interpret/snapshot.rs b/src/librustc_mir/interpret/snapshot.rs index fad9fafbb0803..47289064f4d0d 100644 --- a/src/librustc_mir/interpret/snapshot.rs +++ b/src/librustc_mir/interpret/snapshot.rs @@ -304,7 +304,7 @@ impl_stable_hash_for!(enum crate::interpret::eval_context::StackPopCleanup { #[derive(Eq, PartialEq)] struct FrameSnapshot<'a, 'tcx> { - instance: &'a ty::Instance<'tcx>, + instance: ty::Instance<'tcx>, span: Span, return_to_block: &'a StackPopCleanup, return_place: Option>>, @@ -344,7 +344,7 @@ impl<'a, 'mir, 'tcx, Ctx> Snapshot<'a, Ctx> for &'a Frame<'mir, 'tcx> } = self; FrameSnapshot { - instance, + instance: *instance, span: *span, return_to_block, block, From e81347c3685dfc818fca2d502819ee0d1b692621 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 18:34:42 +0200 Subject: [PATCH 138/148] parser: split into {item,module}.rs --- src/libsyntax/parse/parser.rs | 2276 +------------------------- src/libsyntax/parse/parser/item.rs | 1899 +++++++++++++++++++++ src/libsyntax/parse/parser/module.rs | 332 ++++ 3 files changed, 2270 insertions(+), 2237 deletions(-) create mode 100644 src/libsyntax/parse/parser/item.rs create mode 100644 src/libsyntax/parse/parser/module.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f7dced76b64da..6537f59395f4e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2,44 +2,34 @@ mod expr; use expr::LhsExpr; - mod pat; - -use crate::ast::{AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy}; -use crate::ast::{GenericBound, TraitBoundModifier}; -use crate::ast::Unsafety; -use crate::ast::{Mod, AnonConst, Arg, Attribute, BindingMode, TraitItemKind}; -use crate::ast::{Block, BlockCheckMode}; -use crate::ast::{Constness, Crate}; -use crate::ast::Defaultness; -use crate::ast::EnumDef; -use crate::ast::{Expr, ExprKind}; -use crate::ast::{FnDecl, FnHeader}; -use crate::ast::{ForeignItem, ForeignItemKind, FunctionRetTy}; -use crate::ast::{GenericParam, GenericParamKind}; -use crate::ast::GenericArg; -use crate::ast::{Ident, ImplItem, IsAsync, IsAuto, Item, ItemKind, Local, Lifetime}; -use crate::ast::{MacStmtStyle, Mac, Mac_, MacDelimiter}; +mod item; +pub use item::AliasKind; +mod module; +pub use module::{ModulePath, ModulePathSuccess}; + +use crate::ast::{self, AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy}; +use crate::ast::{AnonConst, Arg, Attribute, BindingMode}; +use crate::ast::{Block, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind}; +use crate::ast::{FnDecl, FunctionRetTy}; +use crate::ast::{Ident, IsAsync, Local, Lifetime}; +use crate::ast::{MacStmtStyle, Mac_, MacDelimiter}; use crate::ast::{MutTy, Mutability}; -use crate::ast::{PathSegment}; -use crate::ast::{PolyTraitRef, QSelf}; -use crate::ast::{Stmt, StmtKind}; -use crate::ast::{VariantData, StructField}; +use crate::ast::{PolyTraitRef, QSelf, PathSegment}; use crate::ast::StrStyle; use crate::ast::SelfKind; -use crate::ast::{TraitItem, TraitRef, TraitObjectSyntax}; +use crate::ast::{GenericBound, TraitBoundModifier, TraitObjectSyntax}; +use crate::ast::{GenericParam, GenericParamKind, GenericArg, WhereClause}; use crate::ast::{Ty, TyKind, AssocTyConstraint, AssocTyConstraintKind, GenericBounds}; -use crate::ast::{Visibility, VisibilityKind, WhereClause, CrateSugar}; -use crate::ast::{UseTree, UseTreeKind}; -use crate::{ast, attr}; +use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; use crate::ext::base::DummyResult; use crate::ext::hygiene::SyntaxContext; -use crate::source_map::{self, SourceMap, Spanned, respan}; +use crate::source_map::{self, respan}; use crate::parse::{SeqSep, classify, literal, token}; use crate::parse::lexer::UnmatchedBrace; use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use crate::parse::token::{Token, TokenKind, DelimToken}; -use crate::parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership}; +use crate::parse::{ParseSess, Directory, DirectoryOwnership}; use crate::print::pprust; use crate::ptr::P; use crate::parse::PResult; @@ -48,25 +38,14 @@ use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint}; use crate::symbol::{kw, sym, Symbol}; use crate::parse::diagnostics::{Error, dummy_arg}; -use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError}; +use errors::{Applicability, DiagnosticId, FatalError}; use rustc_target::spec::abi::{self, Abi}; use syntax_pos::{Span, BytePos, DUMMY_SP, FileName}; use log::debug; use std::borrow::Cow; -use std::cmp; -use std::mem; -use std::path::{self, Path, PathBuf}; -use std::slice; - -#[derive(Debug)] -/// Whether the type alias or associated type is a concrete type or an opaque type -pub enum AliasKind { - /// Just a new name for the same type - Weak(P), - /// Only trait impls of the type will be usable, not the actual type itself - OpaqueTy(GenericBounds), -} +use std::{cmp, mem, slice}; +use std::path::PathBuf; bitflags::bitflags! { struct Restrictions: u8 { @@ -75,8 +54,6 @@ bitflags::bitflags! { } } -type ItemInfo = (Ident, ItemKind, Option>); - /// Specifies how to parse a path. #[derive(Copy, Clone, PartialEq)] pub enum PathStyle { @@ -390,19 +367,6 @@ fn can_continue_type_after_non_fn_ident(t: &Token) -> bool { t == &token::BinOp(token::Shl) } -/// Information about the path to a module. -pub struct ModulePath { - name: String, - path_exists: bool, - pub result: Result, -} - -pub struct ModulePathSuccess { - pub path: PathBuf, - pub directory_ownership: DirectoryOwnership, - warn: bool, -} - #[derive(Copy, Clone, Debug)] crate enum TokenExpectType { Expect, @@ -1077,120 +1041,6 @@ impl<'a> Parser<'a> { } } - /// Parses the items in a trait declaration. - pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, TraitItem> { - maybe_whole!(self, NtTraitItem, |x| x); - let attrs = self.parse_outer_attributes()?; - let mut unclosed_delims = vec![]; - let (mut item, tokens) = self.collect_tokens(|this| { - let item = this.parse_trait_item_(at_end, attrs); - unclosed_delims.append(&mut this.unclosed_delims); - item - })?; - self.unclosed_delims.append(&mut unclosed_delims); - // See `parse_item` for why this clause is here. - if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { - item.tokens = Some(tokens); - } - Ok(item) - } - - fn parse_trait_item_(&mut self, - at_end: &mut bool, - mut attrs: Vec) -> PResult<'a, TraitItem> { - let lo = self.token.span; - self.eat_bad_pub(); - let (name, node, generics) = if self.eat_keyword(kw::Type) { - self.parse_trait_item_assoc_ty()? - } else if self.is_const_item() { - self.expect_keyword(kw::Const)?; - let ident = self.parse_ident()?; - self.expect(&token::Colon)?; - let ty = self.parse_ty()?; - let default = if self.eat(&token::Eq) { - let expr = self.parse_expr()?; - self.expect(&token::Semi)?; - Some(expr) - } else { - self.expect(&token::Semi)?; - None - }; - (ident, TraitItemKind::Const(ty, default), ast::Generics::default()) - } else if let Some(mac) = self.parse_assoc_macro_invoc("trait", None, &mut false)? { - // trait item macro. - (Ident::invalid(), ast::TraitItemKind::Macro(mac), ast::Generics::default()) - } else { - let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; - - let ident = self.parse_ident()?; - let mut generics = self.parse_generics()?; - - let decl = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| { - // This is somewhat dubious; We don't want to allow - // argument names to be left off if there is a - // definition... - - // We don't allow argument names to be left off in edition 2018. - let is_name_required = p.token.span.rust_2018(); - p.parse_arg_general(true, false, |_| is_name_required) - })?; - generics.where_clause = self.parse_where_clause()?; - - let sig = ast::MethodSig { - header: FnHeader { - unsafety, - constness, - abi, - asyncness, - }, - decl, - }; - - let body = match self.token.kind { - token::Semi => { - self.bump(); - *at_end = true; - debug!("parse_trait_methods(): parsing required method"); - None - } - token::OpenDelim(token::Brace) => { - debug!("parse_trait_methods(): parsing provided method"); - *at_end = true; - let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - attrs.extend(inner_attrs.iter().cloned()); - Some(body) - } - token::Interpolated(ref nt) => { - match **nt { - token::NtBlock(..) => { - *at_end = true; - let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - attrs.extend(inner_attrs.iter().cloned()); - Some(body) - } - _ => { - return self.expected_semi_or_open_brace(); - } - } - } - _ => { - return self.expected_semi_or_open_brace(); - } - }; - (ident, ast::TraitItemKind::Method(sig, body), generics) - }; - - Ok(TraitItem { - id: ast::DUMMY_NODE_ID, - ident: name, - attrs, - generics, - node, - span: lo.to(self.prev_span), - tokens: None, - }) - } - /// Parses an optional return type `[ -> TY ]` in a function declaration. fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> { if self.eat(&token::RArrow) { @@ -1992,39 +1842,6 @@ impl<'a> Parser<'a> { })) } - /// Parses a structure field. - fn parse_name_and_ty(&mut self, - lo: Span, - vis: Visibility, - attrs: Vec) - -> PResult<'a, StructField> { - let name = self.parse_ident()?; - self.expect(&token::Colon)?; - let ty = self.parse_ty()?; - Ok(StructField { - span: lo.to(self.prev_span), - ident: Some(name), - vis, - id: ast::DUMMY_NODE_ID, - ty, - attrs, - }) - } - - /// Emits an expected-item-after-attributes error. - fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> { - let message = match attrs.last() { - Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment", - _ => "expected item after attributes", - }; - - let mut err = self.diagnostic().struct_span_err(self.prev_span, message); - if attrs.last().unwrap().is_sugared_doc { - err.span_label(self.prev_span, "this doc comment doesn't document anything"); - } - Err(err) - } - /// Parse a statement. This stops just before trailing semicolons on everything but items. /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. pub fn parse_stmt(&mut self) -> PResult<'a, Option> { @@ -2044,11 +1861,6 @@ impl<'a> Parser<'a> { self.is_keyword_ahead(1, &[kw::Fn]) } - fn is_union_item(&self) -> bool { - self.token.is_keyword(kw::Union) && - self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident()) - } - fn is_crate_vis(&self) -> bool { self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep) } @@ -2063,58 +1875,6 @@ impl<'a> Parser<'a> { self.is_keyword_ahead(2, &[kw::Trait])) } - fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility, lo: Span) - -> PResult<'a, Option>> { - let token_lo = self.token.span; - let (ident, def) = if self.eat_keyword(kw::Macro) { - let ident = self.parse_ident()?; - let tokens = if self.check(&token::OpenDelim(token::Brace)) { - match self.parse_token_tree() { - TokenTree::Delimited(_, _, tts) => tts, - _ => unreachable!(), - } - } else if self.check(&token::OpenDelim(token::Paren)) { - let args = self.parse_token_tree(); - let body = if self.check(&token::OpenDelim(token::Brace)) { - self.parse_token_tree() - } else { - self.unexpected()?; - unreachable!() - }; - TokenStream::new(vec![ - args.into(), - TokenTree::token(token::FatArrow, token_lo.to(self.prev_span)).into(), - body.into(), - ]) - } else { - self.unexpected()?; - unreachable!() - }; - - (ident, ast::MacroDef { tokens: tokens.into(), legacy: false }) - } else if self.check_keyword(sym::macro_rules) && - self.look_ahead(1, |t| *t == token::Not) && - self.look_ahead(2, |t| t.is_ident()) { - let prev_span = self.prev_span; - self.complain_if_pub_macro(&vis.node, prev_span); - self.bump(); - self.bump(); - - let ident = self.parse_ident()?; - let (delim, tokens) = self.expect_delimited_token_tree()?; - if delim != MacDelimiter::Brace && !self.eat(&token::Semi) { - self.report_invalid_macro_expansion_item(); - } - - (ident, ast::MacroDef { tokens, legacy: true }) - } else { - return Ok(None); - }; - - let span = lo.to(self.prev_span); - Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec()))) - } - fn parse_stmt_without_recovery( &mut self, macro_legacy_warnings: bool, @@ -2613,32 +2373,6 @@ impl<'a> Parser<'a> { }) } - /// Parses the following grammar: - /// - /// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty] - fn parse_trait_item_assoc_ty(&mut self) - -> PResult<'a, (Ident, TraitItemKind, ast::Generics)> { - let ident = self.parse_ident()?; - let mut generics = self.parse_generics()?; - - // Parse optional colon and param bounds. - let bounds = if self.eat(&token::Colon) { - self.parse_generic_bounds(None)? - } else { - Vec::new() - }; - generics.where_clause = self.parse_where_clause()?; - - let default = if self.eat(&token::Eq) { - Some(self.parse_ty()?) - } else { - None - }; - self.expect(&token::Semi)?; - - Ok((ident, TraitItemKind::Type(bounds, default), generics)) - } - fn parse_const_param(&mut self, preceding_attrs: Vec) -> PResult<'a, GenericParam> { self.expect_keyword(kw::Const)?; let ident = self.parse_ident()?; @@ -3134,18 +2868,6 @@ impl<'a> Parser<'a> { Ok((args, c_variadic)) } - /// Parses the argument list and result type of a function declaration. - fn parse_fn_decl(&mut self, allow_c_variadic: bool) -> PResult<'a, P> { - let (args, c_variadic) = self.parse_fn_args(true, allow_c_variadic)?; - let ret_ty = self.parse_ret_ty(true)?; - - Ok(P(FnDecl { - inputs: args, - output: ret_ty, - c_variadic, - })) - } - /// Returns the parsed optional self argument and whether a self shortcut was used. /// /// See `parse_self_arg_with_attrs` to collect attributes. @@ -3340,301 +3062,6 @@ impl<'a> Parser<'a> { })) } - /// Parses the name and optional generic types of a function header. - fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> { - let id = self.parse_ident()?; - let generics = self.parse_generics()?; - Ok((id, generics)) - } - - fn mk_item(&self, span: Span, ident: Ident, node: ItemKind, vis: Visibility, - attrs: Vec) -> P { - P(Item { - ident, - attrs, - id: ast::DUMMY_NODE_ID, - node, - vis, - span, - tokens: None, - }) - } - - /// Parses an item-position function declaration. - fn parse_item_fn(&mut self, - unsafety: Unsafety, - asyncness: Spanned, - constness: Spanned, - abi: Abi) - -> PResult<'a, ItemInfo> { - let (ident, mut generics) = self.parse_fn_header()?; - let allow_c_variadic = abi == Abi::C && unsafety == Unsafety::Unsafe; - let decl = self.parse_fn_decl(allow_c_variadic)?; - generics.where_clause = self.parse_where_clause()?; - let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - let header = FnHeader { unsafety, asyncness, constness, abi }; - Ok((ident, ItemKind::Fn(decl, header, generics, body), Some(inner_attrs))) - } - - /// Returns `true` if we are looking at `const ID` - /// (returns `false` for things like `const fn`, etc.). - fn is_const_item(&self) -> bool { - self.token.is_keyword(kw::Const) && - !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe]) - } - - /// Parses all the "front matter" for a `fn` declaration, up to - /// and including the `fn` keyword: - /// - /// - `const fn` - /// - `unsafe fn` - /// - `const unsafe fn` - /// - `extern fn` - /// - etc. - fn parse_fn_front_matter(&mut self) - -> PResult<'a, ( - Spanned, - Unsafety, - Spanned, - Abi - )> - { - let is_const_fn = self.eat_keyword(kw::Const); - let const_span = self.prev_span; - let asyncness = self.parse_asyncness(); - if let IsAsync::Async { .. } = asyncness { - self.ban_async_in_2015(self.prev_span); - } - let asyncness = respan(self.prev_span, asyncness); - let unsafety = self.parse_unsafety(); - let (constness, unsafety, abi) = if is_const_fn { - (respan(const_span, Constness::Const), unsafety, Abi::Rust) - } else { - let abi = if self.eat_keyword(kw::Extern) { - self.parse_opt_abi()?.unwrap_or(Abi::C) - } else { - Abi::Rust - }; - (respan(self.prev_span, Constness::NotConst), unsafety, abi) - }; - if !self.eat_keyword(kw::Fn) { - // It is possible for `expect_one_of` to recover given the contents of - // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't - // account for this. - if !self.expect_one_of(&[], &[])? { unreachable!() } - } - Ok((constness, unsafety, asyncness, abi)) - } - - /// Parses an impl item. - pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, ImplItem> { - maybe_whole!(self, NtImplItem, |x| x); - let attrs = self.parse_outer_attributes()?; - let mut unclosed_delims = vec![]; - let (mut item, tokens) = self.collect_tokens(|this| { - let item = this.parse_impl_item_(at_end, attrs); - unclosed_delims.append(&mut this.unclosed_delims); - item - })?; - self.unclosed_delims.append(&mut unclosed_delims); - - // See `parse_item` for why this clause is here. - if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { - item.tokens = Some(tokens); - } - Ok(item) - } - - fn parse_impl_item_(&mut self, - at_end: &mut bool, - mut attrs: Vec) -> PResult<'a, ImplItem> { - let lo = self.token.span; - let vis = self.parse_visibility(false)?; - let defaultness = self.parse_defaultness(); - let (name, node, generics) = if let Some(type_) = self.eat_type() { - let (name, alias, generics) = type_?; - let kind = match alias { - AliasKind::Weak(typ) => ast::ImplItemKind::TyAlias(typ), - AliasKind::OpaqueTy(bounds) => ast::ImplItemKind::OpaqueTy(bounds), - }; - (name, kind, generics) - } else if self.is_const_item() { - // This parses the grammar: - // ImplItemConst = "const" Ident ":" Ty "=" Expr ";" - self.expect_keyword(kw::Const)?; - let name = self.parse_ident()?; - self.expect(&token::Colon)?; - let typ = self.parse_ty()?; - self.expect(&token::Eq)?; - let expr = self.parse_expr()?; - self.expect(&token::Semi)?; - (name, ast::ImplItemKind::Const(typ, expr), ast::Generics::default()) - } else { - let (name, inner_attrs, generics, node) = self.parse_impl_method(&vis, at_end)?; - attrs.extend(inner_attrs); - (name, node, generics) - }; - - Ok(ImplItem { - id: ast::DUMMY_NODE_ID, - span: lo.to(self.prev_span), - ident: name, - vis, - defaultness, - attrs, - generics, - node, - tokens: None, - }) - } - - fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) { - match *vis { - VisibilityKind::Inherited => {} - _ => { - let mut err = if self.token.is_keyword(sym::macro_rules) { - let mut err = self.diagnostic() - .struct_span_err(sp, "can't qualify macro_rules invocation with `pub`"); - err.span_suggestion( - sp, - "try exporting the macro", - "#[macro_export]".to_owned(), - Applicability::MaybeIncorrect // speculative - ); - err - } else { - let mut err = self.diagnostic() - .struct_span_err(sp, "can't qualify macro invocation with `pub`"); - err.help("try adjusting the macro to put `pub` inside the invocation"); - err - }; - err.emit(); - } - } - } - - fn missing_assoc_item_kind_err(&self, item_type: &str, prev_span: Span) - -> DiagnosticBuilder<'a> - { - let expected_kinds = if item_type == "extern" { - "missing `fn`, `type`, or `static`" - } else { - "missing `fn`, `type`, or `const`" - }; - - // Given this code `path(`, it seems like this is not - // setting the visibility of a macro invocation, but rather - // a mistyped method declaration. - // Create a diagnostic pointing out that `fn` is missing. - // - // x | pub path(&self) { - // | ^ missing `fn`, `type`, or `const` - // pub path( - // ^^ `sp` below will point to this - let sp = prev_span.between(self.prev_span); - let mut err = self.diagnostic().struct_span_err( - sp, - &format!("{} for {}-item declaration", - expected_kinds, item_type)); - err.span_label(sp, expected_kinds); - err - } - - /// Parse a method or a macro invocation in a trait impl. - fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool) - -> PResult<'a, (Ident, Vec, ast::Generics, - ast::ImplItemKind)> { - // code copied from parse_macro_use_or_failure... abstraction! - if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(vis), at_end)? { - // method macro - Ok((Ident::invalid(), vec![], ast::Generics::default(), - ast::ImplItemKind::Macro(mac))) - } else { - let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; - let ident = self.parse_ident()?; - let mut generics = self.parse_generics()?; - let decl = self.parse_fn_decl_with_self(|p| { - p.parse_arg_general(true, false, |_| true) - })?; - generics.where_clause = self.parse_where_clause()?; - *at_end = true; - let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - let header = ast::FnHeader { abi, unsafety, constness, asyncness }; - Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method( - ast::MethodSig { header, decl }, - body - ))) - } - } - - /// Parses `trait Foo { ... }` or `trait Foo = Bar;`. - fn parse_item_trait(&mut self, is_auto: IsAuto, unsafety: Unsafety) -> PResult<'a, ItemInfo> { - let ident = self.parse_ident()?; - let mut tps = self.parse_generics()?; - - // Parse optional colon and supertrait bounds. - let bounds = if self.eat(&token::Colon) { - self.parse_generic_bounds(Some(self.prev_span))? - } else { - Vec::new() - }; - - if self.eat(&token::Eq) { - // it's a trait alias - let bounds = self.parse_generic_bounds(None)?; - tps.where_clause = self.parse_where_clause()?; - self.expect(&token::Semi)?; - if is_auto == IsAuto::Yes { - let msg = "trait aliases cannot be `auto`"; - self.struct_span_err(self.prev_span, msg) - .span_label(self.prev_span, msg) - .emit(); - } - if unsafety != Unsafety::Normal { - let msg = "trait aliases cannot be `unsafe`"; - self.struct_span_err(self.prev_span, msg) - .span_label(self.prev_span, msg) - .emit(); - } - Ok((ident, ItemKind::TraitAlias(tps, bounds), None)) - } else { - // it's a normal trait - tps.where_clause = self.parse_where_clause()?; - self.expect(&token::OpenDelim(token::Brace))?; - let mut trait_items = vec![]; - while !self.eat(&token::CloseDelim(token::Brace)) { - if let token::DocComment(_) = self.token.kind { - if self.look_ahead(1, - |tok| tok == &token::CloseDelim(token::Brace)) { - self.diagnostic().struct_span_err_with_code( - self.token.span, - "found a documentation comment that doesn't document anything", - DiagnosticId::Error("E0584".into()), - ) - .help( - "doc comments must come before what they document, maybe a \ - comment was intended with `//`?", - ) - .emit(); - self.bump(); - continue; - } - } - let mut at_end = false; - match self.parse_trait_item(&mut at_end) { - Ok(item) => trait_items.push(item), - Err(mut e) => { - e.emit(); - if !at_end { - self.recover_stmt_(SemiColonMode::Break, BlockMode::Break); - } - } - } - } - Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None)) - } - } - fn choose_generics_over_qpath(&self) -> bool { // There's an ambiguity between generic parameters and qualified paths in impls. // If we see `<` it may start both, so we have to inspect some following tokens. @@ -3660,118 +3087,6 @@ impl<'a> Parser<'a> { self.is_keyword_ahead(1, &[kw::Const])) } - fn parse_impl_body(&mut self) -> PResult<'a, (Vec, Vec)> { - self.expect(&token::OpenDelim(token::Brace))?; - let attrs = self.parse_inner_attributes()?; - - let mut impl_items = Vec::new(); - while !self.eat(&token::CloseDelim(token::Brace)) { - let mut at_end = false; - match self.parse_impl_item(&mut at_end) { - Ok(impl_item) => impl_items.push(impl_item), - Err(mut err) => { - err.emit(); - if !at_end { - self.recover_stmt_(SemiColonMode::Break, BlockMode::Break); - } - } - } - } - Ok((impl_items, attrs)) - } - - /// Parses an implementation item, `impl` keyword is already parsed. - /// - /// impl<'a, T> TYPE { /* impl items */ } - /// impl<'a, T> TRAIT for TYPE { /* impl items */ } - /// impl<'a, T> !TRAIT for TYPE { /* impl items */ } - /// - /// We actually parse slightly more relaxed grammar for better error reporting and recovery. - /// `impl` GENERICS `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}` - /// `impl` GENERICS `!`? TYPE (`where` PREDICATES)? `{` BODY `}` - fn parse_item_impl(&mut self, unsafety: Unsafety, defaultness: Defaultness) - -> PResult<'a, ItemInfo> { - // First, parse generic parameters if necessary. - let mut generics = if self.choose_generics_over_qpath() { - self.parse_generics()? - } else { - ast::Generics::default() - }; - - // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type. - let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) { - self.bump(); // `!` - ast::ImplPolarity::Negative - } else { - ast::ImplPolarity::Positive - }; - - // Parse both types and traits as a type, then reinterpret if necessary. - let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span)); - let ty_first = if self.token.is_keyword(kw::For) && - self.look_ahead(1, |t| t != &token::Lt) { - let span = self.prev_span.between(self.token.span); - self.struct_span_err(span, "missing trait in a trait impl").emit(); - P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID }) - } else { - self.parse_ty()? - }; - - // If `for` is missing we try to recover. - let has_for = self.eat_keyword(kw::For); - let missing_for_span = self.prev_span.between(self.token.span); - - let ty_second = if self.token == token::DotDot { - // We need to report this error after `cfg` expansion for compatibility reasons - self.bump(); // `..`, do not add it to expected tokens - Some(DummyResult::raw_ty(self.prev_span, true)) - } else if has_for || self.token.can_begin_type() { - Some(self.parse_ty()?) - } else { - None - }; - - generics.where_clause = self.parse_where_clause()?; - - let (impl_items, attrs) = self.parse_impl_body()?; - - let item_kind = match ty_second { - Some(ty_second) => { - // impl Trait for Type - if !has_for { - self.struct_span_err(missing_for_span, "missing `for` in a trait impl") - .span_suggestion_short( - missing_for_span, - "add `for` here", - " for ".to_string(), - Applicability::MachineApplicable, - ).emit(); - } - - let ty_first = ty_first.into_inner(); - let path = match ty_first.node { - // This notably includes paths passed through `ty` macro fragments (#46438). - TyKind::Path(None, path) => path, - _ => { - self.span_err(ty_first.span, "expected a trait, found type"); - err_path(ty_first.span) - } - }; - let trait_ref = TraitRef { path, ref_id: ty_first.id }; - - ItemKind::Impl(unsafety, polarity, defaultness, - generics, Some(trait_ref), ty_second, impl_items) - } - None => { - // impl Type - ItemKind::Impl(unsafety, polarity, defaultness, - generics, None, ty_first, impl_items) - } - }; - - Ok((Ident::invalid(), item_kind, Some(attrs))) - } - fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec> { if self.eat_keyword(kw::For) { self.expect_lt()?; @@ -3785,227 +3100,27 @@ impl<'a> Parser<'a> { } } - /// Parses `struct Foo { ... }`. - fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> { - let class_name = self.parse_ident()?; - - let mut generics = self.parse_generics()?; + /// 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)`. + /// 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 isn't valid, + /// so emit a proper diagnostic. + pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> { + maybe_whole!(self, NtVis, |x| x); - // There is a special case worth noting here, as reported in issue #17904. - // If we are parsing a tuple struct it is the case that the where clause - // should follow the field list. Like so: - // - // struct Foo(T) where T: Copy; - // - // If we are parsing a normal record-style struct it is the case - // that the where clause comes before the body, and after the generics. - // So if we look ahead and see a brace or a where-clause we begin - // parsing a record style struct. - // - // Otherwise if we look ahead and see a paren we parse a tuple-style - // struct. - - let vdata = if self.token.is_keyword(kw::Where) { - generics.where_clause = self.parse_where_clause()?; - if self.eat(&token::Semi) { - // If we see a: `struct Foo where T: Copy;` style decl. - VariantData::Unit(ast::DUMMY_NODE_ID) - } else { - // If we see: `struct Foo where T: Copy { ... }` - let (fields, recovered) = self.parse_record_struct_body()?; - VariantData::Struct(fields, recovered) - } - // No `where` so: `struct Foo;` - } else if self.eat(&token::Semi) { - VariantData::Unit(ast::DUMMY_NODE_ID) - // Record-style struct definition - } else if self.token == token::OpenDelim(token::Brace) { - let (fields, recovered) = self.parse_record_struct_body()?; - VariantData::Struct(fields, recovered) - // Tuple-style struct definition with optional where-clause. - } else if self.token == token::OpenDelim(token::Paren) { - let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID); - generics.where_clause = self.parse_where_clause()?; - self.expect(&token::Semi)?; - body - } else { - let token_str = self.this_token_descr(); - let mut err = self.fatal(&format!( - "expected `where`, `{{`, `(`, or `;` after struct name, found {}", - token_str - )); - err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name"); - return Err(err); - }; + self.expected_tokens.push(TokenType::Keyword(kw::Crate)); + if self.is_crate_vis() { + self.bump(); // `crate` + return Ok(respan(self.prev_span, VisibilityKind::Crate(CrateSugar::JustCrate))); + } - Ok((class_name, ItemKind::Struct(vdata, generics), None)) - } - - /// Parses `union Foo { ... }`. - fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> { - let class_name = self.parse_ident()?; - - let mut generics = self.parse_generics()?; - - let vdata = if self.token.is_keyword(kw::Where) { - generics.where_clause = self.parse_where_clause()?; - let (fields, recovered) = self.parse_record_struct_body()?; - VariantData::Struct(fields, recovered) - } else if self.token == token::OpenDelim(token::Brace) { - let (fields, recovered) = self.parse_record_struct_body()?; - VariantData::Struct(fields, recovered) - } else { - let token_str = self.this_token_descr(); - let mut err = self.fatal(&format!( - "expected `where` or `{{` after union name, found {}", token_str)); - err.span_label(self.token.span, "expected `where` or `{` after union name"); - return Err(err); - }; - - Ok((class_name, ItemKind::Union(vdata, generics), None)) - } - - fn parse_record_struct_body( - &mut self, - ) -> PResult<'a, (Vec, /* recovered */ bool)> { - let mut fields = Vec::new(); - let mut recovered = false; - if self.eat(&token::OpenDelim(token::Brace)) { - while self.token != token::CloseDelim(token::Brace) { - let field = self.parse_struct_decl_field().map_err(|e| { - self.recover_stmt(); - recovered = true; - e - }); - match field { - Ok(field) => fields.push(field), - Err(mut err) => { - err.emit(); - } - } - } - self.eat(&token::CloseDelim(token::Brace)); - } else { - let token_str = self.this_token_descr(); - let mut err = self.fatal(&format!( - "expected `where`, or `{{` after struct name, found {}", token_str)); - err.span_label(self.token.span, "expected `where`, or `{` after struct name"); - return Err(err); - } - - Ok((fields, recovered)) - } - - fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec> { - // This is the case where we find `struct Foo(T) where T: Copy;` - // Unit like structs are handled in parse_item_struct function - self.parse_paren_comma_seq(|p| { - let attrs = p.parse_outer_attributes()?; - let lo = p.token.span; - let vis = p.parse_visibility(true)?; - let ty = p.parse_ty()?; - Ok(StructField { - span: lo.to(ty.span), - vis, - ident: None, - id: ast::DUMMY_NODE_ID, - ty, - attrs, - }) - }).map(|(r, _)| r) - } - - /// Parses a structure field declaration. - fn parse_single_struct_field(&mut self, - lo: Span, - vis: Visibility, - attrs: Vec ) - -> PResult<'a, StructField> { - let mut seen_comma: bool = false; - let a_var = self.parse_name_and_ty(lo, vis, attrs)?; - if self.token == token::Comma { - seen_comma = true; - } - match self.token.kind { - token::Comma => { - self.bump(); - } - token::CloseDelim(token::Brace) => {} - token::DocComment(_) => { - let previous_span = self.prev_span; - let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment); - self.bump(); // consume the doc comment - let comma_after_doc_seen = self.eat(&token::Comma); - // `seen_comma` is always false, because we are inside doc block - // condition is here to make code more readable - if seen_comma == false && comma_after_doc_seen == true { - seen_comma = true; - } - if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) { - err.emit(); - } else { - if seen_comma == false { - let sp = self.sess.source_map().next_point(previous_span); - err.span_suggestion( - sp, - "missing comma here", - ",".into(), - Applicability::MachineApplicable - ); - } - return Err(err); - } - } - _ => { - let sp = self.sess.source_map().next_point(self.prev_span); - let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}", - self.this_token_descr())); - if self.token.is_ident() { - // This is likely another field; emit the diagnostic and keep going - err.span_suggestion( - sp, - "try adding a comma", - ",".into(), - Applicability::MachineApplicable, - ); - err.emit(); - } else { - return Err(err) - } - } - } - Ok(a_var) - } - - /// Parses an element of a struct declaration. - fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { - let attrs = self.parse_outer_attributes()?; - let lo = self.token.span; - let vis = self.parse_visibility(false)?; - self.parse_single_struct_field(lo, vis, attrs) - } - - /// 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)`. - /// 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 isn't valid, - /// so emit a proper diagnostic. - pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> { - maybe_whole!(self, NtVis, |x| x); - - self.expected_tokens.push(TokenType::Keyword(kw::Crate)); - if self.is_crate_vis() { - self.bump(); // `crate` - return Ok(respan(self.prev_span, VisibilityKind::Crate(CrateSugar::JustCrate))); - } - - 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 - // beginning of the current token would seem to be the "Schelling span". - return Ok(respan(self.token.span.shrink_to_lo(), VisibilityKind::Inherited)) - } - let lo = self.prev_span; + 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 + // beginning of the current token would seem to be the "Schelling span". + return Ok(respan(self.token.span.shrink_to_lo(), VisibilityKind::Inherited)) + } + let lo = self.prev_span; if self.check(&token::OpenDelim(token::Paren)) { // We don't `self.bump()` the `(` yet because this might be a struct definition where @@ -4074,606 +3189,6 @@ impl<'a> Parser<'a> { Ok(respan(lo, VisibilityKind::Public)) } - /// Parses defaultness (i.e., `default` or nothing). - fn parse_defaultness(&mut self) -> Defaultness { - // `pub` is included for better error messages - if self.check_keyword(kw::Default) && - self.is_keyword_ahead(1, &[ - kw::Impl, - kw::Const, - kw::Fn, - kw::Unsafe, - kw::Extern, - kw::Type, - kw::Pub, - ]) - { - self.bump(); // `default` - Defaultness::Default - } else { - Defaultness::Final - } - } - - /// Given a termination token, parses all of the items in a module. - fn parse_mod_items(&mut self, term: &TokenKind, inner_lo: Span) -> PResult<'a, Mod> { - let mut items = vec![]; - while let Some(item) = self.parse_item()? { - items.push(item); - self.maybe_consume_incorrect_semicolon(&items); - } - - if !self.eat(term) { - let token_str = self.this_token_descr(); - if !self.maybe_consume_incorrect_semicolon(&items) { - let mut err = self.fatal(&format!("expected item, found {}", token_str)); - err.span_label(self.token.span, "expected item"); - return Err(err); - } - } - - let hi = if self.token.span.is_dummy() { - inner_lo - } else { - self.prev_span - }; - - Ok(ast::Mod { - inner: inner_lo.to(hi), - items, - inline: true - }) - } - - fn parse_item_const(&mut self, m: Option) -> PResult<'a, ItemInfo> { - let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?; - self.expect(&token::Colon)?; - let ty = self.parse_ty()?; - self.expect(&token::Eq)?; - let e = self.parse_expr()?; - self.expect(&token::Semi)?; - let item = match m { - Some(m) => ItemKind::Static(ty, m, e), - None => ItemKind::Const(ty, e), - }; - Ok((id, item, None)) - } - - /// Parse a `mod { ... }` or `mod ;` item - fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> { - let (in_cfg, outer_attrs) = { - let mut strip_unconfigured = crate::config::StripUnconfigured { - sess: self.sess, - features: None, // don't perform gated feature checking - }; - let mut outer_attrs = outer_attrs.to_owned(); - strip_unconfigured.process_cfg_attrs(&mut outer_attrs); - (!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs) - }; - - let id_span = self.token.span; - let id = self.parse_ident()?; - if self.eat(&token::Semi) { - if in_cfg && self.recurse_into_file_modules { - // This mod is in an external file. Let's go get it! - let ModulePathSuccess { path, directory_ownership, warn } = - self.submod_path(id, &outer_attrs, id_span)?; - let (module, mut attrs) = - self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?; - // Record that we fetched the mod from an external file - if warn { - let attr = attr::mk_attr_outer( - attr::mk_word_item(Ident::with_empty_ctxt(sym::warn_directory_ownership))); - attr::mark_known(&attr); - attrs.push(attr); - } - Ok((id, ItemKind::Mod(module), Some(attrs))) - } else { - let placeholder = ast::Mod { - inner: DUMMY_SP, - items: Vec::new(), - inline: false - }; - Ok((id, ItemKind::Mod(placeholder), None)) - } - } else { - let old_directory = self.directory.clone(); - self.push_directory(id, &outer_attrs); - - self.expect(&token::OpenDelim(token::Brace))?; - let mod_inner_lo = self.token.span; - let attrs = self.parse_inner_attributes()?; - let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?; - - self.directory = old_directory; - Ok((id, ItemKind::Mod(module), Some(attrs))) - } - } - - fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) { - if let Some(path) = attr::first_attr_value_str_by_name(attrs, sym::path) { - self.directory.path.to_mut().push(&path.as_str()); - self.directory.ownership = DirectoryOwnership::Owned { relative: None }; - } else { - // We have to push on the current module name in the case of relative - // paths in order to ensure that any additional module paths from inline - // `mod x { ... }` come after the relative extension. - // - // For example, a `mod z { ... }` inside `x/y.rs` should set the current - // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`. - if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership { - if let Some(ident) = relative.take() { // remove the relative offset - self.directory.path.to_mut().push(ident.as_str()); - } - } - self.directory.path.to_mut().push(&id.as_str()); - } - } - - pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option { - if let Some(s) = attr::first_attr_value_str_by_name(attrs, sym::path) { - let s = s.as_str(); - - // On windows, the base path might have the form - // `\\?\foo\bar` in which case it does not tolerate - // mixed `/` and `\` separators, so canonicalize - // `/` to `\`. - #[cfg(windows)] - let s = s.replace("/", "\\"); - Some(dir_path.join(s)) - } else { - None - } - } - - /// Returns a path to a module. - pub fn default_submod_path( - id: ast::Ident, - relative: Option, - dir_path: &Path, - source_map: &SourceMap) -> ModulePath - { - // If we're in a foo.rs file instead of a mod.rs file, - // we need to look for submodules in - // `./foo/.rs` and `./foo//mod.rs` rather than - // `./.rs` and `.//mod.rs`. - let relative_prefix_string; - let relative_prefix = if let Some(ident) = relative { - relative_prefix_string = format!("{}{}", ident.as_str(), path::MAIN_SEPARATOR); - &relative_prefix_string - } else { - "" - }; - - let mod_name = id.to_string(); - let default_path_str = format!("{}{}.rs", relative_prefix, mod_name); - let secondary_path_str = format!("{}{}{}mod.rs", - relative_prefix, mod_name, path::MAIN_SEPARATOR); - let default_path = dir_path.join(&default_path_str); - let secondary_path = dir_path.join(&secondary_path_str); - let default_exists = source_map.file_exists(&default_path); - let secondary_exists = source_map.file_exists(&secondary_path); - - let result = match (default_exists, secondary_exists) { - (true, false) => Ok(ModulePathSuccess { - path: default_path, - directory_ownership: DirectoryOwnership::Owned { - relative: Some(id), - }, - warn: false, - }), - (false, true) => Ok(ModulePathSuccess { - path: secondary_path, - directory_ownership: DirectoryOwnership::Owned { - relative: None, - }, - warn: false, - }), - (false, false) => Err(Error::FileNotFoundForModule { - mod_name: mod_name.clone(), - default_path: default_path_str, - secondary_path: secondary_path_str, - dir_path: dir_path.display().to_string(), - }), - (true, true) => Err(Error::DuplicatePaths { - mod_name: mod_name.clone(), - default_path: default_path_str, - secondary_path: secondary_path_str, - }), - }; - - ModulePath { - name: mod_name, - path_exists: default_exists || secondary_exists, - result, - } - } - - fn submod_path(&mut self, - id: ast::Ident, - outer_attrs: &[Attribute], - id_sp: Span) - -> PResult<'a, ModulePathSuccess> { - if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) { - return Ok(ModulePathSuccess { - directory_ownership: match path.file_name().and_then(|s| s.to_str()) { - // All `#[path]` files are treated as though they are a `mod.rs` file. - // This means that `mod foo;` declarations inside `#[path]`-included - // files are siblings, - // - // Note that this will produce weirdness when a file named `foo.rs` is - // `#[path]` included and contains a `mod foo;` declaration. - // If you encounter this, it's your own darn fault :P - Some(_) => DirectoryOwnership::Owned { relative: None }, - _ => DirectoryOwnership::UnownedViaMod(true), - }, - path, - warn: false, - }); - } - - let relative = match self.directory.ownership { - DirectoryOwnership::Owned { relative } => relative, - DirectoryOwnership::UnownedViaBlock | - DirectoryOwnership::UnownedViaMod(_) => None, - }; - let paths = Parser::default_submod_path( - id, relative, &self.directory.path, self.sess.source_map()); - - match self.directory.ownership { - DirectoryOwnership::Owned { .. } => { - paths.result.map_err(|err| self.span_fatal_err(id_sp, err)) - }, - DirectoryOwnership::UnownedViaBlock => { - let msg = - "Cannot declare a non-inline module inside a block \ - unless it has a path attribute"; - let mut err = self.diagnostic().struct_span_err(id_sp, msg); - if paths.path_exists { - let msg = format!("Maybe `use` the module `{}` instead of redeclaring it", - paths.name); - err.span_note(id_sp, &msg); - } - Err(err) - } - DirectoryOwnership::UnownedViaMod(warn) => { - if warn { - if let Ok(result) = paths.result { - return Ok(ModulePathSuccess { warn: true, ..result }); - } - } - let mut err = self.diagnostic().struct_span_err(id_sp, - "cannot declare a new module at this location"); - if !id_sp.is_dummy() { - let src_path = self.sess.source_map().span_to_filename(id_sp); - if let FileName::Real(src_path) = src_path { - if let Some(stem) = src_path.file_stem() { - let mut dest_path = src_path.clone(); - dest_path.set_file_name(stem); - dest_path.push("mod.rs"); - err.span_note(id_sp, - &format!("maybe move this module `{}` to its own \ - directory via `{}`", src_path.display(), - dest_path.display())); - } - } - } - if paths.path_exists { - err.span_note(id_sp, - &format!("... or maybe `use` the module `{}` instead \ - of possibly redeclaring it", - paths.name)); - } - Err(err) - } - } - } - - /// Reads a module from a source file. - fn eval_src_mod( - &mut self, - path: PathBuf, - directory_ownership: DirectoryOwnership, - name: String, - id_sp: Span, - ) -> PResult<'a, (ast::Mod, Vec)> { - let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut(); - if let Some(i) = included_mod_stack.iter().position(|p| *p == path) { - let mut err = String::from("circular modules: "); - let len = included_mod_stack.len(); - for p in &included_mod_stack[i.. len] { - err.push_str(&p.to_string_lossy()); - err.push_str(" -> "); - } - err.push_str(&path.to_string_lossy()); - return Err(self.span_fatal(id_sp, &err[..])); - } - included_mod_stack.push(path.clone()); - drop(included_mod_stack); - - let mut p0 = - new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp); - p0.cfg_mods = self.cfg_mods; - let mod_inner_lo = p0.token.span; - let mod_attrs = p0.parse_inner_attributes()?; - let mut m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?; - m0.inline = false; - self.sess.included_mod_stack.borrow_mut().pop(); - Ok((m0, mod_attrs)) - } - - /// Parses a function declaration from a foreign module. - fn parse_item_foreign_fn( - &mut self, - vis: ast::Visibility, - lo: Span, - attrs: Vec, - extern_sp: Span, - ) -> PResult<'a, ForeignItem> { - self.expect_keyword(kw::Fn)?; - - let (ident, mut generics) = self.parse_fn_header()?; - let decl = self.parse_fn_decl(true)?; - generics.where_clause = self.parse_where_clause()?; - let hi = self.token.span; - self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?; - Ok(ast::ForeignItem { - ident, - attrs, - node: ForeignItemKind::Fn(decl, generics), - id: ast::DUMMY_NODE_ID, - span: lo.to(hi), - vis, - }) - } - - /// Parses a static item from a foreign module. - /// Assumes that the `static` keyword is already parsed. - fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec) - -> PResult<'a, ForeignItem> { - let mutbl = self.parse_mutability(); - let ident = self.parse_ident()?; - self.expect(&token::Colon)?; - let ty = self.parse_ty()?; - let hi = self.token.span; - self.expect(&token::Semi)?; - Ok(ForeignItem { - ident, - attrs, - node: ForeignItemKind::Static(ty, mutbl), - id: ast::DUMMY_NODE_ID, - span: lo.to(hi), - vis, - }) - } - - /// Parses a type from a foreign module. - fn parse_item_foreign_type(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec) - -> PResult<'a, ForeignItem> { - self.expect_keyword(kw::Type)?; - - let ident = self.parse_ident()?; - let hi = self.token.span; - self.expect(&token::Semi)?; - Ok(ast::ForeignItem { - ident, - attrs, - node: ForeignItemKind::Ty, - id: ast::DUMMY_NODE_ID, - span: lo.to(hi), - vis - }) - } - - fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> { - let error_msg = "crate name using dashes are not valid in `extern crate` statements"; - let suggestion_msg = "if the original crate name uses dashes you need to use underscores \ - in the code"; - let mut ident = if self.token.is_keyword(kw::SelfLower) { - self.parse_path_segment_ident() - } else { - self.parse_ident() - }?; - let mut idents = vec![]; - let mut replacement = vec![]; - let mut fixed_crate_name = false; - // Accept `extern crate name-like-this` for better diagnostics - let dash = token::BinOp(token::BinOpToken::Minus); - if self.token == dash { // Do not include `-` as part of the expected tokens list - while self.eat(&dash) { - fixed_crate_name = true; - replacement.push((self.prev_span, "_".to_string())); - idents.push(self.parse_ident()?); - } - } - if fixed_crate_name { - let fixed_name_sp = ident.span.to(idents.last().unwrap().span); - let mut fixed_name = format!("{}", ident.name); - for part in idents { - fixed_name.push_str(&format!("_{}", part.name)); - } - ident = Ident::from_str(&fixed_name).with_span_pos(fixed_name_sp); - - self.struct_span_err(fixed_name_sp, error_msg) - .span_label(fixed_name_sp, "dash-separated idents are not valid") - .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable) - .emit(); - } - Ok(ident) - } - - /// Parses `extern crate` links. - /// - /// # Examples - /// - /// ``` - /// extern crate foo; - /// extern crate bar as foo; - /// ``` - fn parse_item_extern_crate(&mut self, - lo: Span, - visibility: Visibility, - attrs: Vec) - -> PResult<'a, P> { - // Accept `extern crate name-like-this` for better diagnostics - let orig_name = self.parse_crate_name_with_dashes()?; - let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? { - (rename, Some(orig_name.name)) - } else { - (orig_name, None) - }; - self.expect(&token::Semi)?; - - let span = lo.to(self.prev_span); - Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs)) - } - - /// Parses `extern` for foreign ABIs modules. - /// - /// `extern` is expected to have been - /// consumed before calling this method. - /// - /// # Examples - /// - /// ```ignore (only-for-syntax-highlight) - /// extern "C" {} - /// extern {} - /// ``` - fn parse_item_foreign_mod( - &mut self, - lo: Span, - opt_abi: Option, - visibility: Visibility, - mut attrs: Vec, - extern_sp: Span, - ) -> PResult<'a, P> { - self.expect(&token::OpenDelim(token::Brace))?; - - let abi = opt_abi.unwrap_or(Abi::C); - - attrs.extend(self.parse_inner_attributes()?); - - let mut foreign_items = vec![]; - while !self.eat(&token::CloseDelim(token::Brace)) { - foreign_items.push(self.parse_foreign_item(extern_sp)?); - } - - let prev_span = self.prev_span; - let m = ast::ForeignMod { - abi, - items: foreign_items - }; - let invalid = Ident::invalid(); - Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs)) - } - - /// Parses `type Foo = Bar;` or returns `None` - /// without modifying the parser state. - fn eat_type(&mut self) -> Option> { - // This parses the grammar: - // Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";" - if self.eat_keyword(kw::Type) { - Some(self.parse_type_alias()) - } else { - None - } - } - - /// Parses a type alias or opaque type. - fn parse_type_alias(&mut self) -> PResult<'a, (Ident, AliasKind, ast::Generics)> { - let ident = self.parse_ident()?; - let mut tps = self.parse_generics()?; - tps.where_clause = self.parse_where_clause()?; - self.expect(&token::Eq)?; - let alias = if self.check_keyword(kw::Impl) { - self.bump(); - let bounds = self.parse_generic_bounds(Some(self.prev_span))?; - AliasKind::OpaqueTy(bounds) - } else { - let ty = self.parse_ty()?; - AliasKind::Weak(ty) - }; - self.expect(&token::Semi)?; - Ok((ident, alias, tps)) - } - - /// Parses the part of an enum declaration following the `{`. - fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> { - let mut variants = Vec::new(); - while self.token != token::CloseDelim(token::Brace) { - let variant_attrs = self.parse_outer_attributes()?; - let vlo = self.token.span; - - self.eat_bad_pub(); - let ident = self.parse_ident()?; - - let struct_def = if self.check(&token::OpenDelim(token::Brace)) { - // Parse a struct variant. - let (fields, recovered) = self.parse_record_struct_body()?; - VariantData::Struct(fields, recovered) - } else if self.check(&token::OpenDelim(token::Paren)) { - VariantData::Tuple( - self.parse_tuple_struct_body()?, - ast::DUMMY_NODE_ID, - ) - } else { - VariantData::Unit(ast::DUMMY_NODE_ID) - }; - - let disr_expr = if self.eat(&token::Eq) { - Some(AnonConst { - id: ast::DUMMY_NODE_ID, - value: self.parse_expr()?, - }) - } else { - None - }; - - let vr = ast::Variant_ { - ident, - id: ast::DUMMY_NODE_ID, - attrs: variant_attrs, - data: struct_def, - disr_expr, - }; - variants.push(respan(vlo.to(self.prev_span), vr)); - - if !self.eat(&token::Comma) { - if self.token.is_ident() && !self.token.is_reserved_ident() { - let sp = self.sess.source_map().next_point(self.prev_span); - self.struct_span_err(sp, "missing comma") - .span_suggestion_short( - sp, - "missing comma", - ",".to_owned(), - Applicability::MaybeIncorrect, - ) - .emit(); - } else { - break; - } - } - } - self.expect(&token::CloseDelim(token::Brace))?; - - Ok(ast::EnumDef { variants }) - } - - /// Parses an enum declaration. - fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> { - let id = self.parse_ident()?; - let mut generics = self.parse_generics()?; - generics.where_clause = self.parse_where_clause()?; - self.expect(&token::OpenDelim(token::Brace))?; - - let enum_definition = self.parse_enum_def(&generics).map_err(|e| { - self.recover_stmt(); - self.eat(&token::CloseDelim(token::Brace)); - e - })?; - Ok((id, ItemKind::Enum(enum_definition, generics), None)) - } - /// Parses a string as an ABI spec on an extern type or module. Consumes /// the `extern` keyword, if one is found. fn parse_opt_abi(&mut self) -> PResult<'a, Option> { @@ -4706,489 +3221,6 @@ impl<'a> Parser<'a> { } } - fn is_static_global(&mut self) -> bool { - if self.check_keyword(kw::Static) { - // Check if this could be a closure - !self.look_ahead(1, |token| { - if token.is_keyword(kw::Move) { - return true; - } - match token.kind { - token::BinOp(token::Or) | token::OrOr => true, - _ => false, - } - }) - } else { - false - } - } - - fn parse_item_( - &mut self, - attrs: Vec, - macros_allowed: bool, - attributes_allowed: bool, - ) -> PResult<'a, Option>> { - let mut unclosed_delims = vec![]; - let (ret, tokens) = self.collect_tokens(|this| { - let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed); - unclosed_delims.append(&mut this.unclosed_delims); - item - })?; - self.unclosed_delims.append(&mut unclosed_delims); - - // Once we've parsed an item and recorded the tokens we got while - // parsing we may want to store `tokens` into the item we're about to - // return. Note, though, that we specifically didn't capture tokens - // related to outer attributes. The `tokens` field here may later be - // used with procedural macros to convert this item back into a token - // stream, but during expansion we may be removing attributes as we go - // along. - // - // If we've got inner attributes then the `tokens` we've got above holds - // these inner attributes. If an inner attribute is expanded we won't - // actually remove it from the token stream, so we'll just keep yielding - // it (bad!). To work around this case for now we just avoid recording - // `tokens` if we detect any inner attributes. This should help keep - // expansion correct, but we should fix this bug one day! - Ok(ret.map(|item| { - item.map(|mut i| { - if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { - i.tokens = Some(tokens); - } - i - }) - })) - } - - /// Parses one of the items allowed by the flags. - fn parse_item_implementation( - &mut self, - attrs: Vec, - macros_allowed: bool, - attributes_allowed: bool, - ) -> PResult<'a, Option>> { - maybe_whole!(self, NtItem, |item| { - let mut item = item.into_inner(); - let mut attrs = attrs; - mem::swap(&mut item.attrs, &mut attrs); - item.attrs.extend(attrs); - Some(P(item)) - }); - - let lo = self.token.span; - - let visibility = self.parse_visibility(false)?; - - if self.eat_keyword(kw::Use) { - // USE ITEM - let item_ = ItemKind::Use(P(self.parse_use_tree()?)); - self.expect(&token::Semi)?; - - let span = lo.to(self.prev_span); - let item = - self.mk_item(span, Ident::invalid(), item_, visibility, attrs); - return Ok(Some(item)); - } - - if self.eat_keyword(kw::Extern) { - let extern_sp = self.prev_span; - if self.eat_keyword(kw::Crate) { - return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?)); - } - - let opt_abi = self.parse_opt_abi()?; - - if self.eat_keyword(kw::Fn) { - // EXTERN FUNCTION ITEM - let fn_span = self.prev_span; - let abi = opt_abi.unwrap_or(Abi::C); - let (ident, item_, extra_attrs) = - self.parse_item_fn(Unsafety::Normal, - respan(fn_span, IsAsync::NotAsync), - respan(fn_span, Constness::NotConst), - abi)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } else if self.check(&token::OpenDelim(token::Brace)) { - return Ok(Some( - self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs, extern_sp)?, - )); - } - - self.unexpected()?; - } - - if self.is_static_global() { - self.bump(); - // STATIC ITEM - let m = if self.eat_keyword(kw::Mut) { - Mutability::Mutable - } else { - Mutability::Immutable - }; - let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.eat_keyword(kw::Const) { - let const_span = self.prev_span; - if self.check_keyword(kw::Fn) - || (self.check_keyword(kw::Unsafe) - && self.is_keyword_ahead(1, &[kw::Fn])) { - // CONST FUNCTION ITEM - let unsafety = self.parse_unsafety(); - self.bump(); - let (ident, item_, extra_attrs) = - self.parse_item_fn(unsafety, - respan(const_span, IsAsync::NotAsync), - respan(const_span, Constness::Const), - Abi::Rust)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - - // CONST ITEM - if self.eat_keyword(kw::Mut) { - let prev_span = self.prev_span; - self.struct_span_err(prev_span, "const globals cannot be mutable") - .span_label(prev_span, "cannot be mutable") - .span_suggestion( - const_span, - "you might want to declare a static instead", - "static".to_owned(), - Applicability::MaybeIncorrect, - ) - .emit(); - } - let (ident, item_, extra_attrs) = self.parse_item_const(None)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - - // Parse `async unsafe? fn`. - if self.check_keyword(kw::Async) { - let async_span = self.token.span; - if self.is_keyword_ahead(1, &[kw::Fn]) - || self.is_keyword_ahead(2, &[kw::Fn]) - { - // ASYNC FUNCTION ITEM - self.bump(); // `async` - let unsafety = self.parse_unsafety(); // `unsafe`? - self.expect_keyword(kw::Fn)?; // `fn` - let fn_span = self.prev_span; - let (ident, item_, extra_attrs) = - self.parse_item_fn(unsafety, - respan(async_span, IsAsync::Async { - closure_id: ast::DUMMY_NODE_ID, - return_impl_trait_id: ast::DUMMY_NODE_ID, - }), - respan(fn_span, Constness::NotConst), - Abi::Rust)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - self.ban_async_in_2015(async_span); - return Ok(Some(item)); - } - } - if self.check_keyword(kw::Unsafe) && - self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) - { - // UNSAFE TRAIT ITEM - self.bump(); // `unsafe` - let is_auto = if self.eat_keyword(kw::Trait) { - IsAuto::No - } else { - self.expect_keyword(kw::Auto)?; - self.expect_keyword(kw::Trait)?; - IsAuto::Yes - }; - let (ident, item_, extra_attrs) = - self.parse_item_trait(is_auto, Unsafety::Unsafe)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.check_keyword(kw::Impl) || - self.check_keyword(kw::Unsafe) && - self.is_keyword_ahead(1, &[kw::Impl]) || - self.check_keyword(kw::Default) && - self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe]) { - // IMPL ITEM - let defaultness = self.parse_defaultness(); - let unsafety = self.parse_unsafety(); - self.expect_keyword(kw::Impl)?; - let (ident, item, extra_attrs) = self.parse_item_impl(unsafety, defaultness)?; - let span = lo.to(self.prev_span); - return Ok(Some(self.mk_item(span, ident, item, visibility, - maybe_append(attrs, extra_attrs)))); - } - if self.check_keyword(kw::Fn) { - // FUNCTION ITEM - self.bump(); - let fn_span = self.prev_span; - let (ident, item_, extra_attrs) = - self.parse_item_fn(Unsafety::Normal, - respan(fn_span, IsAsync::NotAsync), - respan(fn_span, Constness::NotConst), - Abi::Rust)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.check_keyword(kw::Unsafe) - && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) { - // UNSAFE FUNCTION ITEM - self.bump(); // `unsafe` - // `{` is also expected after `unsafe`, in case of error, include it in the diagnostic - self.check(&token::OpenDelim(token::Brace)); - let abi = if self.eat_keyword(kw::Extern) { - self.parse_opt_abi()?.unwrap_or(Abi::C) - } else { - Abi::Rust - }; - self.expect_keyword(kw::Fn)?; - let fn_span = self.prev_span; - let (ident, item_, extra_attrs) = - self.parse_item_fn(Unsafety::Unsafe, - respan(fn_span, IsAsync::NotAsync), - respan(fn_span, Constness::NotConst), - abi)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.eat_keyword(kw::Mod) { - // MODULE ITEM - let (ident, item_, extra_attrs) = - self.parse_item_mod(&attrs[..])?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if let Some(type_) = self.eat_type() { - let (ident, alias, generics) = type_?; - // TYPE ITEM - let item_ = match alias { - AliasKind::Weak(ty) => ItemKind::TyAlias(ty, generics), - AliasKind::OpaqueTy(bounds) => ItemKind::OpaqueTy(bounds, generics), - }; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - attrs); - return Ok(Some(item)); - } - if self.eat_keyword(kw::Enum) { - // ENUM ITEM - let (ident, item_, extra_attrs) = self.parse_item_enum()?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.check_keyword(kw::Trait) - || (self.check_keyword(kw::Auto) - && self.is_keyword_ahead(1, &[kw::Trait])) - { - let is_auto = if self.eat_keyword(kw::Trait) { - IsAuto::No - } else { - self.expect_keyword(kw::Auto)?; - self.expect_keyword(kw::Trait)?; - IsAuto::Yes - }; - // TRAIT ITEM - let (ident, item_, extra_attrs) = - self.parse_item_trait(is_auto, Unsafety::Normal)?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.eat_keyword(kw::Struct) { - // STRUCT ITEM - let (ident, item_, extra_attrs) = self.parse_item_struct()?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if self.is_union_item() { - // UNION ITEM - self.bump(); - let (ident, item_, extra_attrs) = self.parse_item_union()?; - let prev_span = self.prev_span; - let item = self.mk_item(lo.to(prev_span), - ident, - item_, - visibility, - maybe_append(attrs, extra_attrs)); - return Ok(Some(item)); - } - if let Some(macro_def) = self.eat_macro_def(&attrs, &visibility, lo)? { - return Ok(Some(macro_def)); - } - - // Verify whether we have encountered a struct or method definition where the user forgot to - // add the `struct` or `fn` keyword after writing `pub`: `pub S {}` - if visibility.node.is_pub() && - self.check_ident() && - self.look_ahead(1, |t| *t != token::Not) - { - // Space between `pub` keyword and the identifier - // - // pub S {} - // ^^^ `sp` points here - let sp = self.prev_span.between(self.token.span); - let full_sp = self.prev_span.to(self.token.span); - let ident_sp = self.token.span; - if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) { - // possible public struct definition where `struct` was forgotten - let ident = self.parse_ident().unwrap(); - let msg = format!("add `struct` here to parse `{}` as a public struct", - ident); - let mut err = self.diagnostic() - .struct_span_err(sp, "missing `struct` for struct definition"); - err.span_suggestion_short( - sp, &msg, " struct ".into(), Applicability::MaybeIncorrect // speculative - ); - return Err(err); - } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) { - let ident = self.parse_ident().unwrap(); - self.bump(); // `(` - let kw_name = if let Ok(Some(_)) = self.parse_self_arg_with_attrs() - .map_err(|mut e| e.cancel()) - { - "method" - } else { - "function" - }; - self.consume_block(token::Paren); - let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) { - self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]); - self.bump(); // `{` - ("fn", kw_name, false) - } else if self.check(&token::OpenDelim(token::Brace)) { - self.bump(); // `{` - ("fn", kw_name, false) - } else if self.check(&token::Colon) { - let kw = "struct"; - (kw, kw, false) - } else { - ("fn` or `struct", "function or struct", true) - }; - - let msg = format!("missing `{}` for {} definition", kw, kw_name); - let mut err = self.diagnostic().struct_span_err(sp, &msg); - if !ambiguous { - self.consume_block(token::Brace); - let suggestion = format!("add `{}` here to parse `{}` as a public {}", - kw, - ident, - kw_name); - err.span_suggestion_short( - sp, &suggestion, format!(" {} ", kw), Applicability::MachineApplicable - ); - } else { - if let Ok(snippet) = self.span_to_snippet(ident_sp) { - err.span_suggestion( - full_sp, - "if you meant to call a macro, try", - format!("{}!", snippet), - // this is the `ambiguous` conditional branch - Applicability::MaybeIncorrect - ); - } else { - err.help("if you meant to call a macro, remove the `pub` \ - and add a trailing `!` after the identifier"); - } - } - return Err(err); - } else if self.look_ahead(1, |t| *t == token::Lt) { - let ident = self.parse_ident().unwrap(); - self.eat_to_tokens(&[&token::Gt]); - self.bump(); // `>` - let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) { - if let Ok(Some(_)) = self.parse_self_arg_with_attrs() - .map_err(|mut e| e.cancel()) - { - ("fn", "method", false) - } else { - ("fn", "function", false) - } - } else if self.check(&token::OpenDelim(token::Brace)) { - ("struct", "struct", false) - } else { - ("fn` or `struct", "function or struct", true) - }; - let msg = format!("missing `{}` for {} definition", kw, kw_name); - let mut err = self.diagnostic().struct_span_err(sp, &msg); - if !ambiguous { - err.span_suggestion_short( - sp, - &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name), - format!(" {} ", kw), - Applicability::MachineApplicable, - ); - } - return Err(err); - } - } - self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility) - } - /// We are parsing `async fn`. If we are on Rust 2015, emit an error. fn ban_async_in_2015(&self, async_span: Span) { if async_span.rust_2015() { @@ -5202,155 +3234,6 @@ impl<'a> Parser<'a> { } } - /// Parses a foreign item. - crate fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, ForeignItem> { - maybe_whole!(self, NtForeignItem, |ni| ni); - - let attrs = self.parse_outer_attributes()?; - let lo = self.token.span; - let visibility = self.parse_visibility(false)?; - - // FOREIGN STATIC ITEM - // Treat `const` as `static` for error recovery, but don't add it to expected tokens. - if self.check_keyword(kw::Static) || self.token.is_keyword(kw::Const) { - if self.token.is_keyword(kw::Const) { - self.diagnostic() - .struct_span_err(self.token.span, "extern items cannot be `const`") - .span_suggestion( - self.token.span, - "try using a static value", - "static".to_owned(), - Applicability::MachineApplicable - ).emit(); - } - self.bump(); // `static` or `const` - return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?); - } - // FOREIGN FUNCTION ITEM - if self.check_keyword(kw::Fn) { - return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?); - } - // FOREIGN TYPE ITEM - if self.check_keyword(kw::Type) { - return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?); - } - - match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? { - Some(mac) => { - Ok( - ForeignItem { - ident: Ident::invalid(), - span: lo.to(self.prev_span), - id: ast::DUMMY_NODE_ID, - attrs, - vis: visibility, - node: ForeignItemKind::Macro(mac), - } - ) - } - None => { - if !attrs.is_empty() { - self.expected_item_err(&attrs)?; - } - - self.unexpected() - } - } - } - - /// This is the fall-through for parsing items. - fn parse_macro_use_or_failure( - &mut self, - attrs: Vec , - macros_allowed: bool, - attributes_allowed: bool, - lo: Span, - visibility: Visibility - ) -> PResult<'a, Option>> { - if macros_allowed && self.token.is_path_start() && - !(self.is_async_fn() && self.token.span.rust_2015()) { - // MACRO INVOCATION ITEM - - let prev_span = self.prev_span; - self.complain_if_pub_macro(&visibility.node, prev_span); - - let mac_lo = self.token.span; - - // item macro. - let path = self.parse_path(PathStyle::Mod)?; - self.expect(&token::Not)?; - let (delim, tts) = self.expect_delimited_token_tree()?; - if delim != MacDelimiter::Brace && !self.eat(&token::Semi) { - self.report_invalid_macro_expansion_item(); - } - - let hi = self.prev_span; - let mac = respan(mac_lo.to(hi), Mac_ { - path, - tts, - delim, - prior_type_ascription: self.last_type_ascription, - }); - let item = - self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs); - return Ok(Some(item)); - } - - // FAILURE TO PARSE ITEM - match visibility.node { - VisibilityKind::Inherited => {} - _ => { - return Err(self.span_fatal(self.prev_span, "unmatched visibility `pub`")); - } - } - - if !attributes_allowed && !attrs.is_empty() { - self.expected_item_err(&attrs)?; - } - Ok(None) - } - - /// Parses a macro invocation inside a `trait`, `impl` or `extern` block. - fn parse_assoc_macro_invoc(&mut self, item_kind: &str, vis: Option<&Visibility>, - at_end: &mut bool) -> PResult<'a, Option> - { - if self.token.is_path_start() && - !(self.is_async_fn() && self.token.span.rust_2015()) { - let prev_span = self.prev_span; - let lo = self.token.span; - let path = self.parse_path(PathStyle::Mod)?; - - if path.segments.len() == 1 { - if !self.eat(&token::Not) { - return Err(self.missing_assoc_item_kind_err(item_kind, prev_span)); - } - } else { - self.expect(&token::Not)?; - } - - if let Some(vis) = vis { - self.complain_if_pub_macro(&vis.node, prev_span); - } - - *at_end = true; - - // eat a matched-delimiter token tree: - let (delim, tts) = self.expect_delimited_token_tree()?; - if delim != MacDelimiter::Brace { - self.expect(&token::Semi)?; - } - - Ok(Some(respan(lo.to(self.prev_span), Mac_ { - path, - tts, - delim, - prior_type_ascription: self.last_type_ascription, - }))) - } else { - Ok(None) - } - } - fn collect_tokens(&mut self, f: F) -> PResult<'a, (R, TokenStream)> where F: FnOnce(&mut Self) -> PResult<'a, R> { @@ -5420,11 +3303,6 @@ impl<'a> Parser<'a> { Ok((ret?, TokenStream::new(collected_tokens))) } - pub fn parse_item(&mut self) -> PResult<'a, Option>> { - let attrs = self.parse_outer_attributes()?; - self.parse_item_(attrs, true, false) - } - /// `::{` or `::*` fn is_import_coupler(&mut self) -> bool { self.check(&token::ModSep) && @@ -5432,82 +3310,6 @@ impl<'a> Parser<'a> { *t == token::BinOp(token::Star)) } - /// Parses a `UseTree`. - /// - /// ``` - /// USE_TREE = [`::`] `*` | - /// [`::`] `{` USE_TREE_LIST `}` | - /// PATH `::` `*` | - /// PATH `::` `{` USE_TREE_LIST `}` | - /// PATH [`as` IDENT] - /// ``` - fn parse_use_tree(&mut self) -> PResult<'a, UseTree> { - let lo = self.token.span; - - let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() }; - let kind = if self.check(&token::OpenDelim(token::Brace)) || - self.check(&token::BinOp(token::Star)) || - self.is_import_coupler() { - // `use *;` or `use ::*;` or `use {...};` or `use ::{...};` - let mod_sep_ctxt = self.token.span.ctxt(); - if self.eat(&token::ModSep) { - prefix.segments.push( - PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)) - ); - } - - if self.eat(&token::BinOp(token::Star)) { - UseTreeKind::Glob - } else { - UseTreeKind::Nested(self.parse_use_tree_list()?) - } - } else { - // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;` - prefix = self.parse_path(PathStyle::Mod)?; - - if self.eat(&token::ModSep) { - if self.eat(&token::BinOp(token::Star)) { - UseTreeKind::Glob - } else { - UseTreeKind::Nested(self.parse_use_tree_list()?) - } - } else { - UseTreeKind::Simple(self.parse_rename()?, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID) - } - }; - - Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) }) - } - - /// Parses a `UseTreeKind::Nested(list)`. - /// - /// ``` - /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`] - /// ``` - fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> { - self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, ast::DUMMY_NODE_ID))) - .map(|(r, _)| r) - } - - fn parse_rename(&mut self) -> PResult<'a, Option> { - if self.eat_keyword(kw::As) { - self.parse_ident_or_underscore().map(Some) - } else { - Ok(None) - } - } - - /// Parses a source module as a crate. This is the main entry point for the parser. - pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> { - let lo = self.token.span; - let krate = Ok(ast::Crate { - attrs: self.parse_inner_attributes()?, - module: self.parse_mod_items(&token::Eof, lo)?, - span: lo.to(self.token.span), - }); - krate - } - pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option)> { let ret = match self.token.kind { token::Literal(token::Lit { kind: token::Str, symbol, suffix }) => diff --git a/src/libsyntax/parse/parser/item.rs b/src/libsyntax/parse/parser/item.rs new file mode 100644 index 0000000000000..5e264a0ca7753 --- /dev/null +++ b/src/libsyntax/parse/parser/item.rs @@ -0,0 +1,1899 @@ +use super::{Parser, PResult, PathStyle, SemiColonMode, BlockMode}; + +use crate::maybe_whole; +use crate::ptr::P; +use crate::ast::{self, Ident, Attribute, AttrStyle}; +use crate::ast::{Item, ItemKind, ImplItem, TraitItem, TraitItemKind}; +use crate::ast::{UseTree, UseTreeKind, PathSegment}; +use crate::ast::{IsAuto, Constness, IsAsync, Unsafety, Defaultness}; +use crate::ast::{Visibility, VisibilityKind, Mutability, FnDecl, FnHeader}; +use crate::ast::{ForeignItem, ForeignItemKind}; +use crate::ast::{Ty, TyKind, GenericBounds, TraitRef}; +use crate::ast::{EnumDef, VariantData, StructField, AnonConst}; +use crate::ast::{Mac, Mac_, MacDelimiter}; +use crate::ext::base::DummyResult; +use crate::parse::token; +use crate::parse::parser::maybe_append; +use crate::parse::diagnostics::{Error}; +use crate::tokenstream::{TokenTree, TokenStream}; +use crate::source_map::{respan, Span, Spanned}; +use crate::symbol::{kw, sym}; + +use std::mem; +use log::debug; +use rustc_target::spec::abi::{Abi}; +use errors::{Applicability, DiagnosticBuilder, DiagnosticId}; + +/// Whether the type alias or associated type is a concrete type or an opaque type +#[derive(Debug)] +pub enum AliasKind { + /// Just a new name for the same type + Weak(P), + /// Only trait impls of the type will be usable, not the actual type itself + OpaqueTy(GenericBounds), +} + +pub(super) type ItemInfo = (Ident, ItemKind, Option>); + +impl<'a> Parser<'a> { + pub fn parse_item(&mut self) -> PResult<'a, Option>> { + let attrs = self.parse_outer_attributes()?; + self.parse_item_(attrs, true, false) + } + + pub(super) fn parse_item_( + &mut self, + attrs: Vec, + macros_allowed: bool, + attributes_allowed: bool, + ) -> PResult<'a, Option>> { + let mut unclosed_delims = vec![]; + let (ret, tokens) = self.collect_tokens(|this| { + let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed); + unclosed_delims.append(&mut this.unclosed_delims); + item + })?; + self.unclosed_delims.append(&mut unclosed_delims); + + // Once we've parsed an item and recorded the tokens we got while + // parsing we may want to store `tokens` into the item we're about to + // return. Note, though, that we specifically didn't capture tokens + // related to outer attributes. The `tokens` field here may later be + // used with procedural macros to convert this item back into a token + // stream, but during expansion we may be removing attributes as we go + // along. + // + // If we've got inner attributes then the `tokens` we've got above holds + // these inner attributes. If an inner attribute is expanded we won't + // actually remove it from the token stream, so we'll just keep yielding + // it (bad!). To work around this case for now we just avoid recording + // `tokens` if we detect any inner attributes. This should help keep + // expansion correct, but we should fix this bug one day! + Ok(ret.map(|item| { + item.map(|mut i| { + if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { + i.tokens = Some(tokens); + } + i + }) + })) + } + + /// Parses one of the items allowed by the flags. + fn parse_item_implementation( + &mut self, + attrs: Vec, + macros_allowed: bool, + attributes_allowed: bool, + ) -> PResult<'a, Option>> { + maybe_whole!(self, NtItem, |item| { + let mut item = item.into_inner(); + let mut attrs = attrs; + mem::swap(&mut item.attrs, &mut attrs); + item.attrs.extend(attrs); + Some(P(item)) + }); + + let lo = self.token.span; + + let visibility = self.parse_visibility(false)?; + + if self.eat_keyword(kw::Use) { + // USE ITEM + let item_ = ItemKind::Use(P(self.parse_use_tree()?)); + self.expect(&token::Semi)?; + + let span = lo.to(self.prev_span); + let item = + self.mk_item(span, Ident::invalid(), item_, visibility, attrs); + return Ok(Some(item)); + } + + if self.eat_keyword(kw::Extern) { + let extern_sp = self.prev_span; + if self.eat_keyword(kw::Crate) { + return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?)); + } + + let opt_abi = self.parse_opt_abi()?; + + if self.eat_keyword(kw::Fn) { + // EXTERN FUNCTION ITEM + let fn_span = self.prev_span; + let abi = opt_abi.unwrap_or(Abi::C); + let (ident, item_, extra_attrs) = + self.parse_item_fn(Unsafety::Normal, + respan(fn_span, IsAsync::NotAsync), + respan(fn_span, Constness::NotConst), + abi)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } else if self.check(&token::OpenDelim(token::Brace)) { + return Ok(Some( + self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs, extern_sp)?, + )); + } + + self.unexpected()?; + } + + if self.is_static_global() { + self.bump(); + // STATIC ITEM + let m = self.parse_mutability(); + let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.eat_keyword(kw::Const) { + let const_span = self.prev_span; + if self.check_keyword(kw::Fn) + || (self.check_keyword(kw::Unsafe) + && self.is_keyword_ahead(1, &[kw::Fn])) { + // CONST FUNCTION ITEM + let unsafety = self.parse_unsafety(); + self.bump(); + let (ident, item_, extra_attrs) = + self.parse_item_fn(unsafety, + respan(const_span, IsAsync::NotAsync), + respan(const_span, Constness::Const), + Abi::Rust)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + + // CONST ITEM + if self.eat_keyword(kw::Mut) { + let prev_span = self.prev_span; + self.struct_span_err(prev_span, "const globals cannot be mutable") + .span_label(prev_span, "cannot be mutable") + .span_suggestion( + const_span, + "you might want to declare a static instead", + "static".to_owned(), + Applicability::MaybeIncorrect, + ) + .emit(); + } + let (ident, item_, extra_attrs) = self.parse_item_const(None)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + + // Parse `async unsafe? fn`. + if self.check_keyword(kw::Async) { + let async_span = self.token.span; + if self.is_keyword_ahead(1, &[kw::Fn]) + || self.is_keyword_ahead(2, &[kw::Fn]) + { + // ASYNC FUNCTION ITEM + self.bump(); // `async` + let unsafety = self.parse_unsafety(); // `unsafe`? + self.expect_keyword(kw::Fn)?; // `fn` + let fn_span = self.prev_span; + let (ident, item_, extra_attrs) = + self.parse_item_fn(unsafety, + respan(async_span, IsAsync::Async { + closure_id: ast::DUMMY_NODE_ID, + return_impl_trait_id: ast::DUMMY_NODE_ID, + }), + respan(fn_span, Constness::NotConst), + Abi::Rust)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + self.ban_async_in_2015(async_span); + return Ok(Some(item)); + } + } + if self.check_keyword(kw::Unsafe) && + self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) + { + // UNSAFE TRAIT ITEM + self.bump(); // `unsafe` + let is_auto = if self.eat_keyword(kw::Trait) { + IsAuto::No + } else { + self.expect_keyword(kw::Auto)?; + self.expect_keyword(kw::Trait)?; + IsAuto::Yes + }; + let (ident, item_, extra_attrs) = + self.parse_item_trait(is_auto, Unsafety::Unsafe)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.check_keyword(kw::Impl) || + self.check_keyword(kw::Unsafe) && + self.is_keyword_ahead(1, &[kw::Impl]) || + self.check_keyword(kw::Default) && + self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe]) { + // IMPL ITEM + let defaultness = self.parse_defaultness(); + let unsafety = self.parse_unsafety(); + self.expect_keyword(kw::Impl)?; + let (ident, item, extra_attrs) = self.parse_item_impl(unsafety, defaultness)?; + let span = lo.to(self.prev_span); + return Ok(Some(self.mk_item(span, ident, item, visibility, + maybe_append(attrs, extra_attrs)))); + } + if self.check_keyword(kw::Fn) { + // FUNCTION ITEM + self.bump(); + let fn_span = self.prev_span; + let (ident, item_, extra_attrs) = + self.parse_item_fn(Unsafety::Normal, + respan(fn_span, IsAsync::NotAsync), + respan(fn_span, Constness::NotConst), + Abi::Rust)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.check_keyword(kw::Unsafe) + && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) { + // UNSAFE FUNCTION ITEM + self.bump(); // `unsafe` + // `{` is also expected after `unsafe`, in case of error, include it in the diagnostic + self.check(&token::OpenDelim(token::Brace)); + let abi = if self.eat_keyword(kw::Extern) { + self.parse_opt_abi()?.unwrap_or(Abi::C) + } else { + Abi::Rust + }; + self.expect_keyword(kw::Fn)?; + let fn_span = self.prev_span; + let (ident, item_, extra_attrs) = + self.parse_item_fn(Unsafety::Unsafe, + respan(fn_span, IsAsync::NotAsync), + respan(fn_span, Constness::NotConst), + abi)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.eat_keyword(kw::Mod) { + // MODULE ITEM + let (ident, item_, extra_attrs) = + self.parse_item_mod(&attrs[..])?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if let Some(type_) = self.eat_type() { + let (ident, alias, generics) = type_?; + // TYPE ITEM + let item_ = match alias { + AliasKind::Weak(ty) => ItemKind::TyAlias(ty, generics), + AliasKind::OpaqueTy(bounds) => ItemKind::OpaqueTy(bounds, generics), + }; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + attrs); + return Ok(Some(item)); + } + if self.eat_keyword(kw::Enum) { + // ENUM ITEM + let (ident, item_, extra_attrs) = self.parse_item_enum()?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.check_keyword(kw::Trait) + || (self.check_keyword(kw::Auto) + && self.is_keyword_ahead(1, &[kw::Trait])) + { + let is_auto = if self.eat_keyword(kw::Trait) { + IsAuto::No + } else { + self.expect_keyword(kw::Auto)?; + self.expect_keyword(kw::Trait)?; + IsAuto::Yes + }; + // TRAIT ITEM + let (ident, item_, extra_attrs) = + self.parse_item_trait(is_auto, Unsafety::Normal)?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.eat_keyword(kw::Struct) { + // STRUCT ITEM + let (ident, item_, extra_attrs) = self.parse_item_struct()?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if self.is_union_item() { + // UNION ITEM + self.bump(); + let (ident, item_, extra_attrs) = self.parse_item_union()?; + let prev_span = self.prev_span; + let item = self.mk_item(lo.to(prev_span), + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + if let Some(macro_def) = self.eat_macro_def(&attrs, &visibility, lo)? { + return Ok(Some(macro_def)); + } + + // Verify whether we have encountered a struct or method definition where the user forgot to + // add the `struct` or `fn` keyword after writing `pub`: `pub S {}` + if visibility.node.is_pub() && + self.check_ident() && + self.look_ahead(1, |t| *t != token::Not) + { + // Space between `pub` keyword and the identifier + // + // pub S {} + // ^^^ `sp` points here + let sp = self.prev_span.between(self.token.span); + let full_sp = self.prev_span.to(self.token.span); + let ident_sp = self.token.span; + if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) { + // possible public struct definition where `struct` was forgotten + let ident = self.parse_ident().unwrap(); + let msg = format!("add `struct` here to parse `{}` as a public struct", + ident); + let mut err = self.diagnostic() + .struct_span_err(sp, "missing `struct` for struct definition"); + err.span_suggestion_short( + sp, &msg, " struct ".into(), Applicability::MaybeIncorrect // speculative + ); + return Err(err); + } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) { + let ident = self.parse_ident().unwrap(); + self.bump(); // `(` + let kw_name = if let Ok(Some(_)) = self.parse_self_arg_with_attrs() + .map_err(|mut e| e.cancel()) + { + "method" + } else { + "function" + }; + self.consume_block(token::Paren); + let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) { + self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]); + self.bump(); // `{` + ("fn", kw_name, false) + } else if self.check(&token::OpenDelim(token::Brace)) { + self.bump(); // `{` + ("fn", kw_name, false) + } else if self.check(&token::Colon) { + let kw = "struct"; + (kw, kw, false) + } else { + ("fn` or `struct", "function or struct", true) + }; + + let msg = format!("missing `{}` for {} definition", kw, kw_name); + let mut err = self.diagnostic().struct_span_err(sp, &msg); + if !ambiguous { + self.consume_block(token::Brace); + let suggestion = format!("add `{}` here to parse `{}` as a public {}", + kw, + ident, + kw_name); + err.span_suggestion_short( + sp, &suggestion, format!(" {} ", kw), Applicability::MachineApplicable + ); + } else { + if let Ok(snippet) = self.span_to_snippet(ident_sp) { + err.span_suggestion( + full_sp, + "if you meant to call a macro, try", + format!("{}!", snippet), + // this is the `ambiguous` conditional branch + Applicability::MaybeIncorrect + ); + } else { + err.help("if you meant to call a macro, remove the `pub` \ + and add a trailing `!` after the identifier"); + } + } + return Err(err); + } else if self.look_ahead(1, |t| *t == token::Lt) { + let ident = self.parse_ident().unwrap(); + self.eat_to_tokens(&[&token::Gt]); + self.bump(); // `>` + let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) { + if let Ok(Some(_)) = self.parse_self_arg_with_attrs() + .map_err(|mut e| e.cancel()) + { + ("fn", "method", false) + } else { + ("fn", "function", false) + } + } else if self.check(&token::OpenDelim(token::Brace)) { + ("struct", "struct", false) + } else { + ("fn` or `struct", "function or struct", true) + }; + let msg = format!("missing `{}` for {} definition", kw, kw_name); + let mut err = self.diagnostic().struct_span_err(sp, &msg); + if !ambiguous { + err.span_suggestion_short( + sp, + &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name), + format!(" {} ", kw), + Applicability::MachineApplicable, + ); + } + return Err(err); + } + } + self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility) + } + + /// This is the fall-through for parsing items. + fn parse_macro_use_or_failure( + &mut self, + attrs: Vec , + macros_allowed: bool, + attributes_allowed: bool, + lo: Span, + visibility: Visibility + ) -> PResult<'a, Option>> { + if macros_allowed && self.token.is_path_start() && + !(self.is_async_fn() && self.token.span.rust_2015()) { + // MACRO INVOCATION ITEM + + let prev_span = self.prev_span; + self.complain_if_pub_macro(&visibility.node, prev_span); + + let mac_lo = self.token.span; + + // item macro. + let path = self.parse_path(PathStyle::Mod)?; + self.expect(&token::Not)?; + let (delim, tts) = self.expect_delimited_token_tree()?; + if delim != MacDelimiter::Brace && !self.eat(&token::Semi) { + self.report_invalid_macro_expansion_item(); + } + + let hi = self.prev_span; + let mac = respan(mac_lo.to(hi), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }); + let item = + self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs); + return Ok(Some(item)); + } + + // FAILURE TO PARSE ITEM + match visibility.node { + VisibilityKind::Inherited => {} + _ => { + return Err(self.span_fatal(self.prev_span, "unmatched visibility `pub`")); + } + } + + if !attributes_allowed && !attrs.is_empty() { + self.expected_item_err(&attrs)?; + } + Ok(None) + } + + /// Emits an expected-item-after-attributes error. + fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> { + let message = match attrs.last() { + Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment", + _ => "expected item after attributes", + }; + + let mut err = self.diagnostic().struct_span_err(self.prev_span, message); + if attrs.last().unwrap().is_sugared_doc { + err.span_label(self.prev_span, "this doc comment doesn't document anything"); + } + Err(err) + } + + /// Parses a macro invocation inside a `trait`, `impl` or `extern` block. + fn parse_assoc_macro_invoc(&mut self, item_kind: &str, vis: Option<&Visibility>, + at_end: &mut bool) -> PResult<'a, Option> + { + if self.token.is_path_start() && + !(self.is_async_fn() && self.token.span.rust_2015()) { + let prev_span = self.prev_span; + let lo = self.token.span; + let path = self.parse_path(PathStyle::Mod)?; + + if path.segments.len() == 1 { + if !self.eat(&token::Not) { + return Err(self.missing_assoc_item_kind_err(item_kind, prev_span)); + } + } else { + self.expect(&token::Not)?; + } + + if let Some(vis) = vis { + self.complain_if_pub_macro(&vis.node, prev_span); + } + + *at_end = true; + + // eat a matched-delimiter token tree: + let (delim, tts) = self.expect_delimited_token_tree()?; + if delim != MacDelimiter::Brace { + self.expect(&token::Semi)?; + } + + Ok(Some(respan(lo.to(self.prev_span), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }))) + } else { + Ok(None) + } + } + + fn missing_assoc_item_kind_err(&self, item_type: &str, prev_span: Span) + -> DiagnosticBuilder<'a> + { + let expected_kinds = if item_type == "extern" { + "missing `fn`, `type`, or `static`" + } else { + "missing `fn`, `type`, or `const`" + }; + + // Given this code `path(`, it seems like this is not + // setting the visibility of a macro invocation, but rather + // a mistyped method declaration. + // Create a diagnostic pointing out that `fn` is missing. + // + // x | pub path(&self) { + // | ^ missing `fn`, `type`, or `const` + // pub path( + // ^^ `sp` below will point to this + let sp = prev_span.between(self.prev_span); + let mut err = self.diagnostic().struct_span_err( + sp, + &format!("{} for {}-item declaration", + expected_kinds, item_type)); + err.span_label(sp, expected_kinds); + err + } + + /// Parses an implementation item, `impl` keyword is already parsed. + /// + /// impl<'a, T> TYPE { /* impl items */ } + /// impl<'a, T> TRAIT for TYPE { /* impl items */ } + /// impl<'a, T> !TRAIT for TYPE { /* impl items */ } + /// + /// We actually parse slightly more relaxed grammar for better error reporting and recovery. + /// `impl` GENERICS `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}` + /// `impl` GENERICS `!`? TYPE (`where` PREDICATES)? `{` BODY `}` + fn parse_item_impl(&mut self, unsafety: Unsafety, defaultness: Defaultness) + -> PResult<'a, ItemInfo> { + // First, parse generic parameters if necessary. + let mut generics = if self.choose_generics_over_qpath() { + self.parse_generics()? + } else { + ast::Generics::default() + }; + + // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type. + let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) { + self.bump(); // `!` + ast::ImplPolarity::Negative + } else { + ast::ImplPolarity::Positive + }; + + // Parse both types and traits as a type, then reinterpret if necessary. + let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span)); + let ty_first = if self.token.is_keyword(kw::For) && + self.look_ahead(1, |t| t != &token::Lt) { + let span = self.prev_span.between(self.token.span); + self.struct_span_err(span, "missing trait in a trait impl").emit(); + P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID }) + } else { + self.parse_ty()? + }; + + // If `for` is missing we try to recover. + let has_for = self.eat_keyword(kw::For); + let missing_for_span = self.prev_span.between(self.token.span); + + let ty_second = if self.token == token::DotDot { + // We need to report this error after `cfg` expansion for compatibility reasons + self.bump(); // `..`, do not add it to expected tokens + Some(DummyResult::raw_ty(self.prev_span, true)) + } else if has_for || self.token.can_begin_type() { + Some(self.parse_ty()?) + } else { + None + }; + + generics.where_clause = self.parse_where_clause()?; + + let (impl_items, attrs) = self.parse_impl_body()?; + + let item_kind = match ty_second { + Some(ty_second) => { + // impl Trait for Type + if !has_for { + self.struct_span_err(missing_for_span, "missing `for` in a trait impl") + .span_suggestion_short( + missing_for_span, + "add `for` here", + " for ".to_string(), + Applicability::MachineApplicable, + ).emit(); + } + + let ty_first = ty_first.into_inner(); + let path = match ty_first.node { + // This notably includes paths passed through `ty` macro fragments (#46438). + TyKind::Path(None, path) => path, + _ => { + self.span_err(ty_first.span, "expected a trait, found type"); + err_path(ty_first.span) + } + }; + let trait_ref = TraitRef { path, ref_id: ty_first.id }; + + ItemKind::Impl(unsafety, polarity, defaultness, + generics, Some(trait_ref), ty_second, impl_items) + } + None => { + // impl Type + ItemKind::Impl(unsafety, polarity, defaultness, + generics, None, ty_first, impl_items) + } + }; + + Ok((Ident::invalid(), item_kind, Some(attrs))) + } + + fn parse_impl_body(&mut self) -> PResult<'a, (Vec, Vec)> { + self.expect(&token::OpenDelim(token::Brace))?; + let attrs = self.parse_inner_attributes()?; + + let mut impl_items = Vec::new(); + while !self.eat(&token::CloseDelim(token::Brace)) { + let mut at_end = false; + match self.parse_impl_item(&mut at_end) { + Ok(impl_item) => impl_items.push(impl_item), + Err(mut err) => { + err.emit(); + if !at_end { + self.recover_stmt_(SemiColonMode::Break, BlockMode::Break); + } + } + } + } + Ok((impl_items, attrs)) + } + + /// Parses an impl item. + pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, ImplItem> { + maybe_whole!(self, NtImplItem, |x| x); + let attrs = self.parse_outer_attributes()?; + let mut unclosed_delims = vec![]; + let (mut item, tokens) = self.collect_tokens(|this| { + let item = this.parse_impl_item_(at_end, attrs); + unclosed_delims.append(&mut this.unclosed_delims); + item + })?; + self.unclosed_delims.append(&mut unclosed_delims); + + // See `parse_item` for why this clause is here. + if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { + item.tokens = Some(tokens); + } + Ok(item) + } + + fn parse_impl_item_(&mut self, + at_end: &mut bool, + mut attrs: Vec) -> PResult<'a, ImplItem> { + let lo = self.token.span; + let vis = self.parse_visibility(false)?; + let defaultness = self.parse_defaultness(); + let (name, node, generics) = if let Some(type_) = self.eat_type() { + let (name, alias, generics) = type_?; + let kind = match alias { + AliasKind::Weak(typ) => ast::ImplItemKind::TyAlias(typ), + AliasKind::OpaqueTy(bounds) => ast::ImplItemKind::OpaqueTy(bounds), + }; + (name, kind, generics) + } else if self.is_const_item() { + // This parses the grammar: + // ImplItemConst = "const" Ident ":" Ty "=" Expr ";" + self.expect_keyword(kw::Const)?; + let name = self.parse_ident()?; + self.expect(&token::Colon)?; + let typ = self.parse_ty()?; + self.expect(&token::Eq)?; + let expr = self.parse_expr()?; + self.expect(&token::Semi)?; + (name, ast::ImplItemKind::Const(typ, expr), ast::Generics::default()) + } else { + let (name, inner_attrs, generics, node) = self.parse_impl_method(&vis, at_end)?; + attrs.extend(inner_attrs); + (name, node, generics) + }; + + Ok(ImplItem { + id: ast::DUMMY_NODE_ID, + span: lo.to(self.prev_span), + ident: name, + vis, + defaultness, + attrs, + generics, + node, + tokens: None, + }) + } + + /// Parses defaultness (i.e., `default` or nothing). + fn parse_defaultness(&mut self) -> Defaultness { + // `pub` is included for better error messages + if self.check_keyword(kw::Default) && + self.is_keyword_ahead(1, &[ + kw::Impl, + kw::Const, + kw::Fn, + kw::Unsafe, + kw::Extern, + kw::Type, + kw::Pub, + ]) + { + self.bump(); // `default` + Defaultness::Default + } else { + Defaultness::Final + } + } + + /// Returns `true` if we are looking at `const ID` + /// (returns `false` for things like `const fn`, etc.). + fn is_const_item(&self) -> bool { + self.token.is_keyword(kw::Const) && + !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe]) + } + + /// Parse a method or a macro invocation in a trait impl. + fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool) + -> PResult<'a, (Ident, Vec, ast::Generics, + ast::ImplItemKind)> { + // code copied from parse_macro_use_or_failure... abstraction! + if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(vis), at_end)? { + // method macro + Ok((Ident::invalid(), vec![], ast::Generics::default(), + ast::ImplItemKind::Macro(mac))) + } else { + let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; + let ident = self.parse_ident()?; + let mut generics = self.parse_generics()?; + let decl = self.parse_fn_decl_with_self(|p| { + p.parse_arg_general(true, false, |_| true) + })?; + generics.where_clause = self.parse_where_clause()?; + *at_end = true; + let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; + let header = ast::FnHeader { abi, unsafety, constness, asyncness }; + Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method( + ast::MethodSig { header, decl }, + body + ))) + } + } + + /// Parses all the "front matter" for a `fn` declaration, up to + /// and including the `fn` keyword: + /// + /// - `const fn` + /// - `unsafe fn` + /// - `const unsafe fn` + /// - `extern fn` + /// - etc. + fn parse_fn_front_matter(&mut self) + -> PResult<'a, ( + Spanned, + Unsafety, + Spanned, + Abi + )> + { + let is_const_fn = self.eat_keyword(kw::Const); + let const_span = self.prev_span; + let asyncness = self.parse_asyncness(); + if let IsAsync::Async { .. } = asyncness { + self.ban_async_in_2015(self.prev_span); + } + let asyncness = respan(self.prev_span, asyncness); + let unsafety = self.parse_unsafety(); + let (constness, unsafety, abi) = if is_const_fn { + (respan(const_span, Constness::Const), unsafety, Abi::Rust) + } else { + let abi = if self.eat_keyword(kw::Extern) { + self.parse_opt_abi()?.unwrap_or(Abi::C) + } else { + Abi::Rust + }; + (respan(self.prev_span, Constness::NotConst), unsafety, abi) + }; + if !self.eat_keyword(kw::Fn) { + // It is possible for `expect_one_of` to recover given the contents of + // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't + // account for this. + if !self.expect_one_of(&[], &[])? { unreachable!() } + } + Ok((constness, unsafety, asyncness, abi)) + } + + /// Parses `trait Foo { ... }` or `trait Foo = Bar;`. + fn parse_item_trait(&mut self, is_auto: IsAuto, unsafety: Unsafety) -> PResult<'a, ItemInfo> { + let ident = self.parse_ident()?; + let mut tps = self.parse_generics()?; + + // Parse optional colon and supertrait bounds. + let bounds = if self.eat(&token::Colon) { + self.parse_generic_bounds(Some(self.prev_span))? + } else { + Vec::new() + }; + + if self.eat(&token::Eq) { + // it's a trait alias + let bounds = self.parse_generic_bounds(None)?; + tps.where_clause = self.parse_where_clause()?; + self.expect(&token::Semi)?; + if is_auto == IsAuto::Yes { + let msg = "trait aliases cannot be `auto`"; + self.struct_span_err(self.prev_span, msg) + .span_label(self.prev_span, msg) + .emit(); + } + if unsafety != Unsafety::Normal { + let msg = "trait aliases cannot be `unsafe`"; + self.struct_span_err(self.prev_span, msg) + .span_label(self.prev_span, msg) + .emit(); + } + Ok((ident, ItemKind::TraitAlias(tps, bounds), None)) + } else { + // it's a normal trait + tps.where_clause = self.parse_where_clause()?; + self.expect(&token::OpenDelim(token::Brace))?; + let mut trait_items = vec![]; + while !self.eat(&token::CloseDelim(token::Brace)) { + if let token::DocComment(_) = self.token.kind { + if self.look_ahead(1, + |tok| tok == &token::CloseDelim(token::Brace)) { + self.diagnostic().struct_span_err_with_code( + self.token.span, + "found a documentation comment that doesn't document anything", + DiagnosticId::Error("E0584".into()), + ) + .help( + "doc comments must come before what they document, maybe a \ + comment was intended with `//`?", + ) + .emit(); + self.bump(); + continue; + } + } + let mut at_end = false; + match self.parse_trait_item(&mut at_end) { + Ok(item) => trait_items.push(item), + Err(mut e) => { + e.emit(); + if !at_end { + self.recover_stmt_(SemiColonMode::Break, BlockMode::Break); + } + } + } + } + Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None)) + } + } + + /// Parses the items in a trait declaration. + pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, TraitItem> { + maybe_whole!(self, NtTraitItem, |x| x); + let attrs = self.parse_outer_attributes()?; + let mut unclosed_delims = vec![]; + let (mut item, tokens) = self.collect_tokens(|this| { + let item = this.parse_trait_item_(at_end, attrs); + unclosed_delims.append(&mut this.unclosed_delims); + item + })?; + self.unclosed_delims.append(&mut unclosed_delims); + // See `parse_item` for why this clause is here. + if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) { + item.tokens = Some(tokens); + } + Ok(item) + } + + fn parse_trait_item_(&mut self, + at_end: &mut bool, + mut attrs: Vec) -> PResult<'a, TraitItem> { + let lo = self.token.span; + self.eat_bad_pub(); + let (name, node, generics) = if self.eat_keyword(kw::Type) { + self.parse_trait_item_assoc_ty()? + } else if self.is_const_item() { + self.expect_keyword(kw::Const)?; + let ident = self.parse_ident()?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; + let default = if self.eat(&token::Eq) { + let expr = self.parse_expr()?; + self.expect(&token::Semi)?; + Some(expr) + } else { + self.expect(&token::Semi)?; + None + }; + (ident, TraitItemKind::Const(ty, default), ast::Generics::default()) + } else if let Some(mac) = self.parse_assoc_macro_invoc("trait", None, &mut false)? { + // trait item macro. + (Ident::invalid(), ast::TraitItemKind::Macro(mac), ast::Generics::default()) + } else { + let (constness, unsafety, asyncness, abi) = self.parse_fn_front_matter()?; + + let ident = self.parse_ident()?; + let mut generics = self.parse_generics()?; + + let decl = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| { + // This is somewhat dubious; We don't want to allow + // argument names to be left off if there is a + // definition... + + // We don't allow argument names to be left off in edition 2018. + let is_name_required = p.token.span.rust_2018(); + p.parse_arg_general(true, false, |_| is_name_required) + })?; + generics.where_clause = self.parse_where_clause()?; + + let sig = ast::MethodSig { + header: FnHeader { + unsafety, + constness, + abi, + asyncness, + }, + decl, + }; + + let body = match self.token.kind { + token::Semi => { + self.bump(); + *at_end = true; + debug!("parse_trait_methods(): parsing required method"); + None + } + token::OpenDelim(token::Brace) => { + debug!("parse_trait_methods(): parsing provided method"); + *at_end = true; + let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; + attrs.extend(inner_attrs.iter().cloned()); + Some(body) + } + token::Interpolated(ref nt) => { + match **nt { + token::NtBlock(..) => { + *at_end = true; + let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; + attrs.extend(inner_attrs.iter().cloned()); + Some(body) + } + _ => { + return self.expected_semi_or_open_brace(); + } + } + } + _ => { + return self.expected_semi_or_open_brace(); + } + }; + (ident, ast::TraitItemKind::Method(sig, body), generics) + }; + + Ok(TraitItem { + id: ast::DUMMY_NODE_ID, + ident: name, + attrs, + generics, + node, + span: lo.to(self.prev_span), + tokens: None, + }) + } + + /// Parses the following grammar: + /// + /// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty] + fn parse_trait_item_assoc_ty(&mut self) + -> PResult<'a, (Ident, TraitItemKind, ast::Generics)> { + let ident = self.parse_ident()?; + let mut generics = self.parse_generics()?; + + // Parse optional colon and param bounds. + let bounds = if self.eat(&token::Colon) { + self.parse_generic_bounds(None)? + } else { + Vec::new() + }; + generics.where_clause = self.parse_where_clause()?; + + let default = if self.eat(&token::Eq) { + Some(self.parse_ty()?) + } else { + None + }; + self.expect(&token::Semi)?; + + Ok((ident, TraitItemKind::Type(bounds, default), generics)) + } + + /// Parses a `UseTree`. + /// + /// ``` + /// USE_TREE = [`::`] `*` | + /// [`::`] `{` USE_TREE_LIST `}` | + /// PATH `::` `*` | + /// PATH `::` `{` USE_TREE_LIST `}` | + /// PATH [`as` IDENT] + /// ``` + fn parse_use_tree(&mut self) -> PResult<'a, UseTree> { + let lo = self.token.span; + + let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() }; + let kind = if self.check(&token::OpenDelim(token::Brace)) || + self.check(&token::BinOp(token::Star)) || + self.is_import_coupler() { + // `use *;` or `use ::*;` or `use {...};` or `use ::{...};` + let mod_sep_ctxt = self.token.span.ctxt(); + if self.eat(&token::ModSep) { + prefix.segments.push( + PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)) + ); + } + + if self.eat(&token::BinOp(token::Star)) { + UseTreeKind::Glob + } else { + UseTreeKind::Nested(self.parse_use_tree_list()?) + } + } else { + // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;` + prefix = self.parse_path(PathStyle::Mod)?; + + if self.eat(&token::ModSep) { + if self.eat(&token::BinOp(token::Star)) { + UseTreeKind::Glob + } else { + UseTreeKind::Nested(self.parse_use_tree_list()?) + } + } else { + UseTreeKind::Simple(self.parse_rename()?, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID) + } + }; + + Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) }) + } + + /// Parses a `UseTreeKind::Nested(list)`. + /// + /// ``` + /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`] + /// ``` + fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> { + self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, ast::DUMMY_NODE_ID))) + .map(|(r, _)| r) + } + + fn parse_rename(&mut self) -> PResult<'a, Option> { + if self.eat_keyword(kw::As) { + self.parse_ident_or_underscore().map(Some) + } else { + Ok(None) + } + } + + /// Parses `extern crate` links. + /// + /// # Examples + /// + /// ``` + /// extern crate foo; + /// extern crate bar as foo; + /// ``` + fn parse_item_extern_crate( + &mut self, + lo: Span, + visibility: Visibility, + attrs: Vec + ) -> PResult<'a, P> { + // Accept `extern crate name-like-this` for better diagnostics + let orig_name = self.parse_crate_name_with_dashes()?; + let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? { + (rename, Some(orig_name.name)) + } else { + (orig_name, None) + }; + self.expect(&token::Semi)?; + + let span = lo.to(self.prev_span); + Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs)) + } + + fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> { + let error_msg = "crate name using dashes are not valid in `extern crate` statements"; + let suggestion_msg = "if the original crate name uses dashes you need to use underscores \ + in the code"; + let mut ident = if self.token.is_keyword(kw::SelfLower) { + self.parse_path_segment_ident() + } else { + self.parse_ident() + }?; + let mut idents = vec![]; + let mut replacement = vec![]; + let mut fixed_crate_name = false; + // Accept `extern crate name-like-this` for better diagnostics + let dash = token::BinOp(token::BinOpToken::Minus); + if self.token == dash { // Do not include `-` as part of the expected tokens list + while self.eat(&dash) { + fixed_crate_name = true; + replacement.push((self.prev_span, "_".to_string())); + idents.push(self.parse_ident()?); + } + } + if fixed_crate_name { + let fixed_name_sp = ident.span.to(idents.last().unwrap().span); + let mut fixed_name = format!("{}", ident.name); + for part in idents { + fixed_name.push_str(&format!("_{}", part.name)); + } + ident = Ident::from_str(&fixed_name).with_span_pos(fixed_name_sp); + + self.struct_span_err(fixed_name_sp, error_msg) + .span_label(fixed_name_sp, "dash-separated idents are not valid") + .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable) + .emit(); + } + Ok(ident) + } + + /// Parses an item-position function declaration. + fn parse_item_fn( + &mut self, + unsafety: Unsafety, + asyncness: Spanned, + constness: Spanned, + abi: Abi + ) -> PResult<'a, ItemInfo> { + let (ident, mut generics) = self.parse_fn_header()?; + let allow_c_variadic = abi == Abi::C && unsafety == Unsafety::Unsafe; + let decl = self.parse_fn_decl(allow_c_variadic)?; + generics.where_clause = self.parse_where_clause()?; + let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; + let header = FnHeader { unsafety, asyncness, constness, abi }; + Ok((ident, ItemKind::Fn(decl, header, generics, body), Some(inner_attrs))) + } + + /// Parses the name and optional generic types of a function header. + fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> { + let id = self.parse_ident()?; + let generics = self.parse_generics()?; + Ok((id, generics)) + } + + /// Parses the argument list and result type of a function declaration. + fn parse_fn_decl(&mut self, allow_c_variadic: bool) -> PResult<'a, P> { + let (args, c_variadic) = self.parse_fn_args(true, allow_c_variadic)?; + let ret_ty = self.parse_ret_ty(true)?; + + Ok(P(FnDecl { + inputs: args, + output: ret_ty, + c_variadic, + })) + } + + /// Parses `extern` for foreign ABIs modules. + /// + /// `extern` is expected to have been + /// consumed before calling this method. + /// + /// # Examples + /// + /// ```ignore (only-for-syntax-highlight) + /// extern "C" {} + /// extern {} + /// ``` + fn parse_item_foreign_mod( + &mut self, + lo: Span, + opt_abi: Option, + visibility: Visibility, + mut attrs: Vec, + extern_sp: Span, + ) -> PResult<'a, P> { + self.expect(&token::OpenDelim(token::Brace))?; + + let abi = opt_abi.unwrap_or(Abi::C); + + attrs.extend(self.parse_inner_attributes()?); + + let mut foreign_items = vec![]; + while !self.eat(&token::CloseDelim(token::Brace)) { + foreign_items.push(self.parse_foreign_item(extern_sp)?); + } + + let prev_span = self.prev_span; + let m = ast::ForeignMod { + abi, + items: foreign_items + }; + let invalid = Ident::invalid(); + Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs)) + } + + /// Parses a foreign item. + crate fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, ForeignItem> { + maybe_whole!(self, NtForeignItem, |ni| ni); + + let attrs = self.parse_outer_attributes()?; + let lo = self.token.span; + let visibility = self.parse_visibility(false)?; + + // FOREIGN STATIC ITEM + // Treat `const` as `static` for error recovery, but don't add it to expected tokens. + if self.check_keyword(kw::Static) || self.token.is_keyword(kw::Const) { + if self.token.is_keyword(kw::Const) { + self.diagnostic() + .struct_span_err(self.token.span, "extern items cannot be `const`") + .span_suggestion( + self.token.span, + "try using a static value", + "static".to_owned(), + Applicability::MachineApplicable + ).emit(); + } + self.bump(); // `static` or `const` + return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?); + } + // FOREIGN FUNCTION ITEM + if self.check_keyword(kw::Fn) { + return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?); + } + // FOREIGN TYPE ITEM + if self.check_keyword(kw::Type) { + return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?); + } + + match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? { + Some(mac) => { + Ok( + ForeignItem { + ident: Ident::invalid(), + span: lo.to(self.prev_span), + id: ast::DUMMY_NODE_ID, + attrs, + vis: visibility, + node: ForeignItemKind::Macro(mac), + } + ) + } + None => { + if !attrs.is_empty() { + self.expected_item_err(&attrs)?; + } + + self.unexpected() + } + } + } + + /// Parses a function declaration from a foreign module. + fn parse_item_foreign_fn( + &mut self, + vis: ast::Visibility, + lo: Span, + attrs: Vec, + extern_sp: Span, + ) -> PResult<'a, ForeignItem> { + self.expect_keyword(kw::Fn)?; + + let (ident, mut generics) = self.parse_fn_header()?; + let decl = self.parse_fn_decl(true)?; + generics.where_clause = self.parse_where_clause()?; + let hi = self.token.span; + self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?; + Ok(ast::ForeignItem { + ident, + attrs, + node: ForeignItemKind::Fn(decl, generics), + id: ast::DUMMY_NODE_ID, + span: lo.to(hi), + vis, + }) + } + + /// Parses a static item from a foreign module. + /// Assumes that the `static` keyword is already parsed. + fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec) + -> PResult<'a, ForeignItem> { + let mutbl = self.parse_mutability(); + let ident = self.parse_ident()?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; + let hi = self.token.span; + self.expect(&token::Semi)?; + Ok(ForeignItem { + ident, + attrs, + node: ForeignItemKind::Static(ty, mutbl), + id: ast::DUMMY_NODE_ID, + span: lo.to(hi), + vis, + }) + } + + /// Parses a type from a foreign module. + fn parse_item_foreign_type(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec) + -> PResult<'a, ForeignItem> { + self.expect_keyword(kw::Type)?; + + let ident = self.parse_ident()?; + let hi = self.token.span; + self.expect(&token::Semi)?; + Ok(ast::ForeignItem { + ident, + attrs, + node: ForeignItemKind::Ty, + id: ast::DUMMY_NODE_ID, + span: lo.to(hi), + vis + }) + } + + fn is_static_global(&mut self) -> bool { + if self.check_keyword(kw::Static) { + // Check if this could be a closure + !self.look_ahead(1, |token| { + if token.is_keyword(kw::Move) { + return true; + } + match token.kind { + token::BinOp(token::Or) | token::OrOr => true, + _ => false, + } + }) + } else { + false + } + } + + fn parse_item_const(&mut self, m: Option) -> PResult<'a, ItemInfo> { + let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; + self.expect(&token::Eq)?; + let e = self.parse_expr()?; + self.expect(&token::Semi)?; + let item = match m { + Some(m) => ItemKind::Static(ty, m, e), + None => ItemKind::Const(ty, e), + }; + Ok((id, item, None)) + } + + /// Parses `type Foo = Bar;` or returns `None` + /// without modifying the parser state. + fn eat_type(&mut self) -> Option> { + // This parses the grammar: + // Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";" + if self.eat_keyword(kw::Type) { + Some(self.parse_type_alias()) + } else { + None + } + } + + /// Parses a type alias or opaque type. + fn parse_type_alias(&mut self) -> PResult<'a, (Ident, AliasKind, ast::Generics)> { + let ident = self.parse_ident()?; + let mut tps = self.parse_generics()?; + tps.where_clause = self.parse_where_clause()?; + self.expect(&token::Eq)?; + let alias = if self.check_keyword(kw::Impl) { + self.bump(); + let bounds = self.parse_generic_bounds(Some(self.prev_span))?; + AliasKind::OpaqueTy(bounds) + } else { + let ty = self.parse_ty()?; + AliasKind::Weak(ty) + }; + self.expect(&token::Semi)?; + Ok((ident, alias, tps)) + } + + /// Parses an enum declaration. + fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> { + let id = self.parse_ident()?; + let mut generics = self.parse_generics()?; + generics.where_clause = self.parse_where_clause()?; + self.expect(&token::OpenDelim(token::Brace))?; + + let enum_definition = self.parse_enum_def(&generics).map_err(|e| { + self.recover_stmt(); + self.eat(&token::CloseDelim(token::Brace)); + e + })?; + Ok((id, ItemKind::Enum(enum_definition, generics), None)) + } + + /// Parses the part of an enum declaration following the `{`. + fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> { + let mut variants = Vec::new(); + while self.token != token::CloseDelim(token::Brace) { + let variant_attrs = self.parse_outer_attributes()?; + let vlo = self.token.span; + + self.eat_bad_pub(); + let ident = self.parse_ident()?; + + let struct_def = if self.check(&token::OpenDelim(token::Brace)) { + // Parse a struct variant. + let (fields, recovered) = self.parse_record_struct_body()?; + VariantData::Struct(fields, recovered) + } else if self.check(&token::OpenDelim(token::Paren)) { + VariantData::Tuple( + self.parse_tuple_struct_body()?, + ast::DUMMY_NODE_ID, + ) + } else { + VariantData::Unit(ast::DUMMY_NODE_ID) + }; + + let disr_expr = if self.eat(&token::Eq) { + Some(AnonConst { + id: ast::DUMMY_NODE_ID, + value: self.parse_expr()?, + }) + } else { + None + }; + + let vr = ast::Variant_ { + ident, + id: ast::DUMMY_NODE_ID, + attrs: variant_attrs, + data: struct_def, + disr_expr, + }; + variants.push(respan(vlo.to(self.prev_span), vr)); + + if !self.eat(&token::Comma) { + if self.token.is_ident() && !self.token.is_reserved_ident() { + let sp = self.sess.source_map().next_point(self.prev_span); + self.struct_span_err(sp, "missing comma") + .span_suggestion_short( + sp, + "missing comma", + ",".to_owned(), + Applicability::MaybeIncorrect, + ) + .emit(); + } else { + break; + } + } + } + self.expect(&token::CloseDelim(token::Brace))?; + + Ok(ast::EnumDef { variants }) + } + + /// Parses `struct Foo { ... }`. + fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> { + let class_name = self.parse_ident()?; + + let mut generics = self.parse_generics()?; + + // There is a special case worth noting here, as reported in issue #17904. + // If we are parsing a tuple struct it is the case that the where clause + // should follow the field list. Like so: + // + // struct Foo(T) where T: Copy; + // + // If we are parsing a normal record-style struct it is the case + // that the where clause comes before the body, and after the generics. + // So if we look ahead and see a brace or a where-clause we begin + // parsing a record style struct. + // + // Otherwise if we look ahead and see a paren we parse a tuple-style + // struct. + + let vdata = if self.token.is_keyword(kw::Where) { + generics.where_clause = self.parse_where_clause()?; + if self.eat(&token::Semi) { + // If we see a: `struct Foo where T: Copy;` style decl. + VariantData::Unit(ast::DUMMY_NODE_ID) + } else { + // If we see: `struct Foo where T: Copy { ... }` + let (fields, recovered) = self.parse_record_struct_body()?; + VariantData::Struct(fields, recovered) + } + // No `where` so: `struct Foo;` + } else if self.eat(&token::Semi) { + VariantData::Unit(ast::DUMMY_NODE_ID) + // Record-style struct definition + } else if self.token == token::OpenDelim(token::Brace) { + let (fields, recovered) = self.parse_record_struct_body()?; + VariantData::Struct(fields, recovered) + // Tuple-style struct definition with optional where-clause. + } else if self.token == token::OpenDelim(token::Paren) { + let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID); + generics.where_clause = self.parse_where_clause()?; + self.expect(&token::Semi)?; + body + } else { + let token_str = self.this_token_descr(); + let mut err = self.fatal(&format!( + "expected `where`, `{{`, `(`, or `;` after struct name, found {}", + token_str + )); + err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name"); + return Err(err); + }; + + Ok((class_name, ItemKind::Struct(vdata, generics), None)) + } + + /// Parses `union Foo { ... }`. + fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> { + let class_name = self.parse_ident()?; + + let mut generics = self.parse_generics()?; + + let vdata = if self.token.is_keyword(kw::Where) { + generics.where_clause = self.parse_where_clause()?; + let (fields, recovered) = self.parse_record_struct_body()?; + VariantData::Struct(fields, recovered) + } else if self.token == token::OpenDelim(token::Brace) { + let (fields, recovered) = self.parse_record_struct_body()?; + VariantData::Struct(fields, recovered) + } else { + let token_str = self.this_token_descr(); + let mut err = self.fatal(&format!( + "expected `where` or `{{` after union name, found {}", token_str)); + err.span_label(self.token.span, "expected `where` or `{` after union name"); + return Err(err); + }; + + Ok((class_name, ItemKind::Union(vdata, generics), None)) + } + + pub(super) fn is_union_item(&self) -> bool { + self.token.is_keyword(kw::Union) && + self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident()) + } + + fn parse_record_struct_body( + &mut self, + ) -> PResult<'a, (Vec, /* recovered */ bool)> { + let mut fields = Vec::new(); + let mut recovered = false; + if self.eat(&token::OpenDelim(token::Brace)) { + while self.token != token::CloseDelim(token::Brace) { + let field = self.parse_struct_decl_field().map_err(|e| { + self.recover_stmt(); + recovered = true; + e + }); + match field { + Ok(field) => fields.push(field), + Err(mut err) => { + err.emit(); + } + } + } + self.eat(&token::CloseDelim(token::Brace)); + } else { + let token_str = self.this_token_descr(); + let mut err = self.fatal(&format!( + "expected `where`, or `{{` after struct name, found {}", token_str)); + err.span_label(self.token.span, "expected `where`, or `{` after struct name"); + return Err(err); + } + + Ok((fields, recovered)) + } + + fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec> { + // This is the case where we find `struct Foo(T) where T: Copy;` + // Unit like structs are handled in parse_item_struct function + self.parse_paren_comma_seq(|p| { + let attrs = p.parse_outer_attributes()?; + let lo = p.token.span; + let vis = p.parse_visibility(true)?; + let ty = p.parse_ty()?; + Ok(StructField { + span: lo.to(ty.span), + vis, + ident: None, + id: ast::DUMMY_NODE_ID, + ty, + attrs, + }) + }).map(|(r, _)| r) + } + + /// Parses an element of a struct declaration. + fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { + let attrs = self.parse_outer_attributes()?; + let lo = self.token.span; + let vis = self.parse_visibility(false)?; + self.parse_single_struct_field(lo, vis, attrs) + } + + /// Parses a structure field declaration. + fn parse_single_struct_field(&mut self, + lo: Span, + vis: Visibility, + attrs: Vec ) + -> PResult<'a, StructField> { + let mut seen_comma: bool = false; + let a_var = self.parse_name_and_ty(lo, vis, attrs)?; + if self.token == token::Comma { + seen_comma = true; + } + match self.token.kind { + token::Comma => { + self.bump(); + } + token::CloseDelim(token::Brace) => {} + token::DocComment(_) => { + let previous_span = self.prev_span; + let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment); + self.bump(); // consume the doc comment + let comma_after_doc_seen = self.eat(&token::Comma); + // `seen_comma` is always false, because we are inside doc block + // condition is here to make code more readable + if seen_comma == false && comma_after_doc_seen == true { + seen_comma = true; + } + if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) { + err.emit(); + } else { + if seen_comma == false { + let sp = self.sess.source_map().next_point(previous_span); + err.span_suggestion( + sp, + "missing comma here", + ",".into(), + Applicability::MachineApplicable + ); + } + return Err(err); + } + } + _ => { + let sp = self.sess.source_map().next_point(self.prev_span); + let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}", + self.this_token_descr())); + if self.token.is_ident() { + // This is likely another field; emit the diagnostic and keep going + err.span_suggestion( + sp, + "try adding a comma", + ",".into(), + Applicability::MachineApplicable, + ); + err.emit(); + } else { + return Err(err) + } + } + } + Ok(a_var) + } + + /// Parses a structure field. + fn parse_name_and_ty( + &mut self, + lo: Span, + vis: Visibility, + attrs: Vec + ) -> PResult<'a, StructField> { + let name = self.parse_ident()?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; + Ok(StructField { + span: lo.to(self.prev_span), + ident: Some(name), + vis, + id: ast::DUMMY_NODE_ID, + ty, + attrs, + }) + } + + pub(super) fn eat_macro_def( + &mut self, + attrs: &[Attribute], + vis: &Visibility, + lo: Span + ) -> PResult<'a, Option>> { + let token_lo = self.token.span; + let (ident, def) = if self.eat_keyword(kw::Macro) { + let ident = self.parse_ident()?; + let tokens = if self.check(&token::OpenDelim(token::Brace)) { + match self.parse_token_tree() { + TokenTree::Delimited(_, _, tts) => tts, + _ => unreachable!(), + } + } else if self.check(&token::OpenDelim(token::Paren)) { + let args = self.parse_token_tree(); + let body = if self.check(&token::OpenDelim(token::Brace)) { + self.parse_token_tree() + } else { + self.unexpected()?; + unreachable!() + }; + TokenStream::new(vec![ + args.into(), + TokenTree::token(token::FatArrow, token_lo.to(self.prev_span)).into(), + body.into(), + ]) + } else { + self.unexpected()?; + unreachable!() + }; + + (ident, ast::MacroDef { tokens: tokens.into(), legacy: false }) + } else if self.check_keyword(sym::macro_rules) && + self.look_ahead(1, |t| *t == token::Not) && + self.look_ahead(2, |t| t.is_ident()) { + let prev_span = self.prev_span; + self.complain_if_pub_macro(&vis.node, prev_span); + self.bump(); + self.bump(); + + let ident = self.parse_ident()?; + let (delim, tokens) = self.expect_delimited_token_tree()?; + if delim != MacDelimiter::Brace && !self.eat(&token::Semi) { + self.report_invalid_macro_expansion_item(); + } + + (ident, ast::MacroDef { tokens, legacy: true }) + } else { + return Ok(None); + }; + + let span = lo.to(self.prev_span); + Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec()))) + } + + fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) { + match *vis { + VisibilityKind::Inherited => {} + _ => { + let mut err = if self.token.is_keyword(sym::macro_rules) { + let mut err = self.diagnostic() + .struct_span_err(sp, "can't qualify macro_rules invocation with `pub`"); + err.span_suggestion( + sp, + "try exporting the macro", + "#[macro_export]".to_owned(), + Applicability::MaybeIncorrect // speculative + ); + err + } else { + let mut err = self.diagnostic() + .struct_span_err(sp, "can't qualify macro invocation with `pub`"); + err.help("try adjusting the macro to put `pub` inside the invocation"); + err + }; + err.emit(); + } + } + } + + fn mk_item(&self, span: Span, ident: Ident, node: ItemKind, vis: Visibility, + attrs: Vec) -> P { + P(Item { + ident, + attrs, + id: ast::DUMMY_NODE_ID, + node, + vis, + span, + tokens: None, + }) + } +} diff --git a/src/libsyntax/parse/parser/module.rs b/src/libsyntax/parse/parser/module.rs new file mode 100644 index 0000000000000..58a7ffba948b3 --- /dev/null +++ b/src/libsyntax/parse/parser/module.rs @@ -0,0 +1,332 @@ +use super::{Parser, PResult}; +use super::item::ItemInfo; + +use crate::attr; +use crate::ast::{self, Ident, Attribute, ItemKind, Mod, Crate}; +use crate::parse::{new_sub_parser_from_file, DirectoryOwnership}; +use crate::parse::token::{self, TokenKind}; +use crate::parse::diagnostics::{Error}; +use crate::source_map::{SourceMap, Span, DUMMY_SP, FileName}; +use crate::symbol::sym; + +use std::path::{self, Path, PathBuf}; + +/// Information about the path to a module. +pub struct ModulePath { + name: String, + path_exists: bool, + pub result: Result, +} + +pub struct ModulePathSuccess { + pub path: PathBuf, + pub directory_ownership: DirectoryOwnership, + warn: bool, +} + +impl<'a> Parser<'a> { + /// Parses a source module as a crate. This is the main entry point for the parser. + pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> { + let lo = self.token.span; + let krate = Ok(ast::Crate { + attrs: self.parse_inner_attributes()?, + module: self.parse_mod_items(&token::Eof, lo)?, + span: lo.to(self.token.span), + }); + krate + } + + /// Parse a `mod { ... }` or `mod ;` item + pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> { + let (in_cfg, outer_attrs) = { + let mut strip_unconfigured = crate::config::StripUnconfigured { + sess: self.sess, + features: None, // don't perform gated feature checking + }; + let mut outer_attrs = outer_attrs.to_owned(); + strip_unconfigured.process_cfg_attrs(&mut outer_attrs); + (!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs) + }; + + let id_span = self.token.span; + let id = self.parse_ident()?; + if self.eat(&token::Semi) { + if in_cfg && self.recurse_into_file_modules { + // This mod is in an external file. Let's go get it! + let ModulePathSuccess { path, directory_ownership, warn } = + self.submod_path(id, &outer_attrs, id_span)?; + let (module, mut attrs) = + self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?; + // Record that we fetched the mod from an external file + if warn { + let attr = attr::mk_attr_outer( + attr::mk_word_item(Ident::with_empty_ctxt(sym::warn_directory_ownership))); + attr::mark_known(&attr); + attrs.push(attr); + } + Ok((id, ItemKind::Mod(module), Some(attrs))) + } else { + let placeholder = ast::Mod { + inner: DUMMY_SP, + items: Vec::new(), + inline: false + }; + Ok((id, ItemKind::Mod(placeholder), None)) + } + } else { + let old_directory = self.directory.clone(); + self.push_directory(id, &outer_attrs); + + self.expect(&token::OpenDelim(token::Brace))?; + let mod_inner_lo = self.token.span; + let attrs = self.parse_inner_attributes()?; + let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?; + + self.directory = old_directory; + Ok((id, ItemKind::Mod(module), Some(attrs))) + } + } + + /// Given a termination token, parses all of the items in a module. + fn parse_mod_items(&mut self, term: &TokenKind, inner_lo: Span) -> PResult<'a, Mod> { + let mut items = vec![]; + while let Some(item) = self.parse_item()? { + items.push(item); + self.maybe_consume_incorrect_semicolon(&items); + } + + if !self.eat(term) { + let token_str = self.this_token_descr(); + if !self.maybe_consume_incorrect_semicolon(&items) { + let mut err = self.fatal(&format!("expected item, found {}", token_str)); + err.span_label(self.token.span, "expected item"); + return Err(err); + } + } + + let hi = if self.token.span.is_dummy() { + inner_lo + } else { + self.prev_span + }; + + Ok(Mod { + inner: inner_lo.to(hi), + items, + inline: true + }) + } + + fn submod_path( + &mut self, + id: ast::Ident, + outer_attrs: &[Attribute], + id_sp: Span + ) -> PResult<'a, ModulePathSuccess> { + if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) { + return Ok(ModulePathSuccess { + directory_ownership: match path.file_name().and_then(|s| s.to_str()) { + // All `#[path]` files are treated as though they are a `mod.rs` file. + // This means that `mod foo;` declarations inside `#[path]`-included + // files are siblings, + // + // Note that this will produce weirdness when a file named `foo.rs` is + // `#[path]` included and contains a `mod foo;` declaration. + // If you encounter this, it's your own darn fault :P + Some(_) => DirectoryOwnership::Owned { relative: None }, + _ => DirectoryOwnership::UnownedViaMod(true), + }, + path, + warn: false, + }); + } + + let relative = match self.directory.ownership { + DirectoryOwnership::Owned { relative } => relative, + DirectoryOwnership::UnownedViaBlock | + DirectoryOwnership::UnownedViaMod(_) => None, + }; + let paths = Parser::default_submod_path( + id, relative, &self.directory.path, self.sess.source_map()); + + match self.directory.ownership { + DirectoryOwnership::Owned { .. } => { + paths.result.map_err(|err| self.span_fatal_err(id_sp, err)) + }, + DirectoryOwnership::UnownedViaBlock => { + let msg = + "Cannot declare a non-inline module inside a block \ + unless it has a path attribute"; + let mut err = self.diagnostic().struct_span_err(id_sp, msg); + if paths.path_exists { + let msg = format!("Maybe `use` the module `{}` instead of redeclaring it", + paths.name); + err.span_note(id_sp, &msg); + } + Err(err) + } + DirectoryOwnership::UnownedViaMod(warn) => { + if warn { + if let Ok(result) = paths.result { + return Ok(ModulePathSuccess { warn: true, ..result }); + } + } + let mut err = self.diagnostic().struct_span_err(id_sp, + "cannot declare a new module at this location"); + if !id_sp.is_dummy() { + let src_path = self.sess.source_map().span_to_filename(id_sp); + if let FileName::Real(src_path) = src_path { + if let Some(stem) = src_path.file_stem() { + let mut dest_path = src_path.clone(); + dest_path.set_file_name(stem); + dest_path.push("mod.rs"); + err.span_note(id_sp, + &format!("maybe move this module `{}` to its own \ + directory via `{}`", src_path.display(), + dest_path.display())); + } + } + } + if paths.path_exists { + err.span_note(id_sp, + &format!("... or maybe `use` the module `{}` instead \ + of possibly redeclaring it", + paths.name)); + } + Err(err) + } + } + } + + pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option { + if let Some(s) = attr::first_attr_value_str_by_name(attrs, sym::path) { + let s = s.as_str(); + + // On windows, the base path might have the form + // `\\?\foo\bar` in which case it does not tolerate + // mixed `/` and `\` separators, so canonicalize + // `/` to `\`. + #[cfg(windows)] + let s = s.replace("/", "\\"); + Some(dir_path.join(s)) + } else { + None + } + } + + /// Returns a path to a module. + pub fn default_submod_path( + id: ast::Ident, + relative: Option, + dir_path: &Path, + source_map: &SourceMap) -> ModulePath + { + // If we're in a foo.rs file instead of a mod.rs file, + // we need to look for submodules in + // `./foo/.rs` and `./foo//mod.rs` rather than + // `./.rs` and `.//mod.rs`. + let relative_prefix_string; + let relative_prefix = if let Some(ident) = relative { + relative_prefix_string = format!("{}{}", ident.as_str(), path::MAIN_SEPARATOR); + &relative_prefix_string + } else { + "" + }; + + let mod_name = id.to_string(); + let default_path_str = format!("{}{}.rs", relative_prefix, mod_name); + let secondary_path_str = format!("{}{}{}mod.rs", + relative_prefix, mod_name, path::MAIN_SEPARATOR); + let default_path = dir_path.join(&default_path_str); + let secondary_path = dir_path.join(&secondary_path_str); + let default_exists = source_map.file_exists(&default_path); + let secondary_exists = source_map.file_exists(&secondary_path); + + let result = match (default_exists, secondary_exists) { + (true, false) => Ok(ModulePathSuccess { + path: default_path, + directory_ownership: DirectoryOwnership::Owned { + relative: Some(id), + }, + warn: false, + }), + (false, true) => Ok(ModulePathSuccess { + path: secondary_path, + directory_ownership: DirectoryOwnership::Owned { + relative: None, + }, + warn: false, + }), + (false, false) => Err(Error::FileNotFoundForModule { + mod_name: mod_name.clone(), + default_path: default_path_str, + secondary_path: secondary_path_str, + dir_path: dir_path.display().to_string(), + }), + (true, true) => Err(Error::DuplicatePaths { + mod_name: mod_name.clone(), + default_path: default_path_str, + secondary_path: secondary_path_str, + }), + }; + + ModulePath { + name: mod_name, + path_exists: default_exists || secondary_exists, + result, + } + } + + /// Reads a module from a source file. + fn eval_src_mod( + &mut self, + path: PathBuf, + directory_ownership: DirectoryOwnership, + name: String, + id_sp: Span, + ) -> PResult<'a, (Mod, Vec)> { + let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut(); + if let Some(i) = included_mod_stack.iter().position(|p| *p == path) { + let mut err = String::from("circular modules: "); + let len = included_mod_stack.len(); + for p in &included_mod_stack[i.. len] { + err.push_str(&p.to_string_lossy()); + err.push_str(" -> "); + } + err.push_str(&path.to_string_lossy()); + return Err(self.span_fatal(id_sp, &err[..])); + } + included_mod_stack.push(path.clone()); + drop(included_mod_stack); + + let mut p0 = + new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp); + p0.cfg_mods = self.cfg_mods; + let mod_inner_lo = p0.token.span; + let mod_attrs = p0.parse_inner_attributes()?; + let mut m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?; + m0.inline = false; + self.sess.included_mod_stack.borrow_mut().pop(); + Ok((m0, mod_attrs)) + } + + fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) { + if let Some(path) = attr::first_attr_value_str_by_name(attrs, sym::path) { + self.directory.path.to_mut().push(&path.as_str()); + self.directory.ownership = DirectoryOwnership::Owned { relative: None }; + } else { + // We have to push on the current module name in the case of relative + // paths in order to ensure that any additional module paths from inline + // `mod x { ... }` come after the relative extension. + // + // For example, a `mod z { ... }` inside `x/y.rs` should set the current + // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`. + if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership { + if let Some(ident) = relative.take() { // remove the relative offset + self.directory.path.to_mut().push(ident.as_str()); + } + } + self.directory.path.to_mut().push(&id.as_str()); + } + } +} From 3dbfbafe3eb1562592044a8cf134823cdc7f2f12 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 19:59:27 +0200 Subject: [PATCH 139/148] parser: split into {ty, path}.rs --- src/libsyntax/parse/parser.rs | 908 +---------------------------- src/libsyntax/parse/parser/path.rs | 474 +++++++++++++++ src/libsyntax/parse/parser/ty.rs | 447 ++++++++++++++ 3 files changed, 930 insertions(+), 899 deletions(-) create mode 100644 src/libsyntax/parse/parser/path.rs create mode 100644 src/libsyntax/parse/parser/ty.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6537f59395f4e..490abc8edd0b7 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1,5 +1,3 @@ -// ignore-tidy-filelength - mod expr; use expr::LhsExpr; mod pat; @@ -7,20 +5,21 @@ mod item; pub use item::AliasKind; mod module; pub use module::{ModulePath, ModulePathSuccess}; +mod ty; +mod path; +pub use path::PathStyle; -use crate::ast::{self, AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy}; -use crate::ast::{AnonConst, Arg, Attribute, BindingMode}; +use crate::ast::{self, AttrStyle}; +use crate::ast::{Arg, Attribute, BindingMode}; use crate::ast::{Block, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind}; -use crate::ast::{FnDecl, FunctionRetTy}; +use crate::ast::{FnDecl}; use crate::ast::{Ident, IsAsync, Local, Lifetime}; use crate::ast::{MacStmtStyle, Mac_, MacDelimiter}; -use crate::ast::{MutTy, Mutability}; -use crate::ast::{PolyTraitRef, QSelf, PathSegment}; +use crate::ast::{Mutability}; use crate::ast::StrStyle; use crate::ast::SelfKind; -use crate::ast::{GenericBound, TraitBoundModifier, TraitObjectSyntax}; -use crate::ast::{GenericParam, GenericParamKind, GenericArg, WhereClause}; -use crate::ast::{Ty, TyKind, AssocTyConstraint, AssocTyConstraintKind, GenericBounds}; +use crate::ast::{GenericParam, GenericParamKind, WhereClause}; +use crate::ast::{Ty, TyKind, GenericBounds}; use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; use crate::ext::base::DummyResult; use crate::ext::hygiene::SyntaxContext; @@ -54,29 +53,6 @@ bitflags::bitflags! { } } -/// Specifies how to parse a path. -#[derive(Copy, Clone, PartialEq)] -pub enum PathStyle { - /// In some contexts, notably in expressions, paths with generic arguments are ambiguous - /// with something else. For example, in expressions `segment < ....` can be interpreted - /// as a comparison and `segment ( ....` can be interpreted as a function call. - /// In all such contexts the non-path interpretation is preferred by default for practical - /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g. - /// `x` - comparisons, `x::` - unambiguously a path. - Expr, - /// In other contexts, notably in types, no ambiguity exists and paths can be written - /// without the disambiguator, e.g., `x` - unambiguously a path. - /// Paths with disambiguators are still accepted, `x::` - unambiguously a path too. - Type, - /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports, - /// visibilities or attributes. - /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead - /// (paths in "mod" contexts have to be checked later for absence of generic arguments - /// anyway, due to macros), but it is used to avoid weird suggestions about expected - /// tokens when something goes wrong. - Mod, -} - #[derive(Clone, Copy, PartialEq, Debug)] crate enum SemiColonMode { Break, @@ -357,16 +333,6 @@ impl TokenType { } } -/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT`, -/// `IDENT<::AssocTy>`. -/// -/// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes -/// that `IDENT` is not the ident of a fn trait. -fn can_continue_type_after_non_fn_ident(t: &Token) -> bool { - t == &token::ModSep || t == &token::Lt || - t == &token::BinOp(token::Shl) -} - #[derive(Copy, Clone, Debug)] crate enum TokenExpectType { Expect, @@ -976,50 +942,6 @@ impl<'a> Parser<'a> { self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw))) } - /// Is the current token one of the keywords that signals a bare function type? - fn token_is_bare_fn_keyword(&mut self) -> bool { - self.check_keyword(kw::Fn) || - self.check_keyword(kw::Unsafe) || - self.check_keyword(kw::Extern) - } - - /// Parses a `TyKind::BareFn` type. - fn parse_ty_bare_fn(&mut self, generic_params: Vec) -> PResult<'a, TyKind> { - /* - - [unsafe] [extern "ABI"] fn (S) -> T - ^~~~^ ^~~~^ ^~^ ^ - | | | | - | | | Return type - | | Argument types - | | - | ABI - Function Style - */ - - let unsafety = self.parse_unsafety(); - let abi = if self.eat_keyword(kw::Extern) { - self.parse_opt_abi()?.unwrap_or(Abi::C) - } else { - Abi::Rust - }; - - self.expect_keyword(kw::Fn)?; - let (inputs, c_variadic) = self.parse_fn_args(false, true)?; - let ret_ty = self.parse_ret_ty(false)?; - let decl = P(FnDecl { - inputs, - output: ret_ty, - c_variadic, - }); - Ok(TyKind::BareFn(P(BareFnTy { - abi, - unsafety, - generic_params, - decl, - }))) - } - /// Parses asyncness: `async` or nothing. fn parse_asyncness(&mut self) -> IsAsync { if self.eat_keyword(kw::Async) { @@ -1041,236 +963,6 @@ impl<'a> Parser<'a> { } } - /// Parses an optional return type `[ -> TY ]` in a function declaration. - fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> { - if self.eat(&token::RArrow) { - Ok(FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true, false)?)) - } else { - Ok(FunctionRetTy::Default(self.token.span.shrink_to_lo())) - } - } - - /// Parses a type. - pub fn parse_ty(&mut self) -> PResult<'a, P> { - self.parse_ty_common(true, true, false) - } - - /// Parses a type in restricted contexts where `+` is not permitted. - /// - /// Example 1: `&'a TYPE` - /// `+` is prohibited to maintain operator priority (P(+) < P(&)). - /// Example 2: `value1 as TYPE + value2` - /// `+` is prohibited to avoid interactions with expression grammar. - fn parse_ty_no_plus(&mut self) -> PResult<'a, P> { - self.parse_ty_common(false, true, false) - } - - fn parse_ty_common(&mut self, allow_plus: bool, allow_qpath_recovery: bool, - allow_c_variadic: bool) -> PResult<'a, P> { - maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery); - maybe_whole!(self, NtTy, |x| x); - - let lo = self.token.span; - let mut impl_dyn_multi = false; - let node = if self.eat(&token::OpenDelim(token::Paren)) { - // `(TYPE)` is a parenthesized type. - // `(TYPE,)` is a tuple with a single field of type TYPE. - let mut ts = vec![]; - let mut last_comma = false; - while self.token != token::CloseDelim(token::Paren) { - ts.push(self.parse_ty()?); - if self.eat(&token::Comma) { - last_comma = true; - } else { - last_comma = false; - break; - } - } - let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus; - self.expect(&token::CloseDelim(token::Paren))?; - - if ts.len() == 1 && !last_comma { - let ty = ts.into_iter().nth(0).unwrap().into_inner(); - let maybe_bounds = allow_plus && self.token.is_like_plus(); - match ty.node { - // `(TY_BOUND_NOPAREN) + BOUND + ...`. - TyKind::Path(None, ref path) if maybe_bounds => { - self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)? - } - TyKind::TraitObject(ref bounds, TraitObjectSyntax::None) - if maybe_bounds && bounds.len() == 1 && !trailing_plus => { - let path = match bounds[0] { - GenericBound::Trait(ref pt, ..) => pt.trait_ref.path.clone(), - GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"), - }; - self.parse_remaining_bounds(Vec::new(), path, lo, true)? - } - // `(TYPE)` - _ => TyKind::Paren(P(ty)) - } - } else { - TyKind::Tup(ts) - } - } else if self.eat(&token::Not) { - // Never type `!` - TyKind::Never - } else if self.eat(&token::BinOp(token::Star)) { - // Raw pointer - TyKind::Ptr(self.parse_ptr()?) - } else if self.eat(&token::OpenDelim(token::Bracket)) { - // Array or slice - let t = self.parse_ty()?; - // Parse optional `; EXPR` in `[TYPE; EXPR]` - let t = match self.maybe_parse_fixed_length_of_vec()? { - None => TyKind::Slice(t), - Some(length) => TyKind::Array(t, AnonConst { - id: ast::DUMMY_NODE_ID, - value: length, - }), - }; - self.expect(&token::CloseDelim(token::Bracket))?; - t - } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) { - // Reference - self.expect_and()?; - self.parse_borrowed_pointee()? - } else if self.eat_keyword_noexpect(kw::Typeof) { - // `typeof(EXPR)` - // In order to not be ambiguous, the type must be surrounded by parens. - self.expect(&token::OpenDelim(token::Paren))?; - let e = AnonConst { - id: ast::DUMMY_NODE_ID, - value: self.parse_expr()?, - }; - self.expect(&token::CloseDelim(token::Paren))?; - TyKind::Typeof(e) - } else if self.eat_keyword(kw::Underscore) { - // A type to be inferred `_` - TyKind::Infer - } else if self.token_is_bare_fn_keyword() { - // Function pointer type - self.parse_ty_bare_fn(Vec::new())? - } else if self.check_keyword(kw::For) { - // Function pointer type or bound list (trait object type) starting with a poly-trait. - // `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T` - // `for<'lt> Trait1<'lt> + Trait2 + 'a` - let lo = self.token.span; - let lifetime_defs = self.parse_late_bound_lifetime_defs()?; - if self.token_is_bare_fn_keyword() { - self.parse_ty_bare_fn(lifetime_defs)? - } else { - let path = self.parse_path(PathStyle::Type)?; - let parse_plus = allow_plus && self.check_plus(); - self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)? - } - } else if self.eat_keyword(kw::Impl) { - // Always parse bounds greedily for better error recovery. - let bounds = self.parse_generic_bounds(None)?; - impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; - TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds) - } else if self.check_keyword(kw::Dyn) && - (self.token.span.rust_2018() || - self.look_ahead(1, |t| t.can_begin_bound() && - !can_continue_type_after_non_fn_ident(t))) { - self.bump(); // `dyn` - // Always parse bounds greedily for better error recovery. - let bounds = self.parse_generic_bounds(None)?; - impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; - TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn) - } else if self.check(&token::Question) || - self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus()) { - // Bound list (trait object type) - TyKind::TraitObject(self.parse_generic_bounds_common(allow_plus, None)?, - TraitObjectSyntax::None) - } else if self.eat_lt() { - // Qualified path - let (qself, path) = self.parse_qpath(PathStyle::Type)?; - TyKind::Path(Some(qself), path) - } else if self.token.is_path_start() { - // Simple path - let path = self.parse_path(PathStyle::Type)?; - if self.eat(&token::Not) { - // Macro invocation in type position - let (delim, tts) = self.expect_delimited_token_tree()?; - let node = Mac_ { - path, - tts, - delim, - prior_type_ascription: self.last_type_ascription, - }; - TyKind::Mac(respan(lo.to(self.prev_span), node)) - } else { - // Just a type path or bound list (trait object type) starting with a trait. - // `Type` - // `Trait1 + Trait2 + 'a` - if allow_plus && self.check_plus() { - self.parse_remaining_bounds(Vec::new(), path, lo, true)? - } else { - TyKind::Path(None, path) - } - } - } else if self.check(&token::DotDotDot) { - if allow_c_variadic { - self.eat(&token::DotDotDot); - TyKind::CVarArgs - } else { - return Err(self.fatal( - "only foreign functions are allowed to be C-variadic" - )); - } - } else { - let msg = format!("expected type, found {}", self.this_token_descr()); - let mut err = self.fatal(&msg); - err.span_label(self.token.span, "expected type"); - self.maybe_annotate_with_ascription(&mut err, true); - return Err(err); - }; - - let span = lo.to(self.prev_span); - let ty = P(Ty { node, span, id: ast::DUMMY_NODE_ID }); - - // Try to recover from use of `+` with incorrect priority. - self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty); - self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?; - self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery) - } - - fn parse_remaining_bounds(&mut self, generic_params: Vec, path: ast::Path, - lo: Span, parse_plus: bool) -> PResult<'a, TyKind> { - let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span)); - let mut bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)]; - if parse_plus { - self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded - bounds.append(&mut self.parse_generic_bounds(Some(self.prev_span))?); - } - Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None)) - } - - fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> { - let opt_lifetime = if self.check_lifetime() { Some(self.expect_lifetime()) } else { None }; - let mutbl = self.parse_mutability(); - let ty = self.parse_ty_no_plus()?; - return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl })); - } - - fn parse_ptr(&mut self) -> PResult<'a, MutTy> { - let mutbl = if self.eat_keyword(kw::Mut) { - Mutability::Mutable - } else if self.eat_keyword(kw::Const) { - Mutability::Immutable - } else { - let span = self.prev_span; - let msg = "expected mut or const in raw pointer type"; - self.struct_span_err(span, msg) - .span_label(span, msg) - .help("use `*mut T` or `*const T` as appropriate") - .emit(); - Mutability::Immutable - }; - let t = self.parse_ty_no_plus()?; - Ok(MutTy { ty: t, mutbl }) - } - fn is_named_argument(&self) -> bool { let offset = match self.token.kind { token::Interpolated(ref nt) => match **nt { @@ -1387,25 +1079,6 @@ impl<'a> Parser<'a> { }) } - fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option>> { - if self.eat(&token::Semi) { - Ok(Some(self.parse_expr()?)) - } else { - Ok(None) - } - } - - fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> { - match self.token.kind { - token::Ident(name, _) if name.is_path_segment_keyword() => { - let span = self.token.span; - self.bump(); - Ok(Ident::new(name, span)) - } - _ => self.parse_ident(), - } - } - fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> { match self.token.kind { token::Ident(name, false) if name == kw::Underscore => { @@ -1417,188 +1090,6 @@ impl<'a> Parser<'a> { } } - /// Parses a qualified path. - /// Assumes that the leading `<` has been parsed already. - /// - /// `qualified_path = ::path` - /// - /// # Examples - /// `::default` - /// `::a` - /// `::F::a` (without disambiguator) - /// `::F::a::` (with disambiguator) - fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, ast::Path)> { - let lo = self.prev_span; - let ty = self.parse_ty()?; - - // `path` will contain the prefix of the path up to the `>`, - // if any (e.g., `U` in the `::*` examples - // above). `path_span` has the span of that path, or an empty - // span in the case of something like `::Bar`. - let (mut path, path_span); - if self.eat_keyword(kw::As) { - let path_lo = self.token.span; - path = self.parse_path(PathStyle::Type)?; - path_span = path_lo.to(self.prev_span); - } else { - path_span = self.token.span.to(self.token.span); - path = ast::Path { segments: Vec::new(), span: path_span }; - } - - // See doc comment for `unmatched_angle_bracket_count`. - self.expect(&token::Gt)?; - if self.unmatched_angle_bracket_count > 0 { - self.unmatched_angle_bracket_count -= 1; - debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count); - } - - self.expect(&token::ModSep)?; - - let qself = QSelf { ty, path_span, position: path.segments.len() }; - self.parse_path_segments(&mut path.segments, style)?; - - Ok((qself, ast::Path { segments: path.segments, span: lo.to(self.prev_span) })) - } - - /// Parses simple paths. - /// - /// `path = [::] segment+` - /// `segment = ident | ident[::] | ident[::](args) [-> type]` - /// - /// # Examples - /// `a::b::C` (without disambiguator) - /// `a::b::C::` (with disambiguator) - /// `Fn(Args)` (without disambiguator) - /// `Fn::(Args)` (with disambiguator) - pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> { - maybe_whole!(self, NtPath, |path| { - if style == PathStyle::Mod && - path.segments.iter().any(|segment| segment.args.is_some()) { - self.diagnostic().span_err(path.span, "unexpected generic arguments in path"); - } - path - }); - - let lo = self.meta_var_span.unwrap_or(self.token.span); - let mut segments = Vec::new(); - let mod_sep_ctxt = self.token.span.ctxt(); - if self.eat(&token::ModSep) { - segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt))); - } - self.parse_path_segments(&mut segments, style)?; - - Ok(ast::Path { segments, span: lo.to(self.prev_span) }) - } - - /// Like `parse_path`, but also supports parsing `Word` meta items into paths for - /// backwards-compatibility. This is used when parsing derive macro paths in `#[derive]` - /// attributes. - pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast::Path> { - let meta_ident = match self.token.kind { - token::Interpolated(ref nt) => match **nt { - token::NtMeta(ref meta) => match meta.node { - ast::MetaItemKind::Word => Some(meta.path.clone()), - _ => None, - }, - _ => None, - }, - _ => None, - }; - if let Some(path) = meta_ident { - self.bump(); - return Ok(path); - } - self.parse_path(style) - } - - crate fn parse_path_segments(&mut self, - segments: &mut Vec, - style: PathStyle) - -> PResult<'a, ()> { - loop { - let segment = self.parse_path_segment(style)?; - if style == PathStyle::Expr { - // In order to check for trailing angle brackets, we must have finished - // recursing (`parse_path_segment` can indirectly call this function), - // that is, the next token must be the highlighted part of the below example: - // - // `Foo::>::Qux` - // ^ here - // - // As opposed to the below highlight (if we had only finished the first - // recursion): - // - // `Foo::>::Qux` - // ^ here - // - // `PathStyle::Expr` is only provided at the root invocation and never in - // `parse_path_segment` to recurse and therefore can be checked to maintain - // this invariant. - self.check_trailing_angle_brackets(&segment, token::ModSep); - } - segments.push(segment); - - if self.is_import_coupler() || !self.eat(&token::ModSep) { - return Ok(()); - } - } - } - - fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> { - let ident = self.parse_path_segment_ident()?; - - let is_args_start = |token: &Token| match token.kind { - token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren) - | token::LArrow => true, - _ => false, - }; - let check_args_start = |this: &mut Self| { - this.expected_tokens.extend_from_slice( - &[TokenType::Token(token::Lt), TokenType::Token(token::OpenDelim(token::Paren))] - ); - is_args_start(&this.token) - }; - - Ok(if style == PathStyle::Type && check_args_start(self) || - style != PathStyle::Mod && self.check(&token::ModSep) - && self.look_ahead(1, |t| is_args_start(t)) { - // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If - // it isn't, then we reset the unmatched angle bracket count as we're about to start - // parsing a new path. - if style == PathStyle::Expr { - self.unmatched_angle_bracket_count = 0; - self.max_angle_bracket_count = 0; - } - - // Generic arguments are found - `<`, `(`, `::<` or `::(`. - self.eat(&token::ModSep); - let lo = self.token.span; - let args = if self.eat_lt() { - // `<'a, T, A = U>` - let (args, constraints) = - self.parse_generic_args_with_leaning_angle_bracket_recovery(style, lo)?; - self.expect_gt()?; - let span = lo.to(self.prev_span); - AngleBracketedArgs { args, constraints, span }.into() - } else { - // `(T, U) -> R` - let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?; - let span = lo.to(self.prev_span); - let output = if self.eat(&token::RArrow) { - Some(self.parse_ty_common(false, false, false)?) - } else { - None - }; - ParenthesizedArgs { inputs, output, span }.into() - }; - - PathSegment { ident, args, id: ast::DUMMY_NODE_ID } - } else { - // Generic arguments are not found. - PathSegment::from_ident(ident) - }) - } - crate fn check_lifetime(&mut self) -> bool { self.expected_tokens.push(TokenType::Lifetime); self.token.is_lifetime() @@ -2202,130 +1693,6 @@ impl<'a> Parser<'a> { }).emit(); } - /// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`. - /// - /// ``` - /// BOUND = TY_BOUND | LT_BOUND - /// LT_BOUND = LIFETIME (e.g., `'a`) - /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN) - /// TY_BOUND_NOPAREN = [?] [for] SIMPLE_PATH (e.g., `?for<'a: 'b> m::Trait<'a>`) - /// ``` - fn parse_generic_bounds_common(&mut self, - allow_plus: bool, - colon_span: Option) -> PResult<'a, GenericBounds> { - let mut bounds = Vec::new(); - let mut negative_bounds = Vec::new(); - let mut last_plus_span = None; - let mut was_negative = false; - loop { - // This needs to be synchronized with `TokenKind::can_begin_bound`. - let is_bound_start = self.check_path() || self.check_lifetime() || - self.check(&token::Not) || // used for error reporting only - self.check(&token::Question) || - self.check_keyword(kw::For) || - self.check(&token::OpenDelim(token::Paren)); - if is_bound_start { - let lo = self.token.span; - let has_parens = self.eat(&token::OpenDelim(token::Paren)); - let inner_lo = self.token.span; - let is_negative = self.eat(&token::Not); - let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None }; - if self.token.is_lifetime() { - if let Some(question_span) = question { - self.span_err(question_span, - "`?` may only modify trait bounds, not lifetime bounds"); - } - bounds.push(GenericBound::Outlives(self.expect_lifetime())); - if has_parens { - let inner_span = inner_lo.to(self.prev_span); - self.expect(&token::CloseDelim(token::Paren))?; - let mut err = self.struct_span_err( - lo.to(self.prev_span), - "parenthesized lifetime bounds are not supported" - ); - if let Ok(snippet) = self.span_to_snippet(inner_span) { - err.span_suggestion_short( - lo.to(self.prev_span), - "remove the parentheses", - snippet.to_owned(), - Applicability::MachineApplicable - ); - } - err.emit(); - } - } else { - let lifetime_defs = self.parse_late_bound_lifetime_defs()?; - let path = self.parse_path(PathStyle::Type)?; - if has_parens { - self.expect(&token::CloseDelim(token::Paren))?; - } - let poly_span = lo.to(self.prev_span); - if is_negative { - was_negative = true; - if let Some(sp) = last_plus_span.or(colon_span) { - negative_bounds.push(sp.to(poly_span)); - } - } else { - let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span); - let modifier = if question.is_some() { - TraitBoundModifier::Maybe - } else { - TraitBoundModifier::None - }; - bounds.push(GenericBound::Trait(poly_trait, modifier)); - } - } - } else { - break - } - - if !allow_plus || !self.eat_plus() { - break - } else { - last_plus_span = Some(self.prev_span); - } - } - - if !negative_bounds.is_empty() || was_negative { - let plural = negative_bounds.len() > 1; - let last_span = negative_bounds.last().map(|sp| *sp); - let mut err = self.struct_span_err( - negative_bounds, - "negative trait bounds are not supported", - ); - if let Some(sp) = last_span { - err.span_label(sp, "negative trait bounds are not supported"); - } - if let Some(bound_list) = colon_span { - let bound_list = bound_list.to(self.prev_span); - let mut new_bound_list = String::new(); - if !bounds.is_empty() { - let mut snippets = bounds.iter().map(|bound| bound.span()) - .map(|span| self.span_to_snippet(span)); - while let Some(Ok(snippet)) = snippets.next() { - new_bound_list.push_str(" + "); - new_bound_list.push_str(&snippet); - } - new_bound_list = new_bound_list.replacen(" +", ":", 1); - } - err.span_suggestion_hidden( - bound_list, - &format!("remove the trait bound{}", if plural { "s" } else { "" }), - new_bound_list, - Applicability::MachineApplicable, - ); - } - err.emit(); - } - - return Ok(bounds); - } - - crate fn parse_generic_bounds(&mut self, - colon_span: Option) -> PResult<'a, GenericBounds> { - self.parse_generic_bounds_common(true, colon_span) - } - /// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`. /// /// ``` @@ -2475,250 +1842,6 @@ impl<'a> Parser<'a> { }) } - /// Parses generic args (within a path segment) with recovery for extra leading angle brackets. - /// For the purposes of understanding the parsing logic of generic arguments, this function - /// can be thought of being the same as just calling `self.parse_generic_args()` if the source - /// had the correct amount of leading angle brackets. - /// - /// ```ignore (diagnostics) - /// bar::<<<::Output>(); - /// ^^ help: remove extra angle brackets - /// ``` - fn parse_generic_args_with_leaning_angle_bracket_recovery( - &mut self, - style: PathStyle, - lo: Span, - ) -> PResult<'a, (Vec, Vec)> { - // We need to detect whether there are extra leading left angle brackets and produce an - // appropriate error and suggestion. This cannot be implemented by looking ahead at - // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens - // then there won't be matching `>` tokens to find. - // - // To explain how this detection works, consider the following example: - // - // ```ignore (diagnostics) - // bar::<<<::Output>(); - // ^^ help: remove extra angle brackets - // ``` - // - // Parsing of the left angle brackets starts in this function. We start by parsing the - // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via - // `eat_lt`): - // - // *Upcoming tokens:* `<<<::Output>;` - // *Unmatched count:* 1 - // *`parse_path_segment` calls deep:* 0 - // - // This has the effect of recursing as this function is called if a `<` character - // is found within the expected generic arguments: - // - // *Upcoming tokens:* `<<::Output>;` - // *Unmatched count:* 2 - // *`parse_path_segment` calls deep:* 1 - // - // Eventually we will have recursed until having consumed all of the `<` tokens and - // this will be reflected in the count: - // - // *Upcoming tokens:* `T as Foo>::Output>;` - // *Unmatched count:* 4 - // `parse_path_segment` calls deep:* 3 - // - // The parser will continue until reaching the first `>` - this will decrement the - // unmatched angle bracket count and return to the parent invocation of this function - // having succeeded in parsing: - // - // *Upcoming tokens:* `::Output>;` - // *Unmatched count:* 3 - // *`parse_path_segment` calls deep:* 2 - // - // This will continue until the next `>` character which will also return successfully - // to the parent invocation of this function and decrement the count: - // - // *Upcoming tokens:* `;` - // *Unmatched count:* 2 - // *`parse_path_segment` calls deep:* 1 - // - // At this point, this function will expect to find another matching `>` character but - // won't be able to and will return an error. This will continue all the way up the - // call stack until the first invocation: - // - // *Upcoming tokens:* `;` - // *Unmatched count:* 2 - // *`parse_path_segment` calls deep:* 0 - // - // In doing this, we have managed to work out how many unmatched leading left angle - // brackets there are, but we cannot recover as the unmatched angle brackets have - // already been consumed. To remedy this, we keep a snapshot of the parser state - // before we do the above. We can then inspect whether we ended up with a parsing error - // and unmatched left angle brackets and if so, restore the parser state before we - // consumed any `<` characters to emit an error and consume the erroneous tokens to - // recover by attempting to parse again. - // - // In practice, the recursion of this function is indirect and there will be other - // locations that consume some `<` characters - as long as we update the count when - // this happens, it isn't an issue. - - let is_first_invocation = style == PathStyle::Expr; - // Take a snapshot before attempting to parse - we can restore this later. - let snapshot = if is_first_invocation { - Some(self.clone()) - } else { - None - }; - - debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)"); - match self.parse_generic_args() { - Ok(value) => Ok(value), - Err(ref mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => { - // Cancel error from being unable to find `>`. We know the error - // must have been this due to a non-zero unmatched angle bracket - // count. - e.cancel(); - - // Swap `self` with our backup of the parser state before attempting to parse - // generic arguments. - let snapshot = mem::replace(self, snapshot.unwrap()); - - debug!( - "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \ - snapshot.count={:?}", - snapshot.unmatched_angle_bracket_count, - ); - - // Eat the unmatched angle brackets. - for _ in 0..snapshot.unmatched_angle_bracket_count { - self.eat_lt(); - } - - // Make a span over ${unmatched angle bracket count} characters. - let span = lo.with_hi( - lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count) - ); - let plural = snapshot.unmatched_angle_bracket_count > 1; - self.diagnostic() - .struct_span_err( - span, - &format!( - "unmatched angle bracket{}", - if plural { "s" } else { "" } - ), - ) - .span_suggestion( - span, - &format!( - "remove extra angle bracket{}", - if plural { "s" } else { "" } - ), - String::new(), - Applicability::MachineApplicable, - ) - .emit(); - - // Try again without unmatched angle bracket characters. - self.parse_generic_args() - }, - Err(e) => Err(e), - } - } - - /// Parses (possibly empty) list of lifetime and type arguments and associated type bindings, - /// possibly including trailing comma. - fn parse_generic_args(&mut self) -> PResult<'a, (Vec, Vec)> { - let mut args = Vec::new(); - let mut constraints = Vec::new(); - let mut misplaced_assoc_ty_constraints: Vec = Vec::new(); - let mut assoc_ty_constraints: Vec = Vec::new(); - - let args_lo = self.token.span; - - loop { - if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { - // Parse lifetime argument. - args.push(GenericArg::Lifetime(self.expect_lifetime())); - misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); - } else if self.check_ident() && self.look_ahead(1, - |t| t == &token::Eq || t == &token::Colon) { - // Parse associated type constraint. - let lo = self.token.span; - let ident = self.parse_ident()?; - let kind = if self.eat(&token::Eq) { - AssocTyConstraintKind::Equality { - ty: self.parse_ty()?, - } - } else if self.eat(&token::Colon) { - AssocTyConstraintKind::Bound { - bounds: self.parse_generic_bounds(Some(self.prev_span))?, - } - } else { - unreachable!(); - }; - let span = lo.to(self.prev_span); - constraints.push(AssocTyConstraint { - id: ast::DUMMY_NODE_ID, - ident, - kind, - span, - }); - assoc_ty_constraints.push(span); - } else if self.check_const_arg() { - // Parse const argument. - let expr = if let token::OpenDelim(token::Brace) = self.token.kind { - self.parse_block_expr( - None, self.token.span, BlockCheckMode::Default, ThinVec::new() - )? - } else if self.token.is_ident() { - // FIXME(const_generics): to distinguish between idents for types and consts, - // we should introduce a GenericArg::Ident in the AST and distinguish when - // lowering to the HIR. For now, idents for const args are not permitted. - if self.token.is_keyword(kw::True) || self.token.is_keyword(kw::False) { - self.parse_literal_maybe_minus()? - } else { - return Err( - self.fatal("identifiers may currently not be used for const generics") - ); - } - } else { - self.parse_literal_maybe_minus()? - }; - let value = AnonConst { - id: ast::DUMMY_NODE_ID, - value: expr, - }; - args.push(GenericArg::Const(value)); - misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); - } else if self.check_type() { - // Parse type argument. - args.push(GenericArg::Type(self.parse_ty()?)); - misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); - } else { - break - } - - if !self.eat(&token::Comma) { - break - } - } - - // FIXME: we would like to report this in ast_validation instead, but we currently do not - // preserve ordering of generic parameters with respect to associated type binding, so we - // lose that information after parsing. - if misplaced_assoc_ty_constraints.len() > 0 { - let mut err = self.struct_span_err( - args_lo.to(self.prev_span), - "associated type bindings must be declared after generic parameters", - ); - for span in misplaced_assoc_ty_constraints { - err.span_label( - span, - "this associated type binding should be moved after the generic parameters", - ); - } - err.emit(); - } - - Ok((args, constraints)) - } - /// Parses an optional where-clause and places it in `generics`. /// /// ```ignore (only-for-syntax-highlight) @@ -3087,19 +2210,6 @@ impl<'a> Parser<'a> { self.is_keyword_ahead(1, &[kw::Const])) } - fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec> { - if self.eat_keyword(kw::For) { - self.expect_lt()?; - let params = self.parse_generic_params()?; - self.expect_gt()?; - // We rely on AST validation to rule out invalid cases: There must not be type - // parameters, and the lifetime parameters must not have bounds. - Ok(params) - } else { - Ok(Vec::new()) - } - } - /// 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)`. /// If the following element can't be a tuple (i.e., it's a function definition), then diff --git a/src/libsyntax/parse/parser/path.rs b/src/libsyntax/parse/parser/path.rs new file mode 100644 index 0000000000000..2760cd38cabc6 --- /dev/null +++ b/src/libsyntax/parse/parser/path.rs @@ -0,0 +1,474 @@ +use super::{Parser, PResult, TokenType, BlockCheckMode}; + +use crate::{maybe_whole, ThinVec}; +use crate::ast::{self, QSelf, Path, PathSegment, Ident, ParenthesizedArgs, AngleBracketedArgs}; +use crate::ast::{AnonConst, GenericArg, AssocTyConstraint, AssocTyConstraintKind}; +use crate::parse::token::{self, Token}; +use crate::source_map::{Span, BytePos}; +use crate::symbol::{kw}; + +use std::mem; +use log::debug; +use errors::{Applicability}; + +/// Specifies how to parse a path. +#[derive(Copy, Clone, PartialEq)] +pub enum PathStyle { + /// In some contexts, notably in expressions, paths with generic arguments are ambiguous + /// with something else. For example, in expressions `segment < ....` can be interpreted + /// as a comparison and `segment ( ....` can be interpreted as a function call. + /// In all such contexts the non-path interpretation is preferred by default for practical + /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g. + /// `x` - comparisons, `x::` - unambiguously a path. + Expr, + /// In other contexts, notably in types, no ambiguity exists and paths can be written + /// without the disambiguator, e.g., `x` - unambiguously a path. + /// Paths with disambiguators are still accepted, `x::` - unambiguously a path too. + Type, + /// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports, + /// visibilities or attributes. + /// Technically, this variant is unnecessary and e.g., `Expr` can be used instead + /// (paths in "mod" contexts have to be checked later for absence of generic arguments + /// anyway, due to macros), but it is used to avoid weird suggestions about expected + /// tokens when something goes wrong. + Mod, +} + +impl<'a> Parser<'a> { + /// Parses a qualified path. + /// Assumes that the leading `<` has been parsed already. + /// + /// `qualified_path = ::path` + /// + /// # Examples + /// `::default` + /// `::a` + /// `::F::a` (without disambiguator) + /// `::F::a::` (with disambiguator) + pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Path)> { + let lo = self.prev_span; + let ty = self.parse_ty()?; + + // `path` will contain the prefix of the path up to the `>`, + // if any (e.g., `U` in the `::*` examples + // above). `path_span` has the span of that path, or an empty + // span in the case of something like `::Bar`. + let (mut path, path_span); + if self.eat_keyword(kw::As) { + let path_lo = self.token.span; + path = self.parse_path(PathStyle::Type)?; + path_span = path_lo.to(self.prev_span); + } else { + path_span = self.token.span.to(self.token.span); + path = ast::Path { segments: Vec::new(), span: path_span }; + } + + // See doc comment for `unmatched_angle_bracket_count`. + self.expect(&token::Gt)?; + if self.unmatched_angle_bracket_count > 0 { + self.unmatched_angle_bracket_count -= 1; + debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count); + } + + self.expect(&token::ModSep)?; + + let qself = QSelf { ty, path_span, position: path.segments.len() }; + self.parse_path_segments(&mut path.segments, style)?; + + Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_span) })) + } + + /// Parses simple paths. + /// + /// `path = [::] segment+` + /// `segment = ident | ident[::] | ident[::](args) [-> type]` + /// + /// # Examples + /// `a::b::C` (without disambiguator) + /// `a::b::C::` (with disambiguator) + /// `Fn(Args)` (without disambiguator) + /// `Fn::(Args)` (with disambiguator) + pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> { + maybe_whole!(self, NtPath, |path| { + if style == PathStyle::Mod && + path.segments.iter().any(|segment| segment.args.is_some()) { + self.diagnostic().span_err(path.span, "unexpected generic arguments in path"); + } + path + }); + + let lo = self.meta_var_span.unwrap_or(self.token.span); + let mut segments = Vec::new(); + let mod_sep_ctxt = self.token.span.ctxt(); + if self.eat(&token::ModSep) { + segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt))); + } + self.parse_path_segments(&mut segments, style)?; + + Ok(Path { segments, span: lo.to(self.prev_span) }) + } + + /// Like `parse_path`, but also supports parsing `Word` meta items into paths for + /// backwards-compatibility. This is used when parsing derive macro paths in `#[derive]` + /// attributes. + pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> { + let meta_ident = match self.token.kind { + token::Interpolated(ref nt) => match **nt { + token::NtMeta(ref meta) => match meta.node { + ast::MetaItemKind::Word => Some(meta.path.clone()), + _ => None, + }, + _ => None, + }, + _ => None, + }; + if let Some(path) = meta_ident { + self.bump(); + return Ok(path); + } + self.parse_path(style) + } + + crate fn parse_path_segments(&mut self, + segments: &mut Vec, + style: PathStyle) + -> PResult<'a, ()> { + loop { + let segment = self.parse_path_segment(style)?; + if style == PathStyle::Expr { + // In order to check for trailing angle brackets, we must have finished + // recursing (`parse_path_segment` can indirectly call this function), + // that is, the next token must be the highlighted part of the below example: + // + // `Foo::>::Qux` + // ^ here + // + // As opposed to the below highlight (if we had only finished the first + // recursion): + // + // `Foo::>::Qux` + // ^ here + // + // `PathStyle::Expr` is only provided at the root invocation and never in + // `parse_path_segment` to recurse and therefore can be checked to maintain + // this invariant. + self.check_trailing_angle_brackets(&segment, token::ModSep); + } + segments.push(segment); + + if self.is_import_coupler() || !self.eat(&token::ModSep) { + return Ok(()); + } + } + } + + pub(super) fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> { + let ident = self.parse_path_segment_ident()?; + + let is_args_start = |token: &Token| match token.kind { + token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren) + | token::LArrow => true, + _ => false, + }; + let check_args_start = |this: &mut Self| { + this.expected_tokens.extend_from_slice( + &[TokenType::Token(token::Lt), TokenType::Token(token::OpenDelim(token::Paren))] + ); + is_args_start(&this.token) + }; + + Ok(if style == PathStyle::Type && check_args_start(self) || + style != PathStyle::Mod && self.check(&token::ModSep) + && self.look_ahead(1, |t| is_args_start(t)) { + // We use `style == PathStyle::Expr` to check if this is in a recursion or not. If + // it isn't, then we reset the unmatched angle bracket count as we're about to start + // parsing a new path. + if style == PathStyle::Expr { + self.unmatched_angle_bracket_count = 0; + self.max_angle_bracket_count = 0; + } + + // Generic arguments are found - `<`, `(`, `::<` or `::(`. + self.eat(&token::ModSep); + let lo = self.token.span; + let args = if self.eat_lt() { + // `<'a, T, A = U>` + let (args, constraints) = + self.parse_generic_args_with_leaning_angle_bracket_recovery(style, lo)?; + self.expect_gt()?; + let span = lo.to(self.prev_span); + AngleBracketedArgs { args, constraints, span }.into() + } else { + // `(T, U) -> R` + let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?; + let span = lo.to(self.prev_span); + let output = if self.eat(&token::RArrow) { + Some(self.parse_ty_common(false, false, false)?) + } else { + None + }; + ParenthesizedArgs { inputs, output, span }.into() + }; + + PathSegment { ident, args, id: ast::DUMMY_NODE_ID } + } else { + // Generic arguments are not found. + PathSegment::from_ident(ident) + }) + } + + pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> { + match self.token.kind { + token::Ident(name, _) if name.is_path_segment_keyword() => { + let span = self.token.span; + self.bump(); + Ok(Ident::new(name, span)) + } + _ => self.parse_ident(), + } + } + + /// Parses generic args (within a path segment) with recovery for extra leading angle brackets. + /// For the purposes of understanding the parsing logic of generic arguments, this function + /// can be thought of being the same as just calling `self.parse_generic_args()` if the source + /// had the correct amount of leading angle brackets. + /// + /// ```ignore (diagnostics) + /// bar::<<<::Output>(); + /// ^^ help: remove extra angle brackets + /// ``` + fn parse_generic_args_with_leaning_angle_bracket_recovery( + &mut self, + style: PathStyle, + lo: Span, + ) -> PResult<'a, (Vec, Vec)> { + // We need to detect whether there are extra leading left angle brackets and produce an + // appropriate error and suggestion. This cannot be implemented by looking ahead at + // upcoming tokens for a matching `>` character - if there are unmatched `<` tokens + // then there won't be matching `>` tokens to find. + // + // To explain how this detection works, consider the following example: + // + // ```ignore (diagnostics) + // bar::<<<::Output>(); + // ^^ help: remove extra angle brackets + // ``` + // + // Parsing of the left angle brackets starts in this function. We start by parsing the + // `<` token (incrementing the counter of unmatched angle brackets on `Parser` via + // `eat_lt`): + // + // *Upcoming tokens:* `<<<::Output>;` + // *Unmatched count:* 1 + // *`parse_path_segment` calls deep:* 0 + // + // This has the effect of recursing as this function is called if a `<` character + // is found within the expected generic arguments: + // + // *Upcoming tokens:* `<<::Output>;` + // *Unmatched count:* 2 + // *`parse_path_segment` calls deep:* 1 + // + // Eventually we will have recursed until having consumed all of the `<` tokens and + // this will be reflected in the count: + // + // *Upcoming tokens:* `T as Foo>::Output>;` + // *Unmatched count:* 4 + // `parse_path_segment` calls deep:* 3 + // + // The parser will continue until reaching the first `>` - this will decrement the + // unmatched angle bracket count and return to the parent invocation of this function + // having succeeded in parsing: + // + // *Upcoming tokens:* `::Output>;` + // *Unmatched count:* 3 + // *`parse_path_segment` calls deep:* 2 + // + // This will continue until the next `>` character which will also return successfully + // to the parent invocation of this function and decrement the count: + // + // *Upcoming tokens:* `;` + // *Unmatched count:* 2 + // *`parse_path_segment` calls deep:* 1 + // + // At this point, this function will expect to find another matching `>` character but + // won't be able to and will return an error. This will continue all the way up the + // call stack until the first invocation: + // + // *Upcoming tokens:* `;` + // *Unmatched count:* 2 + // *`parse_path_segment` calls deep:* 0 + // + // In doing this, we have managed to work out how many unmatched leading left angle + // brackets there are, but we cannot recover as the unmatched angle brackets have + // already been consumed. To remedy this, we keep a snapshot of the parser state + // before we do the above. We can then inspect whether we ended up with a parsing error + // and unmatched left angle brackets and if so, restore the parser state before we + // consumed any `<` characters to emit an error and consume the erroneous tokens to + // recover by attempting to parse again. + // + // In practice, the recursion of this function is indirect and there will be other + // locations that consume some `<` characters - as long as we update the count when + // this happens, it isn't an issue. + + let is_first_invocation = style == PathStyle::Expr; + // Take a snapshot before attempting to parse - we can restore this later. + let snapshot = if is_first_invocation { + Some(self.clone()) + } else { + None + }; + + debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)"); + match self.parse_generic_args() { + Ok(value) => Ok(value), + Err(ref mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => { + // Cancel error from being unable to find `>`. We know the error + // must have been this due to a non-zero unmatched angle bracket + // count. + e.cancel(); + + // Swap `self` with our backup of the parser state before attempting to parse + // generic arguments. + let snapshot = mem::replace(self, snapshot.unwrap()); + + debug!( + "parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \ + snapshot.count={:?}", + snapshot.unmatched_angle_bracket_count, + ); + + // Eat the unmatched angle brackets. + for _ in 0..snapshot.unmatched_angle_bracket_count { + self.eat_lt(); + } + + // Make a span over ${unmatched angle bracket count} characters. + let span = lo.with_hi( + lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count) + ); + let plural = snapshot.unmatched_angle_bracket_count > 1; + self.diagnostic() + .struct_span_err( + span, + &format!( + "unmatched angle bracket{}", + if plural { "s" } else { "" } + ), + ) + .span_suggestion( + span, + &format!( + "remove extra angle bracket{}", + if plural { "s" } else { "" } + ), + String::new(), + Applicability::MachineApplicable, + ) + .emit(); + + // Try again without unmatched angle bracket characters. + self.parse_generic_args() + }, + Err(e) => Err(e), + } + } + + /// Parses (possibly empty) list of lifetime and type arguments and associated type bindings, + /// possibly including trailing comma. + fn parse_generic_args(&mut self) -> PResult<'a, (Vec, Vec)> { + let mut args = Vec::new(); + let mut constraints = Vec::new(); + let mut misplaced_assoc_ty_constraints: Vec = Vec::new(); + let mut assoc_ty_constraints: Vec = Vec::new(); + + let args_lo = self.token.span; + + loop { + if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { + // Parse lifetime argument. + args.push(GenericArg::Lifetime(self.expect_lifetime())); + misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); + } else if self.check_ident() && self.look_ahead(1, + |t| t == &token::Eq || t == &token::Colon) { + // Parse associated type constraint. + let lo = self.token.span; + let ident = self.parse_ident()?; + let kind = if self.eat(&token::Eq) { + AssocTyConstraintKind::Equality { + ty: self.parse_ty()?, + } + } else if self.eat(&token::Colon) { + AssocTyConstraintKind::Bound { + bounds: self.parse_generic_bounds(Some(self.prev_span))?, + } + } else { + unreachable!(); + }; + let span = lo.to(self.prev_span); + constraints.push(AssocTyConstraint { + id: ast::DUMMY_NODE_ID, + ident, + kind, + span, + }); + assoc_ty_constraints.push(span); + } else if self.check_const_arg() { + // Parse const argument. + let expr = if let token::OpenDelim(token::Brace) = self.token.kind { + self.parse_block_expr( + None, self.token.span, BlockCheckMode::Default, ThinVec::new() + )? + } else if self.token.is_ident() { + // FIXME(const_generics): to distinguish between idents for types and consts, + // we should introduce a GenericArg::Ident in the AST and distinguish when + // lowering to the HIR. For now, idents for const args are not permitted. + if self.token.is_keyword(kw::True) || self.token.is_keyword(kw::False) { + self.parse_literal_maybe_minus()? + } else { + return Err( + self.fatal("identifiers may currently not be used for const generics") + ); + } + } else { + self.parse_literal_maybe_minus()? + }; + let value = AnonConst { + id: ast::DUMMY_NODE_ID, + value: expr, + }; + args.push(GenericArg::Const(value)); + misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); + } else if self.check_type() { + // Parse type argument. + args.push(GenericArg::Type(self.parse_ty()?)); + misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints); + } else { + break + } + + if !self.eat(&token::Comma) { + break + } + } + + // FIXME: we would like to report this in ast_validation instead, but we currently do not + // preserve ordering of generic parameters with respect to associated type binding, so we + // lose that information after parsing. + if misplaced_assoc_ty_constraints.len() > 0 { + let mut err = self.struct_span_err( + args_lo.to(self.prev_span), + "associated type bindings must be declared after generic parameters", + ); + for span in misplaced_assoc_ty_constraints { + err.span_label( + span, + "this associated type binding should be moved after the generic parameters", + ); + } + err.emit(); + } + + Ok((args, constraints)) + } +} diff --git a/src/libsyntax/parse/parser/ty.rs b/src/libsyntax/parse/parser/ty.rs new file mode 100644 index 0000000000000..a5a073c3af1a9 --- /dev/null +++ b/src/libsyntax/parse/parser/ty.rs @@ -0,0 +1,447 @@ +use super::{Parser, PResult, PathStyle, PrevTokenKind}; + +use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath}; +use crate::ptr::P; +use crate::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam}; +use crate::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef}; +use crate::ast::{Mutability, AnonConst, FnDecl, Mac_}; +use crate::parse::token::{self, Token}; +use crate::source_map::{respan, Span}; +use crate::symbol::{kw}; + +use rustc_target::spec::abi::Abi; + +use errors::{Applicability}; + +/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT`, +/// `IDENT<::AssocTy>`. +/// +/// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes +/// that `IDENT` is not the ident of a fn trait. +fn can_continue_type_after_non_fn_ident(t: &Token) -> bool { + t == &token::ModSep || t == &token::Lt || + t == &token::BinOp(token::Shl) +} + +impl<'a> Parser<'a> { + /// Parses a type. + pub fn parse_ty(&mut self) -> PResult<'a, P> { + self.parse_ty_common(true, true, false) + } + + /// Parses a type in restricted contexts where `+` is not permitted. + /// + /// Example 1: `&'a TYPE` + /// `+` is prohibited to maintain operator priority (P(+) < P(&)). + /// Example 2: `value1 as TYPE + value2` + /// `+` is prohibited to avoid interactions with expression grammar. + pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P> { + self.parse_ty_common(false, true, false) + } + + /// Parses an optional return type `[ -> TY ]` in a function declaration. + pub(super) fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> { + if self.eat(&token::RArrow) { + Ok(FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true, false)?)) + } else { + Ok(FunctionRetTy::Default(self.token.span.shrink_to_lo())) + } + } + + pub(super) fn parse_ty_common(&mut self, allow_plus: bool, allow_qpath_recovery: bool, + allow_c_variadic: bool) -> PResult<'a, P> { + maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery); + maybe_whole!(self, NtTy, |x| x); + + let lo = self.token.span; + let mut impl_dyn_multi = false; + let node = if self.eat(&token::OpenDelim(token::Paren)) { + // `(TYPE)` is a parenthesized type. + // `(TYPE,)` is a tuple with a single field of type TYPE. + let mut ts = vec![]; + let mut last_comma = false; + while self.token != token::CloseDelim(token::Paren) { + ts.push(self.parse_ty()?); + if self.eat(&token::Comma) { + last_comma = true; + } else { + last_comma = false; + break; + } + } + let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus; + self.expect(&token::CloseDelim(token::Paren))?; + + if ts.len() == 1 && !last_comma { + let ty = ts.into_iter().nth(0).unwrap().into_inner(); + let maybe_bounds = allow_plus && self.token.is_like_plus(); + match ty.node { + // `(TY_BOUND_NOPAREN) + BOUND + ...`. + TyKind::Path(None, ref path) if maybe_bounds => { + self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)? + } + TyKind::TraitObject(ref bounds, TraitObjectSyntax::None) + if maybe_bounds && bounds.len() == 1 && !trailing_plus => { + let path = match bounds[0] { + GenericBound::Trait(ref pt, ..) => pt.trait_ref.path.clone(), + GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"), + }; + self.parse_remaining_bounds(Vec::new(), path, lo, true)? + } + // `(TYPE)` + _ => TyKind::Paren(P(ty)) + } + } else { + TyKind::Tup(ts) + } + } else if self.eat(&token::Not) { + // Never type `!` + TyKind::Never + } else if self.eat(&token::BinOp(token::Star)) { + // Raw pointer + TyKind::Ptr(self.parse_ptr()?) + } else if self.eat(&token::OpenDelim(token::Bracket)) { + // Array or slice + let t = self.parse_ty()?; + // Parse optional `; EXPR` in `[TYPE; EXPR]` + let t = match self.maybe_parse_fixed_length_of_vec()? { + None => TyKind::Slice(t), + Some(length) => TyKind::Array(t, AnonConst { + id: ast::DUMMY_NODE_ID, + value: length, + }), + }; + self.expect(&token::CloseDelim(token::Bracket))?; + t + } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) { + // Reference + self.expect_and()?; + self.parse_borrowed_pointee()? + } else if self.eat_keyword_noexpect(kw::Typeof) { + // `typeof(EXPR)` + // In order to not be ambiguous, the type must be surrounded by parens. + self.expect(&token::OpenDelim(token::Paren))?; + let e = AnonConst { + id: ast::DUMMY_NODE_ID, + value: self.parse_expr()?, + }; + self.expect(&token::CloseDelim(token::Paren))?; + TyKind::Typeof(e) + } else if self.eat_keyword(kw::Underscore) { + // A type to be inferred `_` + TyKind::Infer + } else if self.token_is_bare_fn_keyword() { + // Function pointer type + self.parse_ty_bare_fn(Vec::new())? + } else if self.check_keyword(kw::For) { + // Function pointer type or bound list (trait object type) starting with a poly-trait. + // `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T` + // `for<'lt> Trait1<'lt> + Trait2 + 'a` + let lo = self.token.span; + let lifetime_defs = self.parse_late_bound_lifetime_defs()?; + if self.token_is_bare_fn_keyword() { + self.parse_ty_bare_fn(lifetime_defs)? + } else { + let path = self.parse_path(PathStyle::Type)?; + let parse_plus = allow_plus && self.check_plus(); + self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)? + } + } else if self.eat_keyword(kw::Impl) { + // Always parse bounds greedily for better error recovery. + let bounds = self.parse_generic_bounds(None)?; + impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; + TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds) + } else if self.check_keyword(kw::Dyn) && + (self.token.span.rust_2018() || + self.look_ahead(1, |t| t.can_begin_bound() && + !can_continue_type_after_non_fn_ident(t))) { + self.bump(); // `dyn` + // Always parse bounds greedily for better error recovery. + let bounds = self.parse_generic_bounds(None)?; + impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; + TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn) + } else if self.check(&token::Question) || + self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus()) { + // Bound list (trait object type) + TyKind::TraitObject(self.parse_generic_bounds_common(allow_plus, None)?, + TraitObjectSyntax::None) + } else if self.eat_lt() { + // Qualified path + let (qself, path) = self.parse_qpath(PathStyle::Type)?; + TyKind::Path(Some(qself), path) + } else if self.token.is_path_start() { + // Simple path + let path = self.parse_path(PathStyle::Type)?; + if self.eat(&token::Not) { + // Macro invocation in type position + let (delim, tts) = self.expect_delimited_token_tree()?; + let node = Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }; + TyKind::Mac(respan(lo.to(self.prev_span), node)) + } else { + // Just a type path or bound list (trait object type) starting with a trait. + // `Type` + // `Trait1 + Trait2 + 'a` + if allow_plus && self.check_plus() { + self.parse_remaining_bounds(Vec::new(), path, lo, true)? + } else { + TyKind::Path(None, path) + } + } + } else if self.check(&token::DotDotDot) { + if allow_c_variadic { + self.eat(&token::DotDotDot); + TyKind::CVarArgs + } else { + return Err(self.fatal( + "only foreign functions are allowed to be C-variadic" + )); + } + } else { + let msg = format!("expected type, found {}", self.this_token_descr()); + let mut err = self.fatal(&msg); + err.span_label(self.token.span, "expected type"); + self.maybe_annotate_with_ascription(&mut err, true); + return Err(err); + }; + + let span = lo.to(self.prev_span); + let ty = P(Ty { node, span, id: ast::DUMMY_NODE_ID }); + + // Try to recover from use of `+` with incorrect priority. + self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty); + self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?; + self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery) + } + + fn parse_remaining_bounds(&mut self, generic_params: Vec, path: ast::Path, + lo: Span, parse_plus: bool) -> PResult<'a, TyKind> { + let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span)); + let mut bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)]; + if parse_plus { + self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded + bounds.append(&mut self.parse_generic_bounds(Some(self.prev_span))?); + } + Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None)) + } + + fn parse_ptr(&mut self) -> PResult<'a, MutTy> { + let mutbl = if self.eat_keyword(kw::Mut) { + Mutability::Mutable + } else if self.eat_keyword(kw::Const) { + Mutability::Immutable + } else { + let span = self.prev_span; + let msg = "expected mut or const in raw pointer type"; + self.struct_span_err(span, msg) + .span_label(span, msg) + .help("use `*mut T` or `*const T` as appropriate") + .emit(); + Mutability::Immutable + }; + let t = self.parse_ty_no_plus()?; + Ok(MutTy { ty: t, mutbl }) + } + + fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option>> { + if self.eat(&token::Semi) { + Ok(Some(self.parse_expr()?)) + } else { + Ok(None) + } + } + + fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> { + let opt_lifetime = if self.check_lifetime() { Some(self.expect_lifetime()) } else { None }; + let mutbl = self.parse_mutability(); + let ty = self.parse_ty_no_plus()?; + return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl })); + } + + /// Is the current token one of the keywords that signals a bare function type? + fn token_is_bare_fn_keyword(&mut self) -> bool { + self.check_keyword(kw::Fn) || + self.check_keyword(kw::Unsafe) || + self.check_keyword(kw::Extern) + } + + /// Parses a `TyKind::BareFn` type. + fn parse_ty_bare_fn(&mut self, generic_params: Vec) -> PResult<'a, TyKind> { + /* + + [unsafe] [extern "ABI"] fn (S) -> T + ^~~~^ ^~~~^ ^~^ ^ + | | | | + | | | Return type + | | Argument types + | | + | ABI + Function Style + */ + + let unsafety = self.parse_unsafety(); + let abi = if self.eat_keyword(kw::Extern) { + self.parse_opt_abi()?.unwrap_or(Abi::C) + } else { + Abi::Rust + }; + + self.expect_keyword(kw::Fn)?; + let (inputs, c_variadic) = self.parse_fn_args(false, true)?; + let ret_ty = self.parse_ret_ty(false)?; + let decl = P(FnDecl { + inputs, + output: ret_ty, + c_variadic, + }); + Ok(TyKind::BareFn(P(BareFnTy { + abi, + unsafety, + generic_params, + decl, + }))) + } + + crate fn parse_generic_bounds(&mut self, + colon_span: Option) -> PResult<'a, GenericBounds> { + self.parse_generic_bounds_common(true, colon_span) + } + + /// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`. + /// + /// ``` + /// BOUND = TY_BOUND | LT_BOUND + /// LT_BOUND = LIFETIME (e.g., `'a`) + /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN) + /// TY_BOUND_NOPAREN = [?] [for] SIMPLE_PATH (e.g., `?for<'a: 'b> m::Trait<'a>`) + /// ``` + fn parse_generic_bounds_common(&mut self, + allow_plus: bool, + colon_span: Option) -> PResult<'a, GenericBounds> { + let mut bounds = Vec::new(); + let mut negative_bounds = Vec::new(); + let mut last_plus_span = None; + let mut was_negative = false; + loop { + // This needs to be synchronized with `TokenKind::can_begin_bound`. + let is_bound_start = self.check_path() || self.check_lifetime() || + self.check(&token::Not) || // used for error reporting only + self.check(&token::Question) || + self.check_keyword(kw::For) || + self.check(&token::OpenDelim(token::Paren)); + if is_bound_start { + let lo = self.token.span; + let has_parens = self.eat(&token::OpenDelim(token::Paren)); + let inner_lo = self.token.span; + let is_negative = self.eat(&token::Not); + let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None }; + if self.token.is_lifetime() { + if let Some(question_span) = question { + self.span_err(question_span, + "`?` may only modify trait bounds, not lifetime bounds"); + } + bounds.push(GenericBound::Outlives(self.expect_lifetime())); + if has_parens { + let inner_span = inner_lo.to(self.prev_span); + self.expect(&token::CloseDelim(token::Paren))?; + let mut err = self.struct_span_err( + lo.to(self.prev_span), + "parenthesized lifetime bounds are not supported" + ); + if let Ok(snippet) = self.span_to_snippet(inner_span) { + err.span_suggestion_short( + lo.to(self.prev_span), + "remove the parentheses", + snippet.to_owned(), + Applicability::MachineApplicable + ); + } + err.emit(); + } + } else { + let lifetime_defs = self.parse_late_bound_lifetime_defs()?; + let path = self.parse_path(PathStyle::Type)?; + if has_parens { + self.expect(&token::CloseDelim(token::Paren))?; + } + let poly_span = lo.to(self.prev_span); + if is_negative { + was_negative = true; + if let Some(sp) = last_plus_span.or(colon_span) { + negative_bounds.push(sp.to(poly_span)); + } + } else { + let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span); + let modifier = if question.is_some() { + TraitBoundModifier::Maybe + } else { + TraitBoundModifier::None + }; + bounds.push(GenericBound::Trait(poly_trait, modifier)); + } + } + } else { + break + } + + if !allow_plus || !self.eat_plus() { + break + } else { + last_plus_span = Some(self.prev_span); + } + } + + if !negative_bounds.is_empty() || was_negative { + let plural = negative_bounds.len() > 1; + let last_span = negative_bounds.last().map(|sp| *sp); + let mut err = self.struct_span_err( + negative_bounds, + "negative trait bounds are not supported", + ); + if let Some(sp) = last_span { + err.span_label(sp, "negative trait bounds are not supported"); + } + if let Some(bound_list) = colon_span { + let bound_list = bound_list.to(self.prev_span); + let mut new_bound_list = String::new(); + if !bounds.is_empty() { + let mut snippets = bounds.iter().map(|bound| bound.span()) + .map(|span| self.span_to_snippet(span)); + while let Some(Ok(snippet)) = snippets.next() { + new_bound_list.push_str(" + "); + new_bound_list.push_str(&snippet); + } + new_bound_list = new_bound_list.replacen(" +", ":", 1); + } + err.span_suggestion_hidden( + bound_list, + &format!("remove the trait bound{}", if plural { "s" } else { "" }), + new_bound_list, + Applicability::MachineApplicable, + ); + } + err.emit(); + } + + return Ok(bounds); + } + + // TODO remove super below. + + pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec> { + if self.eat_keyword(kw::For) { + self.expect_lt()?; + let params = self.parse_generic_params()?; + self.expect_gt()?; + // We rely on AST validation to rule out invalid cases: There must not be type + // parameters, and the lifetime parameters must not have bounds. + Ok(params) + } else { + Ok(Vec::new()) + } + } +} From 848ec4aa3c416a4b489fc2080f1a46e5e019a06f Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 20:00:38 +0200 Subject: [PATCH 140/148] parser: move parse_ident_or_underscore into item.rs --- src/libsyntax/parse/parser.rs | 11 ----------- src/libsyntax/parse/parser/item.rs | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 490abc8edd0b7..7ba3e7c423608 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1079,17 +1079,6 @@ impl<'a> Parser<'a> { }) } - fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> { - match self.token.kind { - token::Ident(name, false) if name == kw::Underscore => { - let span = self.token.span; - self.bump(); - Ok(Ident::new(name, span)) - } - _ => self.parse_ident(), - } - } - crate fn check_lifetime(&mut self) -> bool { self.expected_tokens.push(TokenType::Lifetime); self.token.is_lifetime() diff --git a/src/libsyntax/parse/parser/item.rs b/src/libsyntax/parse/parser/item.rs index 5e264a0ca7753..c5608a160cf60 100644 --- a/src/libsyntax/parse/parser/item.rs +++ b/src/libsyntax/parse/parser/item.rs @@ -1182,6 +1182,17 @@ impl<'a> Parser<'a> { } } + fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> { + match self.token.kind { + token::Ident(name, false) if name == kw::Underscore => { + let span = self.token.span; + self.bump(); + Ok(Ident::new(name, span)) + } + _ => self.parse_ident(), + } + } + /// Parses `extern crate` links. /// /// # Examples From 28db7c5968ea15ad2c26d9267ab722994ae371c0 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 20:04:09 +0200 Subject: [PATCH 141/148] parser: move parse_fn_block_decl into expr.rs --- src/libsyntax/parse/parser.rs | 52 +----------------------- src/libsyntax/parse/parser/expr.rs | 64 +++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7ba3e7c423608..e2a41a808686e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -19,7 +19,7 @@ use crate::ast::{Mutability}; use crate::ast::StrStyle; use crate::ast::SelfKind; use crate::ast::{GenericParam, GenericParamKind, WhereClause}; -use crate::ast::{Ty, TyKind, GenericBounds}; +use crate::ast::{TyKind, GenericBounds}; use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; use crate::ext::base::DummyResult; use crate::ext::hygiene::SyntaxContext; @@ -1055,30 +1055,6 @@ impl<'a> Parser<'a> { Ok(Arg { attrs: attrs.into(), id: ast::DUMMY_NODE_ID, pat, span, ty }) } - /// Parses an argument in a lambda header (e.g., `|arg, arg|`). - fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> { - let lo = self.token.span; - let attrs = self.parse_arg_attributes()?; - let pat = self.parse_pat(Some("argument name"))?; - let t = if self.eat(&token::Colon) { - self.parse_ty()? - } else { - P(Ty { - id: ast::DUMMY_NODE_ID, - node: TyKind::Infer, - span: self.prev_span, - }) - }; - let span = lo.to(self.token.span); - Ok(Arg { - attrs: attrs.into(), - ty: t, - pat, - span, - id: ast::DUMMY_NODE_ID - }) - } - crate fn check_lifetime(&mut self) -> bool { self.expected_tokens.push(TokenType::Lifetime); self.token.is_lifetime() @@ -2148,32 +2124,6 @@ impl<'a> Parser<'a> { })) } - /// Parses the `|arg, arg|` header of a closure. - fn parse_fn_block_decl(&mut self) -> PResult<'a, P> { - let inputs_captures = { - if self.eat(&token::OrOr) { - Vec::new() - } else { - self.expect(&token::BinOp(token::Or))?; - let args = self.parse_seq_to_before_tokens( - &[&token::BinOp(token::Or), &token::OrOr], - SeqSep::trailing_allowed(token::Comma), - TokenExpectType::NoExpect, - |p| p.parse_fn_block_arg() - )?.0; - self.expect_or()?; - args - } - }; - let output = self.parse_ret_ty(true)?; - - Ok(P(FnDecl { - inputs: inputs_captures, - output, - c_variadic: false - })) - } - fn choose_generics_over_qpath(&self) -> bool { // There's an ambiguity between generic parameters and qualified paths in impls. // If we see `<` it may start both, so we have to inspect some following tokens. diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index 88e534394fe16..19d5bcd3ee9e2 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -1,15 +1,13 @@ use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle}; use super::{BlockCheckMode, BlockMode, SemiColonMode}; -use super::SeqSep; - -use crate::{maybe_recover_from_interpolated_ty_qpath}; +use super::{SeqSep, TokenExpectType}; +use crate::maybe_recover_from_interpolated_ty_qpath; use crate::ptr::P; -use crate::ast; -use crate::ast::{Attribute, AttrStyle}; +use crate::ast::{self, Attribute, AttrStyle}; use crate::ast::{Ident, CaptureBy}; use crate::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm}; -use crate::ast::{Ty, TyKind, FunctionRetTy}; +use crate::ast::{Ty, TyKind, FunctionRetTy, Arg, FnDecl}; use crate::ast::{BinOpKind, BinOp, UnOp}; use crate::ast::{Mac_, AnonConst, Field}; @@ -22,9 +20,7 @@ use crate::symbol::{kw, sym}; use crate::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par}; use std::mem; - -use errors::{Applicability}; - +use errors::Applicability; use rustc_data_structures::thin_vec::ThinVec; /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression @@ -1142,6 +1138,56 @@ impl<'a> Parser<'a> { } } + /// Parses the `|arg, arg|` header of a closure. + fn parse_fn_block_decl(&mut self) -> PResult<'a, P> { + let inputs_captures = { + if self.eat(&token::OrOr) { + Vec::new() + } else { + self.expect(&token::BinOp(token::Or))?; + let args = self.parse_seq_to_before_tokens( + &[&token::BinOp(token::Or), &token::OrOr], + SeqSep::trailing_allowed(token::Comma), + TokenExpectType::NoExpect, + |p| p.parse_fn_block_arg() + )?.0; + self.expect_or()?; + args + } + }; + let output = self.parse_ret_ty(true)?; + + Ok(P(FnDecl { + inputs: inputs_captures, + output, + c_variadic: false + })) + } + + /// Parses an argument in a lambda header (e.g., `|arg, arg|`). + fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> { + let lo = self.token.span; + let attrs = self.parse_arg_attributes()?; + let pat = self.parse_pat(Some("argument name"))?; + let t = if self.eat(&token::Colon) { + self.parse_ty()? + } else { + P(Ty { + id: ast::DUMMY_NODE_ID, + node: TyKind::Infer, + span: self.prev_span, + }) + }; + let span = lo.to(self.token.span); + Ok(Arg { + attrs: attrs.into(), + ty: t, + pat, + span, + id: ast::DUMMY_NODE_ID + }) + } + /// Parses an `if` expression (`if` token already eaten). fn parse_if_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.prev_span; From d6d93b3d829116dbcd0c27d746a5ffd46f703ede Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 20:32:29 +0200 Subject: [PATCH 142/148] parser: move into stmt.rs --- src/libsyntax/parse/parser.rs | 467 +---------------------------- src/libsyntax/parse/parser/expr.rs | 5 +- src/libsyntax/parse/parser/item.rs | 5 + src/libsyntax/parse/parser/path.rs | 6 +- src/libsyntax/parse/parser/stmt.rs | 458 ++++++++++++++++++++++++++++ 5 files changed, 477 insertions(+), 464 deletions(-) create mode 100644 src/libsyntax/parse/parser/stmt.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e2a41a808686e..182941406ab8b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1,5 +1,4 @@ mod expr; -use expr::LhsExpr; mod pat; mod item; pub use item::AliasKind; @@ -8,23 +7,15 @@ pub use module::{ModulePath, ModulePathSuccess}; mod ty; mod path; pub use path::PathStyle; +mod stmt; -use crate::ast::{self, AttrStyle}; -use crate::ast::{Arg, Attribute, BindingMode}; -use crate::ast::{Block, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind}; -use crate::ast::{FnDecl}; -use crate::ast::{Ident, IsAsync, Local, Lifetime}; -use crate::ast::{MacStmtStyle, Mac_, MacDelimiter}; -use crate::ast::{Mutability}; -use crate::ast::StrStyle; -use crate::ast::SelfKind; -use crate::ast::{GenericParam, GenericParamKind, WhereClause}; -use crate::ast::{TyKind, GenericBounds}; +use crate::ast::{self, AttrStyle, Attribute, Arg, BindingMode, StrStyle, SelfKind}; +use crate::ast::{FnDecl, Ident, IsAsync, Lifetime, MacDelimiter, Mutability}; +use crate::ast::{GenericParam, GenericParamKind, WhereClause, TyKind, GenericBounds}; use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; -use crate::ext::base::DummyResult; use crate::ext::hygiene::SyntaxContext; use crate::source_map::{self, respan}; -use crate::parse::{SeqSep, classify, literal, token}; +use crate::parse::{SeqSep, literal, token}; use crate::parse::lexer::UnmatchedBrace; use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use crate::parse::token::{Token, TokenKind, DelimToken}; @@ -1214,450 +1205,6 @@ impl<'a> Parser<'a> { } - /// Parses the RHS of a local variable declaration (e.g., '= 14;'). - fn parse_initializer(&mut self, skip_eq: bool) -> PResult<'a, Option>> { - if self.eat(&token::Eq) { - Ok(Some(self.parse_expr()?)) - } else if skip_eq { - Ok(Some(self.parse_expr()?)) - } else { - Ok(None) - } - } - - /// Parses a local variable declaration. - fn parse_local(&mut self, attrs: ThinVec) -> PResult<'a, P> { - let lo = self.prev_span; - let pat = self.parse_top_level_pat()?; - - let (err, ty) = if self.eat(&token::Colon) { - // Save the state of the parser before parsing type normally, in case there is a `:` - // instead of an `=` typo. - let parser_snapshot_before_type = self.clone(); - let colon_sp = self.prev_span; - match self.parse_ty() { - Ok(ty) => (None, Some(ty)), - Err(mut err) => { - // Rewind to before attempting to parse the type and continue parsing - let parser_snapshot_after_type = self.clone(); - mem::replace(self, parser_snapshot_before_type); - - let snippet = self.span_to_snippet(pat.span).unwrap(); - err.span_label(pat.span, format!("while parsing the type for `{}`", snippet)); - (Some((parser_snapshot_after_type, colon_sp, err)), None) - } - } - } else { - (None, None) - }; - let init = match (self.parse_initializer(err.is_some()), err) { - (Ok(init), None) => { // init parsed, ty parsed - init - } - (Ok(init), Some((_, colon_sp, mut err))) => { // init parsed, ty error - // Could parse the type as if it were the initializer, it is likely there was a - // typo in the code: `:` instead of `=`. Add suggestion and emit the error. - err.span_suggestion_short( - colon_sp, - "use `=` if you meant to assign", - "=".to_string(), - Applicability::MachineApplicable - ); - err.emit(); - // As this was parsed successfully, continue as if the code has been fixed for the - // rest of the file. It will still fail due to the emitted error, but we avoid - // extra noise. - init - } - (Err(mut init_err), Some((snapshot, _, ty_err))) => { // init error, ty error - init_err.cancel(); - // Couldn't parse the type nor the initializer, only raise the type error and - // return to the parser state before parsing the type as the initializer. - // let x: ; - mem::replace(self, snapshot); - return Err(ty_err); - } - (Err(err), None) => { // init error, ty parsed - // Couldn't parse the initializer and we're not attempting to recover a failed - // parse of the type, return the error. - return Err(err); - } - }; - let hi = if self.token == token::Semi { - self.token.span - } else { - self.prev_span - }; - Ok(P(ast::Local { - ty, - pat, - init, - id: ast::DUMMY_NODE_ID, - span: lo.to(hi), - attrs, - })) - } - - /// Parse a statement. This stops just before trailing semicolons on everything but items. - /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. - pub fn parse_stmt(&mut self) -> PResult<'a, Option> { - Ok(self.parse_stmt_(true)) - } - - fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option { - self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| { - e.emit(); - self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); - None - }) - } - - fn is_async_fn(&self) -> bool { - self.token.is_keyword(kw::Async) && - self.is_keyword_ahead(1, &[kw::Fn]) - } - - fn is_crate_vis(&self) -> bool { - self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep) - } - - fn is_auto_trait_item(&self) -> bool { - // auto trait - (self.token.is_keyword(kw::Auto) && - self.is_keyword_ahead(1, &[kw::Trait])) - || // unsafe auto trait - (self.token.is_keyword(kw::Unsafe) && - self.is_keyword_ahead(1, &[kw::Auto]) && - self.is_keyword_ahead(2, &[kw::Trait])) - } - - fn parse_stmt_without_recovery( - &mut self, - macro_legacy_warnings: bool, - ) -> PResult<'a, Option> { - maybe_whole!(self, NtStmt, |x| Some(x)); - - let attrs = self.parse_outer_attributes()?; - let lo = self.token.span; - - Ok(Some(if self.eat_keyword(kw::Let) { - Stmt { - id: ast::DUMMY_NODE_ID, - node: StmtKind::Local(self.parse_local(attrs.into())?), - span: lo.to(self.prev_span), - } - } else if let Some(macro_def) = self.eat_macro_def( - &attrs, - &source_map::respan(lo, VisibilityKind::Inherited), - lo, - )? { - Stmt { - id: ast::DUMMY_NODE_ID, - node: StmtKind::Item(macro_def), - span: lo.to(self.prev_span), - } - // Starts like a simple path, being careful to avoid contextual keywords - // such as a union items, item with `crate` visibility or auto trait items. - // Our goal here is to parse an arbitrary path `a::b::c` but not something that starts - // like a path (1 token), but it fact not a path. - // `union::b::c` - path, `union U { ... }` - not a path. - // `crate::b::c` - path, `crate struct S;` - not a path. - } else if self.token.is_path_start() && - !self.token.is_qpath_start() && - !self.is_union_item() && - !self.is_crate_vis() && - !self.is_auto_trait_item() && - !self.is_async_fn() { - let path = self.parse_path(PathStyle::Expr)?; - - if !self.eat(&token::Not) { - let expr = if self.check(&token::OpenDelim(token::Brace)) { - self.parse_struct_expr(lo, path, ThinVec::new())? - } else { - let hi = self.prev_span; - self.mk_expr(lo.to(hi), ExprKind::Path(None, path), ThinVec::new()) - }; - - let expr = self.with_res(Restrictions::STMT_EXPR, |this| { - let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?; - this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr)) - })?; - - return Ok(Some(Stmt { - id: ast::DUMMY_NODE_ID, - node: StmtKind::Expr(expr), - span: lo.to(self.prev_span), - })); - } - - let (delim, tts) = self.expect_delimited_token_tree()?; - let hi = self.prev_span; - - let style = if delim == MacDelimiter::Brace { - MacStmtStyle::Braces - } else { - MacStmtStyle::NoBraces - }; - - let mac = respan(lo.to(hi), Mac_ { - path, - tts, - delim, - prior_type_ascription: self.last_type_ascription, - }); - let node = if delim == MacDelimiter::Brace || - self.token == token::Semi || self.token == token::Eof { - StmtKind::Mac(P((mac, style, attrs.into()))) - } - // We used to incorrectly stop parsing macro-expanded statements here. - // If the next token will be an error anyway but could have parsed with the - // earlier behavior, stop parsing here and emit a warning to avoid breakage. - else if macro_legacy_warnings && - self.token.can_begin_expr() && - match self.token.kind { - // These can continue an expression, so we can't stop parsing and warn. - token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) | - token::BinOp(token::Minus) | token::BinOp(token::Star) | - token::BinOp(token::And) | token::BinOp(token::Or) | - token::AndAnd | token::OrOr | - token::DotDot | token::DotDotDot | token::DotDotEq => false, - _ => true, - } { - self.warn_missing_semicolon(); - StmtKind::Mac(P((mac, style, attrs.into()))) - } else { - let e = self.mk_expr(mac.span, ExprKind::Mac(mac), ThinVec::new()); - let e = self.maybe_recover_from_bad_qpath(e, true)?; - let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; - let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; - StmtKind::Expr(e) - }; - Stmt { - id: ast::DUMMY_NODE_ID, - span: lo.to(hi), - node, - } - } else { - // FIXME: Bad copy of attrs - let old_directory_ownership = - mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock); - let item = self.parse_item_(attrs.clone(), false, true)?; - self.directory.ownership = old_directory_ownership; - - match item { - Some(i) => Stmt { - id: ast::DUMMY_NODE_ID, - span: lo.to(i.span), - node: StmtKind::Item(i), - }, - None => { - let unused_attrs = |attrs: &[Attribute], s: &mut Self| { - if !attrs.is_empty() { - if s.prev_token_kind == PrevTokenKind::DocComment { - s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit(); - } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { - s.span_err( - s.token.span, "expected statement after outer attribute" - ); - } - } - }; - - // Do not attempt to parse an expression if we're done here. - if self.token == token::Semi { - unused_attrs(&attrs, self); - self.bump(); - return Ok(None); - } - - if self.token == token::CloseDelim(token::Brace) { - unused_attrs(&attrs, self); - return Ok(None); - } - - // Remainder are line-expr stmts. - let e = self.parse_expr_res( - Restrictions::STMT_EXPR, Some(attrs.into()))?; - Stmt { - id: ast::DUMMY_NODE_ID, - span: lo.to(e.span), - node: StmtKind::Expr(e), - } - } - } - })) - } - - /// Parses a block. No inner attributes are allowed. - pub fn parse_block(&mut self) -> PResult<'a, P> { - maybe_whole!(self, NtBlock, |x| x); - - let lo = self.token.span; - - if !self.eat(&token::OpenDelim(token::Brace)) { - let sp = self.token.span; - let tok = self.this_token_descr(); - let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok)); - let do_not_suggest_help = - self.token.is_keyword(kw::In) || self.token == token::Colon; - - if self.token.is_ident_named(sym::and) { - e.span_suggestion_short( - self.token.span, - "use `&&` instead of `and` for the boolean operator", - "&&".to_string(), - Applicability::MaybeIncorrect, - ); - } - if self.token.is_ident_named(sym::or) { - e.span_suggestion_short( - self.token.span, - "use `||` instead of `or` for the boolean operator", - "||".to_string(), - Applicability::MaybeIncorrect, - ); - } - - // Check to see if the user has written something like - // - // if (cond) - // bar; - // - // Which is valid in other languages, but not Rust. - match self.parse_stmt_without_recovery(false) { - Ok(Some(stmt)) => { - if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) - || do_not_suggest_help { - // if the next token is an open brace (e.g., `if a b {`), the place- - // inside-a-block suggestion would be more likely wrong than right - e.span_label(sp, "expected `{`"); - return Err(e); - } - let mut stmt_span = stmt.span; - // expand the span to include the semicolon, if it exists - if self.eat(&token::Semi) { - stmt_span = stmt_span.with_hi(self.prev_span.hi()); - } - if let Ok(snippet) = self.span_to_snippet(stmt_span) { - e.span_suggestion( - stmt_span, - "try placing this code inside a block", - format!("{{ {} }}", snippet), - // speculative, has been misleading in the past (#46836) - Applicability::MaybeIncorrect, - ); - } - } - Err(mut e) => { - self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); - self.cancel(&mut e); - } - _ => () - } - e.span_label(sp, "expected `{`"); - return Err(e); - } - - self.parse_block_tail(lo, BlockCheckMode::Default) - } - - /// Parses a block. Inner attributes are allowed. - crate fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec, P)> { - maybe_whole!(self, NtBlock, |x| (Vec::new(), x)); - - let lo = self.token.span; - self.expect(&token::OpenDelim(token::Brace))?; - Ok((self.parse_inner_attributes()?, - self.parse_block_tail(lo, BlockCheckMode::Default)?)) - } - - /// Parses the rest of a block expression or function body. - /// Precondition: already parsed the '{'. - fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P> { - let mut stmts = vec![]; - while !self.eat(&token::CloseDelim(token::Brace)) { - if self.token == token::Eof { - break; - } - let stmt = match self.parse_full_stmt(false) { - Err(mut err) => { - err.emit(); - self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); - Some(Stmt { - id: ast::DUMMY_NODE_ID, - node: StmtKind::Expr(DummyResult::raw_expr(self.token.span, true)), - span: self.token.span, - }) - } - Ok(stmt) => stmt, - }; - if let Some(stmt) = stmt { - stmts.push(stmt); - } else { - // Found only `;` or `}`. - continue; - }; - } - Ok(P(ast::Block { - stmts, - id: ast::DUMMY_NODE_ID, - rules: s, - span: lo.to(self.prev_span), - })) - } - - /// Parses a statement, including the trailing semicolon. - crate fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option> { - // skip looking for a trailing semicolon when we have an interpolated statement - maybe_whole!(self, NtStmt, |x| Some(x)); - - let mut stmt = match self.parse_stmt_without_recovery(macro_legacy_warnings)? { - Some(stmt) => stmt, - None => return Ok(None), - }; - - match stmt.node { - StmtKind::Expr(ref expr) if self.token != token::Eof => { - // expression without semicolon - if classify::expr_requires_semi_to_be_stmt(expr) { - // Just check for errors and recover; do not eat semicolon yet. - if let Err(mut e) = - self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)]) - { - e.emit(); - self.recover_stmt(); - // Don't complain about type errors in body tail after parse error (#57383). - let sp = expr.span.to(self.prev_span); - stmt.node = StmtKind::Expr(DummyResult::raw_expr(sp, true)); - } - } - } - StmtKind::Local(..) => { - // We used to incorrectly allow a macro-expanded let statement to lack a semicolon. - if macro_legacy_warnings && self.token != token::Semi { - self.warn_missing_semicolon(); - } else { - self.expect_one_of(&[], &[token::Semi])?; - } - } - _ => {} - } - - if self.eat(&token::Semi) { - stmt = stmt.add_trailing_semicolon(); - } - stmt.span = stmt.span.to(self.prev_span); - Ok(Some(stmt)) - } - - fn warn_missing_semicolon(&self) { - self.diagnostic().struct_span_warn(self.token.span, { - &format!("expected `;`, found {}", self.this_token_descr()) - }).note({ - "This was erroneously allowed and will become a hard error in a future release" - }).emit(); - } - /// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`. /// /// ``` @@ -2149,6 +1696,10 @@ impl<'a> Parser<'a> { self.is_keyword_ahead(1, &[kw::Const])) } + 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)`. /// If the following element can't be a tuple (i.e., it's a function definition), then diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index 19d5bcd3ee9e2..4432c1329cbfe 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -1,11 +1,10 @@ use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle}; -use super::{BlockCheckMode, BlockMode, SemiColonMode}; +use super::{BlockMode, SemiColonMode}; use super::{SeqSep, TokenExpectType}; use crate::maybe_recover_from_interpolated_ty_qpath; use crate::ptr::P; -use crate::ast::{self, Attribute, AttrStyle}; -use crate::ast::{Ident, CaptureBy}; +use crate::ast::{self, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode}; use crate::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm}; use crate::ast::{Ty, TyKind, FunctionRetTy, Arg, FnDecl}; use crate::ast::{BinOpKind, BinOp, UnOp}; diff --git a/src/libsyntax/parse/parser/item.rs b/src/libsyntax/parse/parser/item.rs index c5608a160cf60..e85ef9cc97419 100644 --- a/src/libsyntax/parse/parser/item.rs +++ b/src/libsyntax/parse/parser/item.rs @@ -569,6 +569,11 @@ impl<'a> Parser<'a> { Err(err) } + pub(super) fn is_async_fn(&self) -> bool { + self.token.is_keyword(kw::Async) && + self.is_keyword_ahead(1, &[kw::Fn]) + } + /// Parses a macro invocation inside a `trait`, `impl` or `extern` block. fn parse_assoc_macro_invoc(&mut self, item_kind: &str, vis: Option<&Visibility>, at_end: &mut bool) -> PResult<'a, Option> diff --git a/src/libsyntax/parse/parser/path.rs b/src/libsyntax/parse/parser/path.rs index 2760cd38cabc6..3eb4d45045a9e 100644 --- a/src/libsyntax/parse/parser/path.rs +++ b/src/libsyntax/parse/parser/path.rs @@ -1,11 +1,11 @@ -use super::{Parser, PResult, TokenType, BlockCheckMode}; +use super::{Parser, PResult, TokenType}; use crate::{maybe_whole, ThinVec}; use crate::ast::{self, QSelf, Path, PathSegment, Ident, ParenthesizedArgs, AngleBracketedArgs}; -use crate::ast::{AnonConst, GenericArg, AssocTyConstraint, AssocTyConstraintKind}; +use crate::ast::{AnonConst, GenericArg, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode}; use crate::parse::token::{self, Token}; use crate::source_map::{Span, BytePos}; -use crate::symbol::{kw}; +use crate::symbol::kw; use std::mem; use log::debug; diff --git a/src/libsyntax/parse/parser/stmt.rs b/src/libsyntax/parse/parser/stmt.rs new file mode 100644 index 0000000000000..f182edcbff437 --- /dev/null +++ b/src/libsyntax/parse/parser/stmt.rs @@ -0,0 +1,458 @@ +use super::{Parser, PResult, Restrictions, PrevTokenKind, SemiColonMode, BlockMode}; +use super::expr::LhsExpr; +use super::path::PathStyle; + +use crate::ptr::P; +use crate::{maybe_whole, ThinVec}; +use crate::ast::{self, Stmt, StmtKind, Local, Block, BlockCheckMode, Expr, ExprKind}; +use crate::ast::{Attribute, AttrStyle, VisibilityKind, MacStmtStyle, Mac_, MacDelimiter}; +use crate::ext::base::DummyResult; +use crate::parse::{classify, DirectoryOwnership}; +use crate::parse::diagnostics::Error; +use crate::parse::token::{self}; +use crate::source_map::{respan, Span}; +use crate::symbol::{kw, sym}; + +use std::mem; +use errors::Applicability; + +impl<'a> Parser<'a> { + /// Parse a statement. This stops just before trailing semicolons on everything but items. + /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. + pub fn parse_stmt(&mut self) -> PResult<'a, Option> { + Ok(self.parse_stmt_(true)) + } + + fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option { + self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| { + e.emit(); + self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); + None + }) + } + + fn parse_stmt_without_recovery( + &mut self, + macro_legacy_warnings: bool, + ) -> PResult<'a, Option> { + maybe_whole!(self, NtStmt, |x| Some(x)); + + let attrs = self.parse_outer_attributes()?; + let lo = self.token.span; + + Ok(Some(if self.eat_keyword(kw::Let) { + Stmt { + id: ast::DUMMY_NODE_ID, + node: StmtKind::Local(self.parse_local(attrs.into())?), + span: lo.to(self.prev_span), + } + } else if let Some(macro_def) = self.eat_macro_def( + &attrs, + &respan(lo, VisibilityKind::Inherited), + lo, + )? { + Stmt { + id: ast::DUMMY_NODE_ID, + node: StmtKind::Item(macro_def), + span: lo.to(self.prev_span), + } + // Starts like a simple path, being careful to avoid contextual keywords + // such as a union items, item with `crate` visibility or auto trait items. + // Our goal here is to parse an arbitrary path `a::b::c` but not something that starts + // like a path (1 token), but it fact not a path. + // `union::b::c` - path, `union U { ... }` - not a path. + // `crate::b::c` - path, `crate struct S;` - not a path. + } else if self.token.is_path_start() && + !self.token.is_qpath_start() && + !self.is_union_item() && + !self.is_crate_vis() && + !self.is_auto_trait_item() && + !self.is_async_fn() { + let path = self.parse_path(PathStyle::Expr)?; + + if !self.eat(&token::Not) { + let expr = if self.check(&token::OpenDelim(token::Brace)) { + self.parse_struct_expr(lo, path, ThinVec::new())? + } else { + let hi = self.prev_span; + self.mk_expr(lo.to(hi), ExprKind::Path(None, path), ThinVec::new()) + }; + + let expr = self.with_res(Restrictions::STMT_EXPR, |this| { + let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?; + this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr)) + })?; + + return Ok(Some(Stmt { + id: ast::DUMMY_NODE_ID, + node: StmtKind::Expr(expr), + span: lo.to(self.prev_span), + })); + } + + let (delim, tts) = self.expect_delimited_token_tree()?; + let hi = self.prev_span; + + let style = if delim == MacDelimiter::Brace { + MacStmtStyle::Braces + } else { + MacStmtStyle::NoBraces + }; + + let mac = respan(lo.to(hi), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }); + let node = if delim == MacDelimiter::Brace || + self.token == token::Semi || self.token == token::Eof { + StmtKind::Mac(P((mac, style, attrs.into()))) + } + // We used to incorrectly stop parsing macro-expanded statements here. + // If the next token will be an error anyway but could have parsed with the + // earlier behavior, stop parsing here and emit a warning to avoid breakage. + else if macro_legacy_warnings && + self.token.can_begin_expr() && + match self.token.kind { + // These can continue an expression, so we can't stop parsing and warn. + token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) | + token::BinOp(token::Minus) | token::BinOp(token::Star) | + token::BinOp(token::And) | token::BinOp(token::Or) | + token::AndAnd | token::OrOr | + token::DotDot | token::DotDotDot | token::DotDotEq => false, + _ => true, + } { + self.warn_missing_semicolon(); + StmtKind::Mac(P((mac, style, attrs.into()))) + } else { + let e = self.mk_expr(mac.span, ExprKind::Mac(mac), ThinVec::new()); + let e = self.maybe_recover_from_bad_qpath(e, true)?; + let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; + let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; + StmtKind::Expr(e) + }; + Stmt { + id: ast::DUMMY_NODE_ID, + span: lo.to(hi), + node, + } + } else { + // FIXME: Bad copy of attrs + let old_directory_ownership = + mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock); + let item = self.parse_item_(attrs.clone(), false, true)?; + self.directory.ownership = old_directory_ownership; + + match item { + Some(i) => Stmt { + id: ast::DUMMY_NODE_ID, + span: lo.to(i.span), + node: StmtKind::Item(i), + }, + None => { + let unused_attrs = |attrs: &[Attribute], s: &mut Self| { + if !attrs.is_empty() { + if s.prev_token_kind == PrevTokenKind::DocComment { + s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit(); + } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { + s.span_err( + s.token.span, "expected statement after outer attribute" + ); + } + } + }; + + // Do not attempt to parse an expression if we're done here. + if self.token == token::Semi { + unused_attrs(&attrs, self); + self.bump(); + return Ok(None); + } + + if self.token == token::CloseDelim(token::Brace) { + unused_attrs(&attrs, self); + return Ok(None); + } + + // Remainder are line-expr stmts. + let e = self.parse_expr_res( + Restrictions::STMT_EXPR, Some(attrs.into()))?; + Stmt { + id: ast::DUMMY_NODE_ID, + span: lo.to(e.span), + node: StmtKind::Expr(e), + } + } + } + })) + } + + /// Parses a local variable declaration. + fn parse_local(&mut self, attrs: ThinVec) -> PResult<'a, P> { + let lo = self.prev_span; + let pat = self.parse_top_level_pat()?; + + let (err, ty) = if self.eat(&token::Colon) { + // Save the state of the parser before parsing type normally, in case there is a `:` + // instead of an `=` typo. + let parser_snapshot_before_type = self.clone(); + let colon_sp = self.prev_span; + match self.parse_ty() { + Ok(ty) => (None, Some(ty)), + Err(mut err) => { + // Rewind to before attempting to parse the type and continue parsing + let parser_snapshot_after_type = self.clone(); + mem::replace(self, parser_snapshot_before_type); + + let snippet = self.span_to_snippet(pat.span).unwrap(); + err.span_label(pat.span, format!("while parsing the type for `{}`", snippet)); + (Some((parser_snapshot_after_type, colon_sp, err)), None) + } + } + } else { + (None, None) + }; + let init = match (self.parse_initializer(err.is_some()), err) { + (Ok(init), None) => { // init parsed, ty parsed + init + } + (Ok(init), Some((_, colon_sp, mut err))) => { // init parsed, ty error + // Could parse the type as if it were the initializer, it is likely there was a + // typo in the code: `:` instead of `=`. Add suggestion and emit the error. + err.span_suggestion_short( + colon_sp, + "use `=` if you meant to assign", + "=".to_string(), + Applicability::MachineApplicable + ); + err.emit(); + // As this was parsed successfully, continue as if the code has been fixed for the + // rest of the file. It will still fail due to the emitted error, but we avoid + // extra noise. + init + } + (Err(mut init_err), Some((snapshot, _, ty_err))) => { // init error, ty error + init_err.cancel(); + // Couldn't parse the type nor the initializer, only raise the type error and + // return to the parser state before parsing the type as the initializer. + // let x: ; + mem::replace(self, snapshot); + return Err(ty_err); + } + (Err(err), None) => { // init error, ty parsed + // Couldn't parse the initializer and we're not attempting to recover a failed + // parse of the type, return the error. + return Err(err); + } + }; + let hi = if self.token == token::Semi { + self.token.span + } else { + self.prev_span + }; + Ok(P(ast::Local { + ty, + pat, + init, + id: ast::DUMMY_NODE_ID, + span: lo.to(hi), + attrs, + })) + } + + /// Parses the RHS of a local variable declaration (e.g., '= 14;'). + fn parse_initializer(&mut self, skip_eq: bool) -> PResult<'a, Option>> { + if self.eat(&token::Eq) { + Ok(Some(self.parse_expr()?)) + } else if skip_eq { + Ok(Some(self.parse_expr()?)) + } else { + Ok(None) + } + } + + fn is_auto_trait_item(&self) -> bool { + // auto trait + (self.token.is_keyword(kw::Auto) && + self.is_keyword_ahead(1, &[kw::Trait])) + || // unsafe auto trait + (self.token.is_keyword(kw::Unsafe) && + self.is_keyword_ahead(1, &[kw::Auto]) && + self.is_keyword_ahead(2, &[kw::Trait])) + } + + /// Parses a block. No inner attributes are allowed. + pub fn parse_block(&mut self) -> PResult<'a, P> { + maybe_whole!(self, NtBlock, |x| x); + + let lo = self.token.span; + + if !self.eat(&token::OpenDelim(token::Brace)) { + let sp = self.token.span; + let tok = self.this_token_descr(); + let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok)); + let do_not_suggest_help = + self.token.is_keyword(kw::In) || self.token == token::Colon; + + if self.token.is_ident_named(sym::and) { + e.span_suggestion_short( + self.token.span, + "use `&&` instead of `and` for the boolean operator", + "&&".to_string(), + Applicability::MaybeIncorrect, + ); + } + if self.token.is_ident_named(sym::or) { + e.span_suggestion_short( + self.token.span, + "use `||` instead of `or` for the boolean operator", + "||".to_string(), + Applicability::MaybeIncorrect, + ); + } + + // Check to see if the user has written something like + // + // if (cond) + // bar; + // + // Which is valid in other languages, but not Rust. + match self.parse_stmt_without_recovery(false) { + Ok(Some(stmt)) => { + if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) + || do_not_suggest_help { + // if the next token is an open brace (e.g., `if a b {`), the place- + // inside-a-block suggestion would be more likely wrong than right + e.span_label(sp, "expected `{`"); + return Err(e); + } + let mut stmt_span = stmt.span; + // expand the span to include the semicolon, if it exists + if self.eat(&token::Semi) { + stmt_span = stmt_span.with_hi(self.prev_span.hi()); + } + if let Ok(snippet) = self.span_to_snippet(stmt_span) { + e.span_suggestion( + stmt_span, + "try placing this code inside a block", + format!("{{ {} }}", snippet), + // speculative, has been misleading in the past (#46836) + Applicability::MaybeIncorrect, + ); + } + } + Err(mut e) => { + self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); + self.cancel(&mut e); + } + _ => () + } + e.span_label(sp, "expected `{`"); + return Err(e); + } + + self.parse_block_tail(lo, BlockCheckMode::Default) + } + + /// Parses a block. Inner attributes are allowed. + crate fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec, P)> { + maybe_whole!(self, NtBlock, |x| (Vec::new(), x)); + + let lo = self.token.span; + self.expect(&token::OpenDelim(token::Brace))?; + Ok((self.parse_inner_attributes()?, + self.parse_block_tail(lo, BlockCheckMode::Default)?)) + } + + /// Parses the rest of a block expression or function body. + /// Precondition: already parsed the '{'. + pub(super) fn parse_block_tail( + &mut self, + lo: Span, + s: BlockCheckMode + ) -> PResult<'a, P> { + let mut stmts = vec![]; + while !self.eat(&token::CloseDelim(token::Brace)) { + if self.token == token::Eof { + break; + } + let stmt = match self.parse_full_stmt(false) { + Err(mut err) => { + err.emit(); + self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore); + Some(Stmt { + id: ast::DUMMY_NODE_ID, + node: StmtKind::Expr(DummyResult::raw_expr(self.token.span, true)), + span: self.token.span, + }) + } + Ok(stmt) => stmt, + }; + if let Some(stmt) = stmt { + stmts.push(stmt); + } else { + // Found only `;` or `}`. + continue; + }; + } + Ok(P(ast::Block { + stmts, + id: ast::DUMMY_NODE_ID, + rules: s, + span: lo.to(self.prev_span), + })) + } + + /// Parses a statement, including the trailing semicolon. + crate fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option> { + // skip looking for a trailing semicolon when we have an interpolated statement + maybe_whole!(self, NtStmt, |x| Some(x)); + + let mut stmt = match self.parse_stmt_without_recovery(macro_legacy_warnings)? { + Some(stmt) => stmt, + None => return Ok(None), + }; + + match stmt.node { + StmtKind::Expr(ref expr) if self.token != token::Eof => { + // expression without semicolon + if classify::expr_requires_semi_to_be_stmt(expr) { + // Just check for errors and recover; do not eat semicolon yet. + if let Err(mut e) = + self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)]) + { + e.emit(); + self.recover_stmt(); + // Don't complain about type errors in body tail after parse error (#57383). + let sp = expr.span.to(self.prev_span); + stmt.node = StmtKind::Expr(DummyResult::raw_expr(sp, true)); + } + } + } + StmtKind::Local(..) => { + // We used to incorrectly allow a macro-expanded let statement to lack a semicolon. + if macro_legacy_warnings && self.token != token::Semi { + self.warn_missing_semicolon(); + } else { + self.expect_one_of(&[], &[token::Semi])?; + } + } + _ => {} + } + + if self.eat(&token::Semi) { + stmt = stmt.add_trailing_semicolon(); + } + stmt.span = stmt.span.to(self.prev_span); + Ok(Some(stmt)) + } + + fn warn_missing_semicolon(&self) { + self.diagnostic().struct_span_warn(self.token.span, { + &format!("expected `;`, found {}", self.this_token_descr()) + }).note({ + "This was erroneously allowed and will become a hard error in a future release" + }).emit(); + } +} From 385d07f359cd4d4237cac6ae030d07792750a506 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 20:44:09 +0200 Subject: [PATCH 143/148] parser: move into generics.rs --- src/libsyntax/parse/parser.rs | 272 +----------------------- src/libsyntax/parse/parser/generics.rs | 276 +++++++++++++++++++++++++ src/libsyntax/parse/parser/ty.rs | 2 - 3 files changed, 278 insertions(+), 272 deletions(-) create mode 100644 src/libsyntax/parse/parser/generics.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 182941406ab8b..afa475a4c8bf0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -8,10 +8,10 @@ mod ty; mod path; pub use path::PathStyle; mod stmt; +mod generics; use crate::ast::{self, AttrStyle, Attribute, Arg, BindingMode, StrStyle, SelfKind}; -use crate::ast::{FnDecl, Ident, IsAsync, Lifetime, MacDelimiter, Mutability}; -use crate::ast::{GenericParam, GenericParamKind, WhereClause, TyKind, GenericBounds}; +use crate::ast::{FnDecl, Ident, IsAsync, Lifetime, MacDelimiter, Mutability, TyKind}; use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; use crate::ext::hygiene::SyntaxContext; use crate::source_map::{self, respan}; @@ -1205,249 +1205,6 @@ impl<'a> Parser<'a> { } - /// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`. - /// - /// ``` - /// BOUND = LT_BOUND (e.g., `'a`) - /// ``` - fn parse_lt_param_bounds(&mut self) -> GenericBounds { - let mut lifetimes = Vec::new(); - while self.check_lifetime() { - lifetimes.push(ast::GenericBound::Outlives(self.expect_lifetime())); - - if !self.eat_plus() { - break - } - } - lifetimes - } - - /// Matches `typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?`. - fn parse_ty_param(&mut self, - preceding_attrs: Vec) - -> PResult<'a, GenericParam> { - let ident = self.parse_ident()?; - - // Parse optional colon and param bounds. - let bounds = if self.eat(&token::Colon) { - self.parse_generic_bounds(Some(self.prev_span))? - } else { - Vec::new() - }; - - let default = if self.eat(&token::Eq) { - Some(self.parse_ty()?) - } else { - None - }; - - Ok(GenericParam { - ident, - id: ast::DUMMY_NODE_ID, - attrs: preceding_attrs.into(), - bounds, - kind: GenericParamKind::Type { - default, - } - }) - } - - fn parse_const_param(&mut self, preceding_attrs: Vec) -> PResult<'a, GenericParam> { - self.expect_keyword(kw::Const)?; - let ident = self.parse_ident()?; - self.expect(&token::Colon)?; - let ty = self.parse_ty()?; - - Ok(GenericParam { - ident, - id: ast::DUMMY_NODE_ID, - attrs: preceding_attrs.into(), - bounds: Vec::new(), - kind: GenericParamKind::Const { - ty, - } - }) - } - - /// Parses a (possibly empty) list of lifetime and type parameters, possibly including - /// a trailing comma and erroneous trailing attributes. - crate fn parse_generic_params(&mut self) -> PResult<'a, Vec> { - let mut params = Vec::new(); - loop { - let attrs = self.parse_outer_attributes()?; - if self.check_lifetime() { - let lifetime = self.expect_lifetime(); - // Parse lifetime parameter. - let bounds = if self.eat(&token::Colon) { - self.parse_lt_param_bounds() - } else { - Vec::new() - }; - params.push(ast::GenericParam { - ident: lifetime.ident, - id: lifetime.id, - attrs: attrs.into(), - bounds, - kind: ast::GenericParamKind::Lifetime, - }); - } else if self.check_keyword(kw::Const) { - // Parse const parameter. - params.push(self.parse_const_param(attrs)?); - } else if self.check_ident() { - // Parse type parameter. - params.push(self.parse_ty_param(attrs)?); - } else { - // Check for trailing attributes and stop parsing. - if !attrs.is_empty() { - if !params.is_empty() { - self.struct_span_err( - attrs[0].span, - &format!("trailing attribute after generic parameter"), - ) - .span_label(attrs[0].span, "attributes must go before parameters") - .emit(); - } else { - self.struct_span_err( - attrs[0].span, - &format!("attribute without generic parameters"), - ) - .span_label( - attrs[0].span, - "attributes are only permitted when preceding parameters", - ) - .emit(); - } - } - break - } - - if !self.eat(&token::Comma) { - break - } - } - Ok(params) - } - - /// Parses a set of optional generic type parameter declarations. Where - /// clauses are not parsed here, and must be added later via - /// `parse_where_clause()`. - /// - /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > ) - /// | ( < lifetimes , typaramseq ( , )? > ) - /// where typaramseq = ( typaram ) | ( typaram , typaramseq ) - fn parse_generics(&mut self) -> PResult<'a, ast::Generics> { - let span_lo = self.token.span; - let (params, span) = if self.eat_lt() { - let params = self.parse_generic_params()?; - self.expect_gt()?; - (params, span_lo.to(self.prev_span)) - } else { - (vec![], self.prev_span.between(self.token.span)) - }; - Ok(ast::Generics { - params, - where_clause: WhereClause { - predicates: Vec::new(), - span: DUMMY_SP, - }, - span, - }) - } - - /// Parses an optional where-clause and places it in `generics`. - /// - /// ```ignore (only-for-syntax-highlight) - /// where T : Trait + 'b, 'a : 'b - /// ``` - fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> { - let mut where_clause = WhereClause { - predicates: Vec::new(), - span: self.prev_span.to(self.prev_span), - }; - - if !self.eat_keyword(kw::Where) { - return Ok(where_clause); - } - let lo = self.prev_span; - - // We are considering adding generics to the `where` keyword as an alternative higher-rank - // parameter syntax (as in `where<'a>` or `where`. To avoid that being a breaking - // change we parse those generics now, but report an error. - if self.choose_generics_over_qpath() { - let generics = self.parse_generics()?; - self.struct_span_err( - generics.span, - "generic parameters on `where` clauses are reserved for future use", - ) - .span_label(generics.span, "currently unsupported") - .emit(); - } - - loop { - let lo = self.token.span; - if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { - let lifetime = self.expect_lifetime(); - // Bounds starting with a colon are mandatory, but possibly empty. - self.expect(&token::Colon)?; - let bounds = self.parse_lt_param_bounds(); - where_clause.predicates.push(ast::WherePredicate::RegionPredicate( - ast::WhereRegionPredicate { - span: lo.to(self.prev_span), - lifetime, - bounds, - } - )); - } else if self.check_type() { - // Parse optional `for<'a, 'b>`. - // This `for` is parsed greedily and applies to the whole predicate, - // the bounded type can have its own `for` applying only to it. - // Examples: - // * `for<'a> Trait1<'a>: Trait2<'a /* ok */>` - // * `(for<'a> Trait1<'a>): Trait2<'a /* not ok */>` - // * `for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /* ok */, 'b /* not ok */>` - let lifetime_defs = self.parse_late_bound_lifetime_defs()?; - - // Parse type with mandatory colon and (possibly empty) bounds, - // or with mandatory equality sign and the second type. - let ty = self.parse_ty()?; - if self.eat(&token::Colon) { - let bounds = self.parse_generic_bounds(Some(self.prev_span))?; - where_clause.predicates.push(ast::WherePredicate::BoundPredicate( - ast::WhereBoundPredicate { - span: lo.to(self.prev_span), - bound_generic_params: lifetime_defs, - bounded_ty: ty, - bounds, - } - )); - // FIXME: Decide what should be used here, `=` or `==`. - // FIXME: We are just dropping the binders in lifetime_defs on the floor here. - } else if self.eat(&token::Eq) || self.eat(&token::EqEq) { - let rhs_ty = self.parse_ty()?; - where_clause.predicates.push(ast::WherePredicate::EqPredicate( - ast::WhereEqPredicate { - span: lo.to(self.prev_span), - lhs_ty: ty, - rhs_ty, - id: ast::DUMMY_NODE_ID, - } - )); - } else { - return self.unexpected(); - } - } else { - break - } - - if !self.eat(&token::Comma) { - break - } - } - - where_clause.span = lo.to(self.prev_span); - Ok(where_clause) - } - fn parse_fn_args(&mut self, named_args: bool, allow_c_variadic: bool) -> PResult<'a, (Vec , bool)> { let sp = self.token.span; @@ -1671,31 +1428,6 @@ impl<'a> Parser<'a> { })) } - fn choose_generics_over_qpath(&self) -> bool { - // There's an ambiguity between generic parameters and qualified paths in impls. - // If we see `<` it may start both, so we have to inspect some following tokens. - // The following combinations can only start generics, - // but not qualified paths (with one exception): - // `<` `>` - empty generic parameters - // `<` `#` - generic parameters with attributes - // `<` (LIFETIME|IDENT) `>` - single generic parameter - // `<` (LIFETIME|IDENT) `,` - first generic parameter in a list - // `<` (LIFETIME|IDENT) `:` - generic parameter with bounds - // `<` (LIFETIME|IDENT) `=` - generic parameter with a default - // `<` const - generic const parameter - // The only truly ambiguous case is - // `<` IDENT `>` `::` IDENT ... - // we disambiguate it in favor of generics (`impl ::absolute::Path { ... }`) - // because this is what almost always expected in practice, qualified paths in impls - // (`impl ::AssocTy { ... }`) aren't even allowed by type checker at the moment. - self.token == token::Lt && - (self.look_ahead(1, |t| t == &token::Pound || t == &token::Gt) || - self.look_ahead(1, |t| t.is_lifetime() || t.is_ident()) && - self.look_ahead(2, |t| t == &token::Gt || t == &token::Comma || - t == &token::Colon || t == &token::Eq) || - self.is_keyword_ahead(1, &[kw::Const])) - } - fn is_crate_vis(&self) -> bool { self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep) } diff --git a/src/libsyntax/parse/parser/generics.rs b/src/libsyntax/parse/parser/generics.rs new file mode 100644 index 0000000000000..54f24f8ef2b21 --- /dev/null +++ b/src/libsyntax/parse/parser/generics.rs @@ -0,0 +1,276 @@ +use super::{Parser, PResult}; + +use crate::ast::{self, WhereClause, GenericParam, GenericParamKind, GenericBounds, Attribute}; +use crate::parse::token; +use crate::source_map::DUMMY_SP; +use crate::symbol::kw; + +impl<'a> Parser<'a> { + /// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`. + /// + /// ``` + /// BOUND = LT_BOUND (e.g., `'a`) + /// ``` + fn parse_lt_param_bounds(&mut self) -> GenericBounds { + let mut lifetimes = Vec::new(); + while self.check_lifetime() { + lifetimes.push(ast::GenericBound::Outlives(self.expect_lifetime())); + + if !self.eat_plus() { + break + } + } + lifetimes + } + + /// Matches `typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?`. + fn parse_ty_param(&mut self, + preceding_attrs: Vec) + -> PResult<'a, GenericParam> { + let ident = self.parse_ident()?; + + // Parse optional colon and param bounds. + let bounds = if self.eat(&token::Colon) { + self.parse_generic_bounds(Some(self.prev_span))? + } else { + Vec::new() + }; + + let default = if self.eat(&token::Eq) { + Some(self.parse_ty()?) + } else { + None + }; + + Ok(GenericParam { + ident, + id: ast::DUMMY_NODE_ID, + attrs: preceding_attrs.into(), + bounds, + kind: GenericParamKind::Type { + default, + } + }) + } + + fn parse_const_param(&mut self, preceding_attrs: Vec) -> PResult<'a, GenericParam> { + self.expect_keyword(kw::Const)?; + let ident = self.parse_ident()?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; + + Ok(GenericParam { + ident, + id: ast::DUMMY_NODE_ID, + attrs: preceding_attrs.into(), + bounds: Vec::new(), + kind: GenericParamKind::Const { + ty, + } + }) + } + + /// Parses a (possibly empty) list of lifetime and type parameters, possibly including + /// a trailing comma and erroneous trailing attributes. + crate fn parse_generic_params(&mut self) -> PResult<'a, Vec> { + let mut params = Vec::new(); + loop { + let attrs = self.parse_outer_attributes()?; + if self.check_lifetime() { + let lifetime = self.expect_lifetime(); + // Parse lifetime parameter. + let bounds = if self.eat(&token::Colon) { + self.parse_lt_param_bounds() + } else { + Vec::new() + }; + params.push(ast::GenericParam { + ident: lifetime.ident, + id: lifetime.id, + attrs: attrs.into(), + bounds, + kind: ast::GenericParamKind::Lifetime, + }); + } else if self.check_keyword(kw::Const) { + // Parse const parameter. + params.push(self.parse_const_param(attrs)?); + } else if self.check_ident() { + // Parse type parameter. + params.push(self.parse_ty_param(attrs)?); + } else { + // Check for trailing attributes and stop parsing. + if !attrs.is_empty() { + if !params.is_empty() { + self.struct_span_err( + attrs[0].span, + &format!("trailing attribute after generic parameter"), + ) + .span_label(attrs[0].span, "attributes must go before parameters") + .emit(); + } else { + self.struct_span_err( + attrs[0].span, + &format!("attribute without generic parameters"), + ) + .span_label( + attrs[0].span, + "attributes are only permitted when preceding parameters", + ) + .emit(); + } + } + break + } + + if !self.eat(&token::Comma) { + break + } + } + Ok(params) + } + + /// Parses a set of optional generic type parameter declarations. Where + /// clauses are not parsed here, and must be added later via + /// `parse_where_clause()`. + /// + /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > ) + /// | ( < lifetimes , typaramseq ( , )? > ) + /// where typaramseq = ( typaram ) | ( typaram , typaramseq ) + pub(super) fn parse_generics(&mut self) -> PResult<'a, ast::Generics> { + let span_lo = self.token.span; + let (params, span) = if self.eat_lt() { + let params = self.parse_generic_params()?; + self.expect_gt()?; + (params, span_lo.to(self.prev_span)) + } else { + (vec![], self.prev_span.between(self.token.span)) + }; + Ok(ast::Generics { + params, + where_clause: WhereClause { + predicates: Vec::new(), + span: DUMMY_SP, + }, + span, + }) + } + + /// Parses an optional where-clause and places it in `generics`. + /// + /// ```ignore (only-for-syntax-highlight) + /// where T : Trait + 'b, 'a : 'b + /// ``` + pub(super) fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> { + let mut where_clause = WhereClause { + predicates: Vec::new(), + span: self.prev_span.to(self.prev_span), + }; + + if !self.eat_keyword(kw::Where) { + return Ok(where_clause); + } + let lo = self.prev_span; + + // We are considering adding generics to the `where` keyword as an alternative higher-rank + // parameter syntax (as in `where<'a>` or `where`. To avoid that being a breaking + // change we parse those generics now, but report an error. + if self.choose_generics_over_qpath() { + let generics = self.parse_generics()?; + self.struct_span_err( + generics.span, + "generic parameters on `where` clauses are reserved for future use", + ) + .span_label(generics.span, "currently unsupported") + .emit(); + } + + loop { + let lo = self.token.span; + if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { + let lifetime = self.expect_lifetime(); + // Bounds starting with a colon are mandatory, but possibly empty. + self.expect(&token::Colon)?; + let bounds = self.parse_lt_param_bounds(); + where_clause.predicates.push(ast::WherePredicate::RegionPredicate( + ast::WhereRegionPredicate { + span: lo.to(self.prev_span), + lifetime, + bounds, + } + )); + } else if self.check_type() { + // Parse optional `for<'a, 'b>`. + // This `for` is parsed greedily and applies to the whole predicate, + // the bounded type can have its own `for` applying only to it. + // Examples: + // * `for<'a> Trait1<'a>: Trait2<'a /* ok */>` + // * `(for<'a> Trait1<'a>): Trait2<'a /* not ok */>` + // * `for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /* ok */, 'b /* not ok */>` + let lifetime_defs = self.parse_late_bound_lifetime_defs()?; + + // Parse type with mandatory colon and (possibly empty) bounds, + // or with mandatory equality sign and the second type. + let ty = self.parse_ty()?; + if self.eat(&token::Colon) { + let bounds = self.parse_generic_bounds(Some(self.prev_span))?; + where_clause.predicates.push(ast::WherePredicate::BoundPredicate( + ast::WhereBoundPredicate { + span: lo.to(self.prev_span), + bound_generic_params: lifetime_defs, + bounded_ty: ty, + bounds, + } + )); + // FIXME: Decide what should be used here, `=` or `==`. + // FIXME: We are just dropping the binders in lifetime_defs on the floor here. + } else if self.eat(&token::Eq) || self.eat(&token::EqEq) { + let rhs_ty = self.parse_ty()?; + where_clause.predicates.push(ast::WherePredicate::EqPredicate( + ast::WhereEqPredicate { + span: lo.to(self.prev_span), + lhs_ty: ty, + rhs_ty, + id: ast::DUMMY_NODE_ID, + } + )); + } else { + return self.unexpected(); + } + } else { + break + } + + if !self.eat(&token::Comma) { + break + } + } + + where_clause.span = lo.to(self.prev_span); + Ok(where_clause) + } + + pub(super) fn choose_generics_over_qpath(&self) -> bool { + // There's an ambiguity between generic parameters and qualified paths in impls. + // If we see `<` it may start both, so we have to inspect some following tokens. + // The following combinations can only start generics, + // but not qualified paths (with one exception): + // `<` `>` - empty generic parameters + // `<` `#` - generic parameters with attributes + // `<` (LIFETIME|IDENT) `>` - single generic parameter + // `<` (LIFETIME|IDENT) `,` - first generic parameter in a list + // `<` (LIFETIME|IDENT) `:` - generic parameter with bounds + // `<` (LIFETIME|IDENT) `=` - generic parameter with a default + // `<` const - generic const parameter + // The only truly ambiguous case is + // `<` IDENT `>` `::` IDENT ... + // we disambiguate it in favor of generics (`impl ::absolute::Path { ... }`) + // because this is what almost always expected in practice, qualified paths in impls + // (`impl ::AssocTy { ... }`) aren't even allowed by type checker at the moment. + self.token == token::Lt && + (self.look_ahead(1, |t| t == &token::Pound || t == &token::Gt) || + self.look_ahead(1, |t| t.is_lifetime() || t.is_ident()) && + self.look_ahead(2, |t| t == &token::Gt || t == &token::Comma || + t == &token::Colon || t == &token::Eq) || + self.is_keyword_ahead(1, &[kw::Const])) + } +} diff --git a/src/libsyntax/parse/parser/ty.rs b/src/libsyntax/parse/parser/ty.rs index a5a073c3af1a9..e01ce9a48c0dd 100644 --- a/src/libsyntax/parse/parser/ty.rs +++ b/src/libsyntax/parse/parser/ty.rs @@ -430,8 +430,6 @@ impl<'a> Parser<'a> { return Ok(bounds); } - // TODO remove super below. - pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec> { if self.eat_keyword(kw::For) { self.expect_lt()?; From bcfcbfc923aa821332d8ae8ce977f311764768b1 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 11 Aug 2019 20:46:34 +0200 Subject: [PATCH 144/148] parser: {check,expect}_lifetime into ty.rs --- src/libsyntax/parse/parser.rs | 18 +----------------- src/libsyntax/parse/parser/ty.rs | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index afa475a4c8bf0..1c1428c5713f7 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -11,7 +11,7 @@ mod stmt; mod generics; use crate::ast::{self, AttrStyle, Attribute, Arg, BindingMode, StrStyle, SelfKind}; -use crate::ast::{FnDecl, Ident, IsAsync, Lifetime, MacDelimiter, Mutability, TyKind}; +use crate::ast::{FnDecl, Ident, IsAsync, MacDelimiter, Mutability, TyKind}; use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; use crate::ext::hygiene::SyntaxContext; use crate::source_map::{self, respan}; @@ -1046,22 +1046,6 @@ impl<'a> Parser<'a> { Ok(Arg { attrs: attrs.into(), id: ast::DUMMY_NODE_ID, pat, span, ty }) } - crate fn check_lifetime(&mut self) -> bool { - self.expected_tokens.push(TokenType::Lifetime); - self.token.is_lifetime() - } - - /// Parses a single lifetime `'a` or panics. - crate fn expect_lifetime(&mut self) -> Lifetime { - if let Some(ident) = self.token.lifetime() { - let span = self.token.span; - self.bump(); - Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID } - } else { - self.span_bug(self.token.span, "not a lifetime") - } - } - /// Parses mutability (`mut` or nothing). fn parse_mutability(&mut self) -> Mutability { if self.eat_keyword(kw::Mut) { diff --git a/src/libsyntax/parse/parser/ty.rs b/src/libsyntax/parse/parser/ty.rs index e01ce9a48c0dd..1eb3d441e698a 100644 --- a/src/libsyntax/parse/parser/ty.rs +++ b/src/libsyntax/parse/parser/ty.rs @@ -1,8 +1,8 @@ -use super::{Parser, PResult, PathStyle, PrevTokenKind}; +use super::{Parser, PResult, PathStyle, PrevTokenKind, TokenType}; use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath}; use crate::ptr::P; -use crate::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam}; +use crate::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident}; use crate::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef}; use crate::ast::{Mutability, AnonConst, FnDecl, Mac_}; use crate::parse::token::{self, Token}; @@ -442,4 +442,20 @@ impl<'a> Parser<'a> { Ok(Vec::new()) } } + + crate fn check_lifetime(&mut self) -> bool { + self.expected_tokens.push(TokenType::Lifetime); + self.token.is_lifetime() + } + + /// Parses a single lifetime `'a` or panics. + crate fn expect_lifetime(&mut self) -> Lifetime { + if let Some(ident) = self.token.lifetime() { + let span = self.token.span; + self.bump(); + Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID } + } else { + self.span_bug(self.token.span, "not a lifetime") + } + } } From c8a28554422cbe3d97728058f8f07bff28194c19 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sun, 11 Aug 2019 21:10:16 +0200 Subject: [PATCH 145/148] Update RLS This update includes the ability to warn on deprecated config keys. It's important to be able to warn the user whenever they're using an old configuration rather than giving them a cryptic "unknown configuration error" cc https://github.com/rust-lang/rls-vscode/issues/639 Since we removed a config value in the current nightly, it'd be very good if this change can make also make it before cutting the next release. --- src/tools/rls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/rls b/src/tools/rls index bdfcef769fcdc..7b0a20bf13b70 160000 --- a/src/tools/rls +++ b/src/tools/rls @@ -1 +1 @@ -Subproject commit bdfcef769fcdc777320d452ac71afacad48b0de7 +Subproject commit 7b0a20bf13b7061b1eb31a058117ac5517ff8cc9 From 2d5c26c116318d5b922f439392f41774377e6dfc Mon Sep 17 00:00:00 2001 From: sd234678 Date: Fri, 9 Aug 2019 15:14:05 +0100 Subject: [PATCH 146/148] Remove meaningless comments in src/test --- src/test/pretty/stmt_expr_attributes.rs | 2 -- .../associated-type-projection-from-supertrait.rs | 8 -------- ...ciated-types-binding-to-type-defined-in-supertrait.rs | 6 ------ .../associated-types/associated-types-ref-from-struct.rs | 4 ---- .../ui/higher-rank-trait-bounds/hrtb-type-outlives.rs | 2 -- src/test/ui/hrtb/hrtb-conflate-regions.rs | 1 - src/test/ui/impl-trait/bound-normalization-fail.rs | 2 -- src/test/ui/impl-trait/bound-normalization-pass.rs | 4 ---- src/test/ui/issues/issue-12028.rs | 2 -- src/test/ui/issues/issue-16739.rs | 4 ---- src/test/ui/methods/method-projection.rs | 9 --------- .../regions-outlives-projection-container-hrtb.rs | 7 ------- .../regions/regions-outlives-projection-container-wc.rs | 4 ---- .../ui/regions/regions-outlives-projection-container.rs | 4 ---- .../defaultimpl/specialization-no-default.rs | 8 -------- src/test/ui/specialization/specialization-no-default.rs | 8 -------- src/test/ui/traits/traits-conditional-model-fn.rs | 6 ------ 17 files changed, 81 deletions(-) diff --git a/src/test/pretty/stmt_expr_attributes.rs b/src/test/pretty/stmt_expr_attributes.rs index 02d93238dd643..619cce685d75f 100644 --- a/src/test/pretty/stmt_expr_attributes.rs +++ b/src/test/pretty/stmt_expr_attributes.rs @@ -259,8 +259,6 @@ fn _12() { } } -///////////////// - fn foo() { } fn foo3(_: i32, _: (), _: ()) { } fn qux(_: i32) { } diff --git a/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs b/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs index 06dfe490b8bd3..7e05bcd309a4f 100644 --- a/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs +++ b/src/test/ui/associated-type/associated-type-projection-from-supertrait.rs @@ -12,30 +12,22 @@ pub trait Car : Vehicle { fn chip_paint(&self, c: Self::Color) { } } -/////////////////////////////////////////////////////////////////////////// - struct Black; struct ModelT; impl Vehicle for ModelT { type Color = Black; } impl Car for ModelT { } -/////////////////////////////////////////////////////////////////////////// - struct Blue; struct ModelU; impl Vehicle for ModelU { type Color = Blue; } impl Car for ModelU { } -/////////////////////////////////////////////////////////////////////////// - fn dent(c: C, color: C::Color) { c.chip_paint(color) } fn a() { dent(ModelT, Black); } fn b() { dent(ModelT, Blue); } //~ ERROR mismatched types fn c() { dent(ModelU, Black); } //~ ERROR mismatched types fn d() { dent(ModelU, Blue); } -/////////////////////////////////////////////////////////////////////////// - fn e() { ModelT.chip_paint(Black); } fn f() { ModelT.chip_paint(Blue); } //~ ERROR mismatched types fn g() { ModelU.chip_paint(Black); } //~ ERROR mismatched types diff --git a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs index 653130843c8de..6b2bbbe2e4fb9 100644 --- a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs +++ b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.rs @@ -11,22 +11,16 @@ pub trait Car : Vehicle { fn honk(&self) { } } -/////////////////////////////////////////////////////////////////////////// - struct Black; struct ModelT; impl Vehicle for ModelT { type Color = Black; } impl Car for ModelT { } -/////////////////////////////////////////////////////////////////////////// - struct Blue; struct ModelU; impl Vehicle for ModelU { type Color = Blue; } impl Car for ModelU { } -/////////////////////////////////////////////////////////////////////////// - fn black_car>(c: C) { } diff --git a/src/test/ui/associated-types/associated-types-ref-from-struct.rs b/src/test/ui/associated-types/associated-types-ref-from-struct.rs index 3ccba289e4b0a..c89f6046e6bf2 100644 --- a/src/test/ui/associated-types/associated-types-ref-from-struct.rs +++ b/src/test/ui/associated-types/associated-types-ref-from-struct.rs @@ -9,8 +9,6 @@ trait Test { fn test(&self, value: &Self::V) -> bool; } -/////////////////////////////////////////////////////////////////////////// - struct TesterPair { tester: T, value: T::V, @@ -26,8 +24,6 @@ impl TesterPair { } } -/////////////////////////////////////////////////////////////////////////// - struct EqU32(u32); impl Test for EqU32 { type V = u32; diff --git a/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs b/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs index a8f38180cc29d..88d396101dba2 100644 --- a/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs +++ b/src/test/ui/higher-rank-trait-bounds/hrtb-type-outlives.rs @@ -14,7 +14,6 @@ fn want_foo() { } -/////////////////////////////////////////////////////////////////////////// // Expressed as a where clause struct SomeStruct { @@ -30,7 +29,6 @@ fn one() { want_foo::>(); } -/////////////////////////////////////////////////////////////////////////// // Expressed as shorthand struct AnotherStruct { diff --git a/src/test/ui/hrtb/hrtb-conflate-regions.rs b/src/test/ui/hrtb/hrtb-conflate-regions.rs index 391303676d784..004d62ac513ff 100644 --- a/src/test/ui/hrtb/hrtb-conflate-regions.rs +++ b/src/test/ui/hrtb/hrtb-conflate-regions.rs @@ -15,7 +15,6 @@ fn want_foo1() { } -/////////////////////////////////////////////////////////////////////////// // Expressed as a where clause struct SomeStruct; diff --git a/src/test/ui/impl-trait/bound-normalization-fail.rs b/src/test/ui/impl-trait/bound-normalization-fail.rs index c33261bfd0909..44103554102df 100644 --- a/src/test/ui/impl-trait/bound-normalization-fail.rs +++ b/src/test/ui/impl-trait/bound-normalization-fail.rs @@ -7,7 +7,6 @@ // See issue 60414 -///////////////////////////////////////////// // Reduction to `impl Trait` struct Foo(T); @@ -32,7 +31,6 @@ mod impl_trait { } } -///////////////////////////////////////////// // Same with lifetimes in the trait mod lifetimes { diff --git a/src/test/ui/impl-trait/bound-normalization-pass.rs b/src/test/ui/impl-trait/bound-normalization-pass.rs index 5b634e3106e3b..b0ed4be54b899 100644 --- a/src/test/ui/impl-trait/bound-normalization-pass.rs +++ b/src/test/ui/impl-trait/bound-normalization-pass.rs @@ -8,7 +8,6 @@ // See issue 60414 -///////////////////////////////////////////// // Reduction to `impl Trait` struct Foo(T); @@ -32,7 +31,6 @@ mod impl_trait { } } -///////////////////////////////////////////// // Same with lifetimes in the trait mod lifetimes { @@ -59,7 +57,6 @@ mod lifetimes { } } -///////////////////////////////////////////// // Reduction using `impl Trait` in bindings mod impl_trait_in_bindings { @@ -80,7 +77,6 @@ mod impl_trait_in_bindings { } } -///////////////////////////////////////////// // The same applied to `type Foo = impl Bar`s mod opaque_types { diff --git a/src/test/ui/issues/issue-12028.rs b/src/test/ui/issues/issue-12028.rs index d55354529a9b4..7c2b0d69c8b25 100644 --- a/src/test/ui/issues/issue-12028.rs +++ b/src/test/ui/issues/issue-12028.rs @@ -17,8 +17,6 @@ trait StreamHasher { fn stream(&self) -> Self::S; } -////////////////////////////////////////////////////////////////////////////// - trait StreamHash: Hash { fn input_stream(&self, stream: &mut H::S); } diff --git a/src/test/ui/issues/issue-16739.rs b/src/test/ui/issues/issue-16739.rs index 54ad8fd076e4e..94da2ca5cab81 100644 --- a/src/test/ui/issues/issue-16739.rs +++ b/src/test/ui/issues/issue-16739.rs @@ -16,8 +16,6 @@ impl FnOnce<()> for Foo { extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) } } -///////////////////////////////////////////////////////////////////////// - impl FnMut<(u32,)> for Foo { extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x } } @@ -27,8 +25,6 @@ impl FnOnce<(u32,)> for Foo { extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) } } -///////////////////////////////////////////////////////////////////////// - impl FnMut<(u32,u32)> for Foo { extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y } } diff --git a/src/test/ui/methods/method-projection.rs b/src/test/ui/methods/method-projection.rs index cf33d53968b72..21d983f192ab6 100644 --- a/src/test/ui/methods/method-projection.rs +++ b/src/test/ui/methods/method-projection.rs @@ -2,9 +2,6 @@ // Test that we can use method notation to call methods based on a // projection bound from a trait. Issue #20469. -/////////////////////////////////////////////////////////////////////////// - - trait MakeString { fn make_string(&self) -> String; } @@ -21,8 +18,6 @@ impl MakeString for usize { } } -/////////////////////////////////////////////////////////////////////////// - trait Foo { type F: MakeString; @@ -33,8 +28,6 @@ fn foo(f: &F) -> String { f.get().make_string() } -/////////////////////////////////////////////////////////////////////////// - struct SomeStruct { field: isize, } @@ -47,8 +40,6 @@ impl Foo for SomeStruct { } } -/////////////////////////////////////////////////////////////////////////// - struct SomeOtherStruct { field: usize, } diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs b/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs index 407a4fdf59bb7..cee741184ca2a 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.rs @@ -6,9 +6,6 @@ #![allow(dead_code)] - -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait<'b> { type TheAssocType; } @@ -21,8 +18,6 @@ impl<'a,'b> TheTrait<'a> for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithHrAssoc where for<'a> T : TheTrait<'a> { @@ -37,8 +32,6 @@ fn with_assoc<'a,'b>() { //[nll]~^^ ERROR lifetime may not live long enough } -/////////////////////////////////////////////////////////////////////////// - pub trait TheSubTrait : for<'a> TheTrait<'a> { } diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.rs b/src/test/ui/regions/regions-outlives-projection-container-wc.rs index 5037ea536dae9..99965f333907b 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.rs +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.rs @@ -8,8 +8,6 @@ #![allow(dead_code)] -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait { type TheAssocType; } @@ -22,8 +20,6 @@ impl<'b> TheTrait for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithAssoc where T : TheTrait { m: [T; 0] } diff --git a/src/test/ui/regions/regions-outlives-projection-container.rs b/src/test/ui/regions/regions-outlives-projection-container.rs index 78305c0693905..3afc600becb6e 100644 --- a/src/test/ui/regions/regions-outlives-projection-container.rs +++ b/src/test/ui/regions/regions-outlives-projection-container.rs @@ -5,8 +5,6 @@ #![allow(dead_code)] #![feature(rustc_attrs)] -/////////////////////////////////////////////////////////////////////////// - pub trait TheTrait { type TheAssocType; } @@ -19,8 +17,6 @@ impl<'b> TheTrait for TheType<'b> { type TheAssocType = &'b (); } -/////////////////////////////////////////////////////////////////////////// - pub struct WithAssoc { m: [T; 0] } diff --git a/src/test/ui/specialization/defaultimpl/specialization-no-default.rs b/src/test/ui/specialization/defaultimpl/specialization-no-default.rs index 7ea79a9a7bf82..37005f839d488 100644 --- a/src/test/ui/specialization/defaultimpl/specialization-no-default.rs +++ b/src/test/ui/specialization/defaultimpl/specialization-no-default.rs @@ -3,9 +3,7 @@ // Check a number of scenarios in which one impl tries to override another, // without correctly using `default`. -//////////////////////////////////////////////////////////////////////////////// // Test 1: one layer of specialization, multiple methods, missing `default` -//////////////////////////////////////////////////////////////////////////////// trait Foo { fn foo(&self); @@ -25,9 +23,7 @@ impl Foo for u32 { fn bar(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 2: one layer of specialization, missing `default` on associated type -//////////////////////////////////////////////////////////////////////////////// trait Bar { type T; @@ -41,9 +37,7 @@ impl Bar for u8 { type T = (); //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3a: multiple layers of specialization, missing interior `default` -//////////////////////////////////////////////////////////////////////////////// trait Baz { fn baz(&self); @@ -61,10 +55,8 @@ impl Baz for i32 { fn baz(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3b: multiple layers of specialization, missing interior `default`, // redundant `default` in bottom layer. -//////////////////////////////////////////////////////////////////////////////// trait Redundant { fn redundant(&self); diff --git a/src/test/ui/specialization/specialization-no-default.rs b/src/test/ui/specialization/specialization-no-default.rs index 29afbbd9bf267..57346b26d24ec 100644 --- a/src/test/ui/specialization/specialization-no-default.rs +++ b/src/test/ui/specialization/specialization-no-default.rs @@ -3,9 +3,7 @@ // Check a number of scenarios in which one impl tries to override another, // without correctly using `default`. -//////////////////////////////////////////////////////////////////////////////// // Test 1: one layer of specialization, multiple methods, missing `default` -//////////////////////////////////////////////////////////////////////////////// trait Foo { fn foo(&self); @@ -25,9 +23,7 @@ impl Foo for u32 { fn bar(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 2: one layer of specialization, missing `default` on associated type -//////////////////////////////////////////////////////////////////////////////// trait Bar { type T; @@ -41,9 +37,7 @@ impl Bar for u8 { type T = (); //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3a: multiple layers of specialization, missing interior `default` -//////////////////////////////////////////////////////////////////////////////// trait Baz { fn baz(&self); @@ -61,10 +55,8 @@ impl Baz for i32 { fn baz(&self) {} //~ ERROR E0520 } -//////////////////////////////////////////////////////////////////////////////// // Test 3b: multiple layers of specialization, missing interior `default`, // redundant `default` in bottom layer. -//////////////////////////////////////////////////////////////////////////////// trait Redundant { fn redundant(&self); diff --git a/src/test/ui/traits/traits-conditional-model-fn.rs b/src/test/ui/traits/traits-conditional-model-fn.rs index 27ce6d93a8195..afdfb96394bd0 100644 --- a/src/test/ui/traits/traits-conditional-model-fn.rs +++ b/src/test/ui/traits/traits-conditional-model-fn.rs @@ -14,8 +14,6 @@ use go_trait::{Go, GoMut, GoOnce, go, go_mut, go_once}; use std::rc::Rc; use std::cell::Cell; -/////////////////////////////////////////////////////////////////////////// - struct SomeGoableThing { counter: Rc> } @@ -26,8 +24,6 @@ impl Go for SomeGoableThing { } } -/////////////////////////////////////////////////////////////////////////// - struct SomeGoOnceableThing { counter: Rc> } @@ -38,8 +34,6 @@ impl GoOnce for SomeGoOnceableThing { } } -/////////////////////////////////////////////////////////////////////////// - fn main() { let counter = Rc::new(Cell::new(0)); let mut x = SomeGoableThing { counter: counter.clone() }; From a9f04524e714e056aeb484a422a7ffc16fce6f78 Mon Sep 17 00:00:00 2001 From: sd234678 Date: Fri, 9 Aug 2019 18:43:30 +0100 Subject: [PATCH 147/148] Update stderr files with --bless --- ...ted-type-projection-from-supertrait.stderr | 8 ++-- ...nding-to-type-defined-in-supertrait.stderr | 8 ++-- src/test/ui/hrtb/hrtb-conflate-regions.stderr | 2 +- .../bound-normalization-fail.stderr | 4 +- src/test/ui/issues/issue-12028.stderr | 2 +- ...s-projection-container-hrtb.migrate.stderr | 20 +++++----- ...lives-projection-container-hrtb.nll.stderr | 4 +- ...ves-projection-container-wc.migrate.stderr | 10 ++--- ...utlives-projection-container-wc.nll.stderr | 2 +- ...gions-outlives-projection-container.stderr | 40 +++++++++---------- .../specialization-no-default.stderr | 10 ++--- .../specialization-no-default.stderr | 10 ++--- 12 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr b/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr index 06f1a1cc64c42..4ba4925ef1b37 100644 --- a/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr +++ b/src/test/ui/associated-type/associated-type-projection-from-supertrait.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:33:23 + --> $DIR/associated-type-projection-from-supertrait.rs:27:23 | LL | fn b() { dent(ModelT, Blue); } | ^^^^ expected struct `Black`, found struct `Blue` @@ -8,7 +8,7 @@ LL | fn b() { dent(ModelT, Blue); } found type `Blue` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:34:23 + --> $DIR/associated-type-projection-from-supertrait.rs:28:23 | LL | fn c() { dent(ModelU, Black); } | ^^^^^ expected struct `Blue`, found struct `Black` @@ -17,7 +17,7 @@ LL | fn c() { dent(ModelU, Black); } found type `Black` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:40:28 + --> $DIR/associated-type-projection-from-supertrait.rs:32:28 | LL | fn f() { ModelT.chip_paint(Blue); } | ^^^^ expected struct `Black`, found struct `Blue` @@ -26,7 +26,7 @@ LL | fn f() { ModelT.chip_paint(Blue); } found type `Blue` error[E0308]: mismatched types - --> $DIR/associated-type-projection-from-supertrait.rs:41:28 + --> $DIR/associated-type-projection-from-supertrait.rs:33:28 | LL | fn g() { ModelU.chip_paint(Black); } | ^^^^^ expected struct `Blue`, found struct `Black` diff --git a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr index 4b548604983df..89c48d50cdb65 100644 --- a/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr +++ b/src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr @@ -1,5 +1,5 @@ error[E0271]: type mismatch resolving `::Color == Blue` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:37:10 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:10 | LL | fn b() { blue_car(ModelT); } | ^^^^^^^^ expected struct `Black`, found struct `Blue` @@ -7,13 +7,13 @@ LL | fn b() { blue_car(ModelT); } = note: expected type `Black` found type `Blue` note: required by `blue_car` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:33:1 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:27:1 | LL | fn blue_car>(c: C) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0271]: type mismatch resolving `::Color == Black` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:38:10 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10 | LL | fn c() { black_car(ModelU); } | ^^^^^^^^^ expected struct `Blue`, found struct `Black` @@ -21,7 +21,7 @@ LL | fn c() { black_car(ModelU); } = note: expected type `Blue` found type `Black` note: required by `black_car` - --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:30:1 + --> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:24:1 | LL | fn black_car>(c: C) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/hrtb/hrtb-conflate-regions.stderr b/src/test/ui/hrtb/hrtb-conflate-regions.stderr index 3fb6baa35e144..20265d66c6f43 100644 --- a/src/test/ui/hrtb/hrtb-conflate-regions.stderr +++ b/src/test/ui/hrtb/hrtb-conflate-regions.stderr @@ -1,5 +1,5 @@ error[E0277]: the trait bound `for<'a, 'b> SomeStruct: Foo<(&'a isize, &'b isize)>` is not satisfied - --> $DIR/hrtb-conflate-regions.rs:28:10 + --> $DIR/hrtb-conflate-regions.rs:27:10 | LL | fn b() { want_foo2::(); } | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a, 'b> Foo<(&'a isize, &'b isize)>` is not implemented for `SomeStruct` diff --git a/src/test/ui/impl-trait/bound-normalization-fail.stderr b/src/test/ui/impl-trait/bound-normalization-fail.stderr index aa306a7e08a4c..c63a51bde70f8 100644 --- a/src/test/ui/impl-trait/bound-normalization-fail.stderr +++ b/src/test/ui/impl-trait/bound-normalization-fail.stderr @@ -7,7 +7,7 @@ LL | #![feature(impl_trait_in_bindings)] = note: `#[warn(incomplete_features)]` on by default error[E0271]: type mismatch resolving ` as FooLike>::Output == ::Assoc` - --> $DIR/bound-normalization-fail.rs:29:32 + --> $DIR/bound-normalization-fail.rs:28:32 | LL | fn foo_fail() -> impl FooLike { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type @@ -17,7 +17,7 @@ LL | fn foo_fail() -> impl FooLike { = note: the return type of a function must have a statically known size error[E0271]: type mismatch resolving ` as FooLike>::Output == >::Assoc` - --> $DIR/bound-normalization-fail.rs:46:41 + --> $DIR/bound-normalization-fail.rs:44:41 | LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type diff --git a/src/test/ui/issues/issue-12028.stderr b/src/test/ui/issues/issue-12028.stderr index 64694c7a8d0b6..24aa88c3fa379 100644 --- a/src/test/ui/issues/issue-12028.stderr +++ b/src/test/ui/issues/issue-12028.stderr @@ -1,5 +1,5 @@ error[E0284]: type annotations required: cannot resolve `<_ as StreamHasher>::S == ::S` - --> $DIR/issue-12028.rs:29:14 + --> $DIR/issue-12028.rs:27:14 | LL | self.input_stream(&mut stream); | ^^^^^^^^^^^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr b/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr index d83301840088d..ed5800940ee31 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.migrate.stderr @@ -1,33 +1,33 @@ error[E0491]: in type `&'a WithHrAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-hrtb.rs:35:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:30:12 | LL | let _: &'a WithHrAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 32:15 - --> $DIR/regions-outlives-projection-container-hrtb.rs:32:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 27:15 + --> $DIR/regions-outlives-projection-container-hrtb.rs:27:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 32:18 - --> $DIR/regions-outlives-projection-container-hrtb.rs:32:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 27:18 + --> $DIR/regions-outlives-projection-container-hrtb.rs:27:18 | LL | fn with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithHrAssocSub>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-hrtb.rs:57:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:50:12 | LL | let _: &'a WithHrAssocSub> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 53:19 - --> $DIR/regions-outlives-projection-container-hrtb.rs:53:19 +note: the pointer is valid for the lifetime 'a as defined on the function body at 46:19 + --> $DIR/regions-outlives-projection-container-hrtb.rs:46:19 | LL | fn with_assoc_sub<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 53:22 - --> $DIR/regions-outlives-projection-container-hrtb.rs:53:22 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 46:22 + --> $DIR/regions-outlives-projection-container-hrtb.rs:46:22 | LL | fn with_assoc_sub<'a,'b>() { | ^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr b/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr index 5028663ba6d04..eed9934be121d 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-hrtb.nll.stderr @@ -1,5 +1,5 @@ error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-hrtb.rs:35:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:30:12 | LL | fn with_assoc<'a,'b>() { | -- -- lifetime `'b` defined here @@ -10,7 +10,7 @@ LL | let _: &'a WithHrAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-hrtb.rs:57:12 + --> $DIR/regions-outlives-projection-container-hrtb.rs:50:12 | LL | fn with_assoc_sub<'a,'b>() { | -- -- lifetime `'b` defined here diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr b/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr index 9e31065ca4eec..152e6c5600c4e 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.migrate.stderr @@ -1,16 +1,16 @@ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container-wc.rs:37:12 + --> $DIR/regions-outlives-projection-container-wc.rs:33:12 | LL | let _: &'a WithAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 31:15 - --> $DIR/regions-outlives-projection-container-wc.rs:31:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 27:15 + --> $DIR/regions-outlives-projection-container-wc.rs:27:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 31:18 - --> $DIR/regions-outlives-projection-container-wc.rs:31:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 27:18 + --> $DIR/regions-outlives-projection-container-wc.rs:27:18 | LL | fn with_assoc<'a,'b>() { | ^^ diff --git a/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr b/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr index 880fe17b740e4..8c54d8da0a063 100644 --- a/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container-wc.nll.stderr @@ -1,5 +1,5 @@ error: lifetime may not live long enough - --> $DIR/regions-outlives-projection-container-wc.rs:37:12 + --> $DIR/regions-outlives-projection-container-wc.rs:33:12 | LL | fn with_assoc<'a,'b>() { | -- -- lifetime `'b` defined here diff --git a/src/test/ui/regions/regions-outlives-projection-container.stderr b/src/test/ui/regions/regions-outlives-projection-container.stderr index b50347ac96427..3c1a98a3c018f 100644 --- a/src/test/ui/regions/regions-outlives-projection-container.stderr +++ b/src/test/ui/regions/regions-outlives-projection-container.stderr @@ -1,67 +1,67 @@ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:40:13 + --> $DIR/regions-outlives-projection-container.rs:36:13 | LL | let _x: &'a WithAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 32:15 - --> $DIR/regions-outlives-projection-container.rs:32:15 +note: the pointer is valid for the lifetime 'a as defined on the function body at 28:15 + --> $DIR/regions-outlives-projection-container.rs:28:15 | LL | fn with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 32:18 - --> $DIR/regions-outlives-projection-container.rs:32:18 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 28:18 + --> $DIR/regions-outlives-projection-container.rs:28:18 | LL | fn with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithoutAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:58:13 + --> $DIR/regions-outlives-projection-container.rs:54:13 | LL | let _x: &'a WithoutAssoc> = loop { }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 54:18 - --> $DIR/regions-outlives-projection-container.rs:54:18 +note: the pointer is valid for the lifetime 'a as defined on the function body at 50:18 + --> $DIR/regions-outlives-projection-container.rs:50:18 | LL | fn without_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 54:21 - --> $DIR/regions-outlives-projection-container.rs:54:21 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 50:21 + --> $DIR/regions-outlives-projection-container.rs:50:21 | LL | fn without_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:67:12 + --> $DIR/regions-outlives-projection-container.rs:63:12 | LL | call::<&'a WithAssoc>>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 62:20 - --> $DIR/regions-outlives-projection-container.rs:62:20 +note: the pointer is valid for the lifetime 'a as defined on the function body at 58:20 + --> $DIR/regions-outlives-projection-container.rs:58:20 | LL | fn call_with_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 62:23 - --> $DIR/regions-outlives-projection-container.rs:62:23 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 58:23 + --> $DIR/regions-outlives-projection-container.rs:58:23 | LL | fn call_with_assoc<'a,'b>() { | ^^ error[E0491]: in type `&'a WithoutAssoc>`, reference has a longer lifetime than the data it references - --> $DIR/regions-outlives-projection-container.rs:74:12 + --> $DIR/regions-outlives-projection-container.rs:70:12 | LL | call::<&'a WithoutAssoc>>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: the pointer is valid for the lifetime 'a as defined on the function body at 71:23 - --> $DIR/regions-outlives-projection-container.rs:71:23 +note: the pointer is valid for the lifetime 'a as defined on the function body at 67:23 + --> $DIR/regions-outlives-projection-container.rs:67:23 | LL | fn call_without_assoc<'a,'b>() { | ^^ -note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 71:26 - --> $DIR/regions-outlives-projection-container.rs:71:26 +note: but the referenced data is only valid for the lifetime 'b as defined on the function body at 67:26 + --> $DIR/regions-outlives-projection-container.rs:67:26 | LL | fn call_without_assoc<'a,'b>() { | ^^ diff --git a/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr b/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr index 91690f64d948c..13636b28b126c 100644 --- a/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr +++ b/src/test/ui/specialization/defaultimpl/specialization-no-default.stderr @@ -1,5 +1,5 @@ error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:22:5 + --> $DIR/specialization-no-default.rs:20:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -13,7 +13,7 @@ LL | fn foo(&self) {} = note: to specialize, `foo` in the parent `impl` must be marked `default` error[E0520]: `bar` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:25:5 + --> $DIR/specialization-no-default.rs:23:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -27,7 +27,7 @@ LL | fn bar(&self) {} = note: to specialize, `bar` in the parent `impl` must be marked `default` error[E0520]: `T` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:41:5 + --> $DIR/specialization-no-default.rs:37:5 | LL | / impl Bar for T { LL | | type T = u8; @@ -40,7 +40,7 @@ LL | type T = (); = note: to specialize, `T` in the parent `impl` must be marked `default` error[E0520]: `baz` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:61:5 + --> $DIR/specialization-no-default.rs:55:5 | LL | / impl Baz for T { LL | | fn baz(&self) {} @@ -53,7 +53,7 @@ LL | fn baz(&self) {} = note: to specialize, `baz` in the parent `impl` must be marked `default` error[E0520]: `redundant` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:82:5 + --> $DIR/specialization-no-default.rs:74:5 | LL | / impl Redundant for T { LL | | fn redundant(&self) {} diff --git a/src/test/ui/specialization/specialization-no-default.stderr b/src/test/ui/specialization/specialization-no-default.stderr index c39986de38dc2..992e9abbd4ce2 100644 --- a/src/test/ui/specialization/specialization-no-default.stderr +++ b/src/test/ui/specialization/specialization-no-default.stderr @@ -1,5 +1,5 @@ error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:22:5 + --> $DIR/specialization-no-default.rs:20:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -13,7 +13,7 @@ LL | fn foo(&self) {} = note: to specialize, `foo` in the parent `impl` must be marked `default` error[E0520]: `bar` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:25:5 + --> $DIR/specialization-no-default.rs:23:5 | LL | / impl Foo for T { LL | | fn foo(&self) {} @@ -27,7 +27,7 @@ LL | fn bar(&self) {} = note: to specialize, `bar` in the parent `impl` must be marked `default` error[E0520]: `T` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:41:5 + --> $DIR/specialization-no-default.rs:37:5 | LL | / impl Bar for T { LL | | type T = u8; @@ -40,7 +40,7 @@ LL | type T = (); = note: to specialize, `T` in the parent `impl` must be marked `default` error[E0520]: `baz` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:61:5 + --> $DIR/specialization-no-default.rs:55:5 | LL | / impl Baz for T { LL | | fn baz(&self) {} @@ -53,7 +53,7 @@ LL | fn baz(&self) {} = note: to specialize, `baz` in the parent `impl` must be marked `default` error[E0520]: `redundant` specializes an item from a parent `impl`, but that item is not marked `default` - --> $DIR/specialization-no-default.rs:82:5 + --> $DIR/specialization-no-default.rs:74:5 | LL | / impl Redundant for T { LL | | fn redundant(&self) {} From dcb21b6b6612a0eba5e3e23364dc0bf49c8a813c Mon Sep 17 00:00:00 2001 From: sd234678 Date: Fri, 9 Aug 2019 18:46:35 +0100 Subject: [PATCH 148/148] Rebase onto master --- src/llvm-project | 2 +- src/tools/cargo | 2 +- src/tools/clippy | 2 +- src/tools/miri | 2 +- src/tools/rls | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/llvm-project b/src/llvm-project index 48818e9f5d0f2..f2b0c67661f19 160000 --- a/src/llvm-project +++ b/src/llvm-project @@ -1 +1 @@ -Subproject commit 48818e9f5d0f2d5978a9b43ad1a2e8d0b83f6aa0 +Subproject commit f2b0c67661f19bb2564225d47aca424ad0449791 diff --git a/src/tools/cargo b/src/tools/cargo index e853aa9765431..42a8c0adf9132 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit e853aa976543168fbb6bfcc983c35c3facca9840 +Subproject commit 42a8c0adf91323c01228268c651aef5366b25b69 diff --git a/src/tools/clippy b/src/tools/clippy index 72da1015d6d91..b041511b5fcd3 160000 --- a/src/tools/clippy +++ b/src/tools/clippy @@ -1 +1 @@ -Subproject commit 72da1015d6d918fe1b29170acbf486d30e0c2695 +Subproject commit b041511b5fcd386c4ae74a30b60a5081f8717fbe diff --git a/src/tools/miri b/src/tools/miri index c1cb24969e84d..b12ebfc3de853 160000 --- a/src/tools/miri +++ b/src/tools/miri @@ -1 +1 @@ -Subproject commit c1cb24969e84dfaded2769ab5575effc8d4f5c30 +Subproject commit b12ebfc3de853abf6b4260c44a71cd51323803c5 diff --git a/src/tools/rls b/src/tools/rls index 7b0a20bf13b70..bdfcef769fcdc 160000 --- a/src/tools/rls +++ b/src/tools/rls @@ -1 +1 @@ -Subproject commit 7b0a20bf13b7061b1eb31a058117ac5517ff8cc9 +Subproject commit bdfcef769fcdc777320d452ac71afacad48b0de7