Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RFC 2302: tuple_struct_self_ctor #53751

Merged
merged 2 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/doc/unstable-book/src/language-features/self-struct-ctor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# `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
}
}
}
```
5 changes: 4 additions & 1 deletion src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum Def {
Static(DefId, bool /* is_mutbl */),
StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
VariantCtor(DefId, CtorKind), // DefId refers to the enum variant
SelfCtor(DefId /* impl */), // DefId refers to the impl
Method(DefId),
AssociatedConst(DefId),

Expand Down Expand Up @@ -272,7 +273,8 @@ impl Def {
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
Def::AssociatedConst(id) | Def::Macro(id, ..) |
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) |
Def::SelfCtor(id) => {
id
}

Expand Down Expand Up @@ -309,6 +311,7 @@ impl Def {
Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
Def::StructCtor(.., CtorKind::Const) => "unit struct",
Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
Def::SelfCtor(..) => "self constructor",
Def::Union(..) => "union",
Def::Trait(..) => "trait",
Def::ForeignTy(..) => "foreign type",
Expand Down
30 changes: 26 additions & 4 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ 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};
Expand Down Expand Up @@ -3429,19 +3430,24 @@ 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(),
ddpos,
)
}
PatKind::Path(ref qself, ref path) => hir::PatKind::Path(self.lower_qpath(
PatKind::Path(ref qself, ref path) => {
let qpath = self.lower_qpath(
p.id,
qself,
path,
ParamMode::Optional,
ImplTraitContext::Disallowed,
)),
);
self.check_self_struct_ctor_feature(&qpath);
hir::PatKind::Path(qpath)
}
PatKind::Struct(ref path, ref fields, etc) => {
let qpath = self.lower_qpath(
p.id,
Expand Down Expand Up @@ -3828,13 +3834,17 @@ impl<'a> LoweringContext<'a> {
attrs: e.attrs.clone(),
};
}
ExprKind::Path(ref qself, ref path) => hir::ExprKind::Path(self.lower_qpath(
ExprKind::Path(ref qself, ref path) => {
let qpath = self.lower_qpath(
e.id,
qself,
path,
ParamMode::Optional,
ImplTraitContext::Disallowed,
)),
);
self.check_self_struct_ctor_feature(&qpath);
hir::ExprKind::Path(qpath)
}
ExprKind::Break(opt_label, ref opt_expr) => {
let destination = if self.is_in_loop_condition && opt_label.is_none() {
hir::Destination {
Expand Down Expand Up @@ -4815,6 +4825,18 @@ 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<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ impl_stable_hash_for!(enum hir::def::Def {
Const(def_id),
Static(def_id, is_mutbl),
StructCtor(def_id, ctor_kind),
SelfCtor(impl_def_id),
VariantCtor(def_id, ctor_kind),
Method(def_id),
AssociatedConst(def_id),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {

match def {
Def::StructCtor(..) | Def::VariantCtor(..) | Def::Const(..) |
Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) => {
Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) | Def::SelfCtor(..) => {
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
}

Expand Down Expand Up @@ -1288,7 +1288,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
(self.cat_downcast_if_needed(pat, cmt, def_id),
self.tcx.adt_def(enum_def).variant_with_id(def_id).fields.len())
}
Def::StructCtor(_, CtorKind::Fn) => {
Def::StructCtor(_, CtorKind::Fn) | Def::SelfCtor(..) => {
match self.pat_ty_unadjusted(&pat)?.sty {
ty::Adt(adt_def, _) => {
(cmt, adt_def.non_enum_variant().fields.len())
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
fn visit_path(&mut self, path: &'tcx hir::Path, id: hir::HirId) {
let id = self.tcx.hir.hir_to_node_id(id);
match path.def {
Def::Local(..) | Def::Upvar(..) |
Def::Local(..) | Def::Upvar(..) | Def::SelfCtor(..) |
Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => {}
_ => self.tcx.check_stability(path.def.def_id(), Some(id), path.span)
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
match def {
Def::Variant(vid) | Def::VariantCtor(vid, ..) => self.variant_with_id(vid),
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => self.non_enum_variant(),
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) |
Def::SelfCtor(..) => self.non_enum_variant(),
_ => bug!("unexpected def {:?} in variant_of_def", def)
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
Def::VariantCtor(variant_id, CtorKind::Fn) => {
Some((adt_def, adt_def.variant_index_with_id(variant_id)))
}
Def::StructCtor(_, CtorKind::Fn) => Some((adt_def, 0)),
Def::StructCtor(_, CtorKind::Fn) |
Def::SelfCtor(..) => Some((adt_def, 0)),
varkor marked this conversation as resolved.
Show resolved Hide resolved
_ => None,
}
})
Expand Down Expand Up @@ -759,6 +760,25 @@ fn user_annotated_ty_for_def(
sty => bug!("unexpected sty: {:?}", sty),
},

// `Self` is used in expression as a tuple struct constructor or an unit struct constructor
Def::SelfCtor(_) => {
let sty = &cx.tables().node_id_to_type(hir_id).sty;
match sty {
ty::FnDef(ref def_id, _) => {
Some(cx.tables().user_substs(hir_id)?.unchecked_map(|user_substs| {
// Here, we just pair a `DefId` with the
// `user_substs`, so no new types etc are introduced.
cx.tcx().mk_fn_def(*def_id, user_substs)
}))
}
ty::Adt(ref adt_def, _) => {
user_annotated_ty_for_adt(cx, hir_id, adt_def)
}
_ => {
bug!("unexpected sty: {:?}", sty)
}
}
}
_ =>
bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
}
Expand Down Expand Up @@ -857,7 +877,8 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
Def::Fn(_) |
Def::Method(_) |
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => {
Def::VariantCtor(_, CtorKind::Fn) |
Def::SelfCtor(..) => {
let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, &def);
ExprKind::Literal {
literal: ty::Const::zero_sized(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
}

Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => {
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) | Def::SelfCtor(..) => {
PatternKind::Leaf { subpatterns: subpatterns }
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustc_passes/rvalue_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ fn check_expr_kind<'a, 'tcx>(
let def = v.tables.qpath_def(qpath, e.hir_id);
match def {
Def::VariantCtor(..) | Def::StructCtor(..) |
Def::Fn(..) | Def::Method(..) => Promotable,
Def::Fn(..) | Def::Method(..) | Def::SelfCtor(..) => Promotable,

// References to a static that are themselves within a static
// are inherently promotable with the exception
Expand Down Expand Up @@ -441,7 +441,8 @@ fn check_expr_kind<'a, 'tcx>(
};
let def_result = match def {
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => Promotable,
Def::VariantCtor(_, CtorKind::Fn) |
Def::SelfCtor(..) => Promotable,
Def::Fn(did) => {
v.handle_const_fn_call(did, node_ty, e.span)
}
Expand Down
47 changes: 34 additions & 13 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,17 +558,21 @@ impl<'a> PathSource<'a> {
Def::StructCtor(_, CtorKind::Const) | Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Const) | Def::VariantCtor(_, CtorKind::Fn) |
Def::Const(..) | Def::Static(..) | Def::Local(..) | Def::Upvar(..) |
Def::Fn(..) | Def::Method(..) | Def::AssociatedConst(..) => true,
Def::Fn(..) | Def::Method(..) | Def::AssociatedConst(..) |
Def::SelfCtor(..) => true,
_ => false,
},
PathSource::Pat => match def {
Def::StructCtor(_, CtorKind::Const) |
Def::VariantCtor(_, CtorKind::Const) |
Def::Const(..) | Def::AssociatedConst(..) => true,
Def::Const(..) | Def::AssociatedConst(..) |
Def::SelfCtor(..) => true,
_ => false,
},
PathSource::TupleStruct => match def {
Def::StructCtor(_, CtorKind::Fn) | Def::VariantCtor(_, CtorKind::Fn) => true,
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) |
Def::SelfCtor(..) => true,
_ => false,
},
PathSource::Struct => match def {
Expand Down Expand Up @@ -2204,6 +2208,19 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
None
}

fn resolve_adt(&mut self, item: &Item, generics: &Generics) {
self.with_type_parameter_rib(HasTypeParameters(generics, ItemRibKind), |this| {
let item_def_id = this.definitions.local_def_id(item.id);
if this.session.features_untracked().self_in_typedefs {
this.with_self_rib(Def::SelfTy(None, Some(item_def_id)), |this| {
visit::walk_item(this, item);
});
} else {
visit::walk_item(this, item);
}
});
}

fn resolve_item(&mut self, item: &Item) {
let name = item.ident.name;
debug!("(resolving item) resolving {}", name);
Expand All @@ -2219,16 +2236,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
ItemKind::Enum(_, ref generics) |
ItemKind::Struct(_, ref generics) |
ItemKind::Union(_, ref generics) => {
self.with_type_parameter_rib(HasTypeParameters(generics, ItemRibKind), |this| {
let item_def_id = this.definitions.local_def_id(item.id);
if this.session.features_untracked().self_in_typedefs {
this.with_self_rib(Def::SelfTy(None, Some(item_def_id)), |this| {
visit::walk_item(this, item);
});
} else {
visit::walk_item(this, item);
}
});
self.resolve_adt(item, generics);
}

ItemKind::Impl(.., ref generics, ref opt_trait_ref, ref self_type, ref impl_items) =>
Expand Down Expand Up @@ -2503,6 +2511,17 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
self.ribs[TypeNS].pop();
}

fn with_self_struct_ctor_rib<F>(&mut self, impl_id: DefId, f: F)
where F: FnOnce(&mut Resolver)
{
let self_def = Def::SelfCtor(impl_id);
let mut self_type_rib = Rib::new(NormalRibKind);
self_type_rib.bindings.insert(keywords::SelfType.ident(), self_def);
self.ribs[ValueNS].push(self_type_rib);
f(self);
self.ribs[ValueNS].pop();
}

fn resolve_implementation(&mut self,
generics: &Generics,
opt_trait_reference: &Option<TraitRef>,
Expand All @@ -2527,6 +2546,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
this.visit_generics(generics);
// Resolve the items within the impl.
this.with_current_self_type(self_type, |this| {
this.with_self_struct_ctor_rib(item_def_id, |this| {
for impl_item in impl_items {
this.resolve_visibility(&impl_item.vis);

Expand Down Expand Up @@ -2589,6 +2609,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
});
});
});
});
}

fn check_trait_item<F>(&mut self, ident: Ident, ns: Namespace, span: Span, err: F)
Expand Down
1 change: 1 addition & 0 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
HirDef::Macro(..) |
HirDef::ToolMod |
HirDef::NonMacroAttr(..) |
HirDef::SelfCtor(..) |
HirDef::Err => None,
}
}
Expand Down
Loading