From c144dc07e35c1ef4a25bc321dce07565952121aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 24 Nov 2018 17:12:28 -0800 Subject: [PATCH 1/5] Fix ICE with feature self_struct_ctor --- src/librustc/middle/reachable.rs | 1 + src/test/ui/issues/issue-56202.rs | 15 +++++++++++++++ src/test/ui/issues/issue-56202.stderr | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 src/test/ui/issues/issue-56202.rs create mode 100644 src/test/ui/issues/issue-56202.stderr diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index 0009a517dd1db..cf81729bdaba8 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -116,6 +116,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> { Some(Def::Local(node_id)) | Some(Def::Upvar(node_id, ..)) => { self.reachable_symbols.insert(node_id); } + Some(Def::Err) => {} // #56202: calling `def.def_id()` would be an error Some(def) => { let def_id = def.def_id(); if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) { diff --git a/src/test/ui/issues/issue-56202.rs b/src/test/ui/issues/issue-56202.rs new file mode 100644 index 0000000000000..c80c372c9e2ea --- /dev/null +++ b/src/test/ui/issues/issue-56202.rs @@ -0,0 +1,15 @@ +#![feature(self_struct_ctor)] + +trait FooTrait {} + +trait BarTrait { + fn foo(_: T) -> Self; +} + +struct FooStruct(u32); + +impl BarTrait for FooStruct { + fn foo(_: T) -> Self { + Self(u32::default()) + } +} diff --git a/src/test/ui/issues/issue-56202.stderr b/src/test/ui/issues/issue-56202.stderr new file mode 100644 index 0000000000000..3007b08450298 --- /dev/null +++ b/src/test/ui/issues/issue-56202.stderr @@ -0,0 +1,7 @@ +error[E0601]: `main` function not found in crate `issue_56202` + | + = note: consider adding a `main` function to `$DIR/issue-56202.rs` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0601`. From 027bf8b4de0176a526b7e5adaa63e65a87c37951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 25 Nov 2018 07:40:26 -0800 Subject: [PATCH 2/5] Use opt_def_id instead of having special branch --- src/librustc/middle/reachable.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index cf81729bdaba8..ab0094df0e219 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -116,10 +116,10 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> { Some(Def::Local(node_id)) | Some(Def::Upvar(node_id, ..)) => { self.reachable_symbols.insert(node_id); } - Some(Def::Err) => {} // #56202: calling `def.def_id()` would be an error Some(def) => { - let def_id = def.def_id(); - if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) { + if let Some((node_id, def_id)) = def.opt_def_id().and_then(|def_id| { + self.tcx.hir.as_local_node_id(def_id).map(|node_id| (node_id, def_id)) + }) { if self.def_id_represents_local_inlined_item(def_id) { self.worklist.push(node_id); } else { From 5f19cdc4f87be3bf6391264ef41264bdd3d0d370 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 30 Nov 2018 02:55:22 +0000 Subject: [PATCH 3/5] Changed test for issue 56202 to compile-pass. --- src/test/ui/issues/issue-56202.rs | 4 ++++ src/test/ui/issues/issue-56202.stderr | 7 ------- 2 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 src/test/ui/issues/issue-56202.stderr diff --git a/src/test/ui/issues/issue-56202.rs b/src/test/ui/issues/issue-56202.rs index c80c372c9e2ea..4b25f58c02ac8 100644 --- a/src/test/ui/issues/issue-56202.rs +++ b/src/test/ui/issues/issue-56202.rs @@ -1,3 +1,5 @@ +// compile-pass + #![feature(self_struct_ctor)] trait FooTrait {} @@ -13,3 +15,5 @@ impl BarTrait for FooStruct { Self(u32::default()) } } + +fn main() {} diff --git a/src/test/ui/issues/issue-56202.stderr b/src/test/ui/issues/issue-56202.stderr deleted file mode 100644 index 3007b08450298..0000000000000 --- a/src/test/ui/issues/issue-56202.stderr +++ /dev/null @@ -1,7 +0,0 @@ -error[E0601]: `main` function not found in crate `issue_56202` - | - = note: consider adding a `main` function to `$DIR/issue-56202.rs` - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0601`. From d49a8d558f20a753b13c1c9f10c1f63775399f7d Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 30 Nov 2018 02:57:30 +0000 Subject: [PATCH 4/5] Removed feature gate. --- .../src/language-features/self-struct-ctor.md | 33 ------------------- src/librustc/hir/lowering.rs | 16 --------- src/libsyntax/feature_gate.rs | 7 ++-- .../rfcs/rfc-2302-self-struct-ctor.rs | 2 -- .../feature-gate-self-struct-ctor.rs | 22 ------------- .../feature-gate-self-struct-ctor.stderr | 19 ----------- src/test/ui/issues/issue-56202.rs | 2 -- .../ui/keyword/keyword-self-as-identifier.rs | 1 - .../keyword/keyword-self-as-identifier.stderr | 10 +----- src/test/ui/self/self_type_keyword-2.rs | 3 -- src/test/ui/self/self_type_keyword-2.stderr | 30 ++--------------- 11 files changed, 7 insertions(+), 138 deletions(-) delete mode 100644 src/doc/unstable-book/src/language-features/self-struct-ctor.md delete mode 100644 src/test/ui/feature-gates/feature-gate-self-struct-ctor.rs delete mode 100644 src/test/ui/feature-gates/feature-gate-self-struct-ctor.stderr diff --git a/src/doc/unstable-book/src/language-features/self-struct-ctor.md b/src/doc/unstable-book/src/language-features/self-struct-ctor.md deleted file mode 100644 index b4742c48a32ff..0000000000000 --- a/src/doc/unstable-book/src/language-features/self-struct-ctor.md +++ /dev/null @@ -1,33 +0,0 @@ -# `self_struct_ctor` - -The tracking issue for this feature is: [#51994] -[#51994]: https://github.com/rust-lang/rust/issues/51994 - ------------------------- - -The `self_struct_ctor` feature gate lets you use the special `Self` -identifier as a constructor and a pattern. - -A simple example is: - -```rust -#![feature(self_struct_ctor)] - -struct ST(i32, i32); - -impl ST { - fn new() -> Self { - ST(0, 1) - } - - fn ctor() -> Self { - Self(1,2) // constructed by `Self`, it is the same as `ST(1, 2)` - } - - fn pattern(self) { - match self { - Self(x, y) => println!("{} {}", x, y), // used as a pattern - } - } -} -``` diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index b3ba2968c9f51..dc8baa112bb59 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -67,7 +67,6 @@ use syntax::ast; use syntax::ast::*; use syntax::errors; use syntax::ext::hygiene::{Mark, SyntaxContext}; -use syntax::feature_gate::{emit_feature_err, GateIssue}; use syntax::print::pprust; use syntax::ptr::P; use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned}; @@ -3628,7 +3627,6 @@ impl<'a> LoweringContext<'a> { ParamMode::Optional, ImplTraitContext::disallowed(), ); - self.check_self_struct_ctor_feature(&qpath); hir::PatKind::TupleStruct( qpath, pats.iter().map(|x| self.lower_pat(x)).collect(), @@ -3643,7 +3641,6 @@ impl<'a> LoweringContext<'a> { ParamMode::Optional, ImplTraitContext::disallowed(), ); - self.check_self_struct_ctor_feature(&qpath); hir::PatKind::Path(qpath) } PatKind::Struct(ref path, ref fields, etc) => { @@ -4039,7 +4036,6 @@ impl<'a> LoweringContext<'a> { ParamMode::Optional, ImplTraitContext::disallowed(), ); - self.check_self_struct_ctor_feature(&qpath); hir::ExprKind::Path(qpath) } ExprKind::Break(opt_label, ref opt_expr) => { @@ -5102,18 +5098,6 @@ impl<'a> LoweringContext<'a> { ThinVec::new())); P(self.expr_call(e.span, from_err, hir_vec![e])) } - - fn check_self_struct_ctor_feature(&self, qp: &hir::QPath) { - if let hir::QPath::Resolved(_, ref p) = qp { - if p.segments.len() == 1 && - p.segments[0].ident.name == keywords::SelfType.name() && - !self.sess.features_untracked().self_struct_ctor { - emit_feature_err(&self.sess.parse_sess, "self_struct_ctor", - p.span, GateIssue::Language, - "`Self` struct constructors are unstable"); - } - } - } } fn body_ids(bodies: &BTreeMap) -> Vec { diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 3bc349170514c..0d8f45433e4c7 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -475,9 +475,6 @@ declare_features! ( // Non-builtin attributes in inner attribute position (active, custom_inner_attributes, "1.30.0", Some(54726), None), - // Self struct constructor (RFC 2302) - (active, self_struct_ctor, "1.30.0", Some(51994), None), - // allow mixing of bind-by-move in patterns and references to // those identifiers in guards, *if* we are using MIR-borrowck // (aka NLL). Essentially this means you need to be on @@ -688,9 +685,11 @@ declare_features! ( (accepted, macro_literal_matcher, "1.31.0", Some(35625), None), // Use `?` as the Kleene "at most one" operator (accepted, macro_at_most_once_rep, "1.32.0", Some(48075), None), + // Self struct constructor (RFC 2302) + (accepted, self_struct_ctor, "1.32.0", Some(51994), None), ); -// If you change this, please modify src/doc/unstable-book as well. You must +// If you change this, please modify `src/doc/unstable-book` as well. You must // move that documentation into the relevant place in the other docs, and // remove the chapter on the flag. diff --git a/src/test/run-pass/rfcs/rfc-2302-self-struct-ctor.rs b/src/test/run-pass/rfcs/rfc-2302-self-struct-ctor.rs index 156e240e44282..1ec20c50034bd 100644 --- a/src/test/run-pass/rfcs/rfc-2302-self-struct-ctor.rs +++ b/src/test/run-pass/rfcs/rfc-2302-self-struct-ctor.rs @@ -1,7 +1,5 @@ // run-pass -#![feature(self_struct_ctor)] - #![allow(dead_code)] use std::fmt::Display; diff --git a/src/test/ui/feature-gates/feature-gate-self-struct-ctor.rs b/src/test/ui/feature-gates/feature-gate-self-struct-ctor.rs deleted file mode 100644 index 98eab39491320..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-self-struct-ctor.rs +++ /dev/null @@ -1,22 +0,0 @@ -struct ST1(i32, i32); - -impl ST1 { - fn ctor() -> Self { - Self(1,2) - //~^ ERROR: `Self` struct constructors are unstable (see issue #51994) [E0658] - } -} - -struct ST2; - -impl ST2 { - fn ctor() -> Self { - Self - //~^ ERROR: `Self` struct constructors are unstable (see issue #51994) [E0658] - } -} - -fn main() { - let _ = ST1::ctor(); - let _ = ST2::ctor(); -} diff --git a/src/test/ui/feature-gates/feature-gate-self-struct-ctor.stderr b/src/test/ui/feature-gates/feature-gate-self-struct-ctor.stderr deleted file mode 100644 index 6061a0db76ec5..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-self-struct-ctor.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/feature-gate-self-struct-ctor.rs:5:9 - | -LL | Self(1,2) - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/feature-gate-self-struct-ctor.rs:14:9 - | -LL | Self - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/issues/issue-56202.rs b/src/test/ui/issues/issue-56202.rs index 4b25f58c02ac8..bd222b7fe980e 100644 --- a/src/test/ui/issues/issue-56202.rs +++ b/src/test/ui/issues/issue-56202.rs @@ -1,7 +1,5 @@ // compile-pass -#![feature(self_struct_ctor)] - trait FooTrait {} trait BarTrait { diff --git a/src/test/ui/keyword/keyword-self-as-identifier.rs b/src/test/ui/keyword/keyword-self-as-identifier.rs index ad5b8fb64342d..b50fc68bed6be 100644 --- a/src/test/ui/keyword/keyword-self-as-identifier.rs +++ b/src/test/ui/keyword/keyword-self-as-identifier.rs @@ -10,5 +10,4 @@ fn main() { let Self = 22; //~ ERROR cannot find unit struct/variant or constant `Self` in this scope - //~^ ERROR `Self` struct constructors are unstable (see issue #51994) } diff --git a/src/test/ui/keyword/keyword-self-as-identifier.stderr b/src/test/ui/keyword/keyword-self-as-identifier.stderr index 296269819f836..dbb106e8d53a5 100644 --- a/src/test/ui/keyword/keyword-self-as-identifier.stderr +++ b/src/test/ui/keyword/keyword-self-as-identifier.stderr @@ -4,15 +4,7 @@ error[E0531]: cannot find unit struct/variant or constant `Self` in this scope LL | let Self = 22; //~ ERROR cannot find unit struct/variant or constant `Self` in this scope | ^^^^ not found in this scope -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/keyword-self-as-identifier.rs:12:9 - | -LL | let Self = 22; //~ ERROR cannot find unit struct/variant or constant `Self` in this scope - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error Some errors occurred: E0531, E0658. For more information about an error, try `rustc --explain E0531`. diff --git a/src/test/ui/self/self_type_keyword-2.rs b/src/test/ui/self/self_type_keyword-2.rs index bbaf060ca87e3..8331ae0b307d9 100644 --- a/src/test/ui/self/self_type_keyword-2.rs +++ b/src/test/ui/self/self_type_keyword-2.rs @@ -13,14 +13,11 @@ use self::Self as Foo; //~ ERROR unresolved import `self::Self` pub fn main() { let Self = 5; //~^ ERROR cannot find unit struct/variant or constant `Self` in this scope - //~^^ ERROR `Self` struct constructors are unstable (see issue #51994) match 15 { Self => (), //~^ ERROR cannot find unit struct/variant or constant `Self` in this scope - //~^^ ERROR `Self` struct constructors are unstable (see issue #51994) Foo { x: Self } => (), //~^ ERROR cannot find unit struct/variant or constant `Self` in this scope - //~^^ ERROR `Self` struct constructors are unstable (see issue #51994) } } diff --git a/src/test/ui/self/self_type_keyword-2.stderr b/src/test/ui/self/self_type_keyword-2.stderr index 82529974d0e37..3a2e9bb9c9c83 100644 --- a/src/test/ui/self/self_type_keyword-2.stderr +++ b/src/test/ui/self/self_type_keyword-2.stderr @@ -11,42 +11,18 @@ LL | let Self = 5; | ^^^^ not found in this scope error[E0531]: cannot find unit struct/variant or constant `Self` in this scope - --> $DIR/self_type_keyword-2.rs:19:9 + --> $DIR/self_type_keyword-2.rs:18:9 | LL | Self => (), | ^^^^ not found in this scope error[E0531]: cannot find unit struct/variant or constant `Self` in this scope - --> $DIR/self_type_keyword-2.rs:22:18 + --> $DIR/self_type_keyword-2.rs:20:18 | LL | Foo { x: Self } => (), | ^^^^ not found in this scope -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/self_type_keyword-2.rs:14:9 - | -LL | let Self = 5; - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/self_type_keyword-2.rs:19:9 - | -LL | Self => (), - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error[E0658]: `Self` struct constructors are unstable (see issue #51994) - --> $DIR/self_type_keyword-2.rs:22:18 - | -LL | Foo { x: Self } => (), - | ^^^^ - | - = help: add #![feature(self_struct_ctor)] to the crate attributes to enable - -error: aborting due to 7 previous errors +error: aborting due to 4 previous errors Some errors occurred: E0432, E0531, E0658. For more information about an error, try `rustc --explain E0432`. From 24717fdaa106ffe1cea7826bdaa8377e0268a183 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 30 Nov 2018 15:42:57 +0000 Subject: [PATCH 5/5] Updated ui tests. --- src/test/ui/keyword/keyword-self-as-identifier.stderr | 5 ++--- src/test/ui/self/self_type_keyword-2.stderr | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/ui/keyword/keyword-self-as-identifier.stderr b/src/test/ui/keyword/keyword-self-as-identifier.stderr index dbb106e8d53a5..c47f4aeabefd8 100644 --- a/src/test/ui/keyword/keyword-self-as-identifier.stderr +++ b/src/test/ui/keyword/keyword-self-as-identifier.stderr @@ -4,7 +4,6 @@ error[E0531]: cannot find unit struct/variant or constant `Self` in this scope LL | let Self = 22; //~ ERROR cannot find unit struct/variant or constant `Self` in this scope | ^^^^ not found in this scope -error: aborting due to 1 previous error +error: aborting due to previous error -Some errors occurred: E0531, E0658. -For more information about an error, try `rustc --explain E0531`. +For more information about this error, try `rustc --explain E0531`. diff --git a/src/test/ui/self/self_type_keyword-2.stderr b/src/test/ui/self/self_type_keyword-2.stderr index 3a2e9bb9c9c83..972e5bdddc673 100644 --- a/src/test/ui/self/self_type_keyword-2.stderr +++ b/src/test/ui/self/self_type_keyword-2.stderr @@ -24,5 +24,5 @@ LL | Foo { x: Self } => (), error: aborting due to 4 previous errors -Some errors occurred: E0432, E0531, E0658. +Some errors occurred: E0432, E0531. For more information about an error, try `rustc --explain E0432`.