-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Refactoring/bugfixing around definitions for struct/variant constructors #36814
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da7b1c9
Separate Def::StructCtor/Def::VariantCtor from Def::Struct/Def::Variant
petrochenkov d19c16a
Fix cross-crate resolution of half-items created by export shadowing
petrochenkov e8ea38e
Further cleanup in resolve
petrochenkov c95b280
Move pattern resolution checks from typeck to resolve
petrochenkov 64bdf1b
Set `NON_ZERO_SIZED` flag correctly for struct/union ctors
petrochenkov 75d6522
Eliminate ty::VariantKind in favor of def::CtorKind
petrochenkov bd291ce
Turn some impossible definitions into ICEs
petrochenkov bc0eabd
Remove some unused methods from metadata
petrochenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,34 +13,46 @@ use util::nodemap::NodeMap; | |
use syntax::ast; | ||
use hir; | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] | ||
pub enum CtorKind { | ||
// Constructor function automatically created by a tuple struct/variant. | ||
Fn, | ||
// Constructor constant automatically created by a unit struct/variant. | ||
Const, | ||
// Unusable name in value namespace created by a struct variant. | ||
Fictive, | ||
} | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] | ||
pub enum Def { | ||
Fn(DefId), | ||
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */), | ||
// Type namespace | ||
Mod(DefId), | ||
Static(DefId, bool /* is_mutbl */), | ||
Const(DefId), | ||
AssociatedConst(DefId), | ||
Local(DefId), | ||
Variant(DefId), | ||
Struct(DefId), // DefId refers to NodeId of the struct itself | ||
Union(DefId), | ||
Enum(DefId), | ||
Variant(DefId), | ||
Trait(DefId), | ||
TyAlias(DefId), | ||
AssociatedTy(DefId), | ||
Trait(DefId), | ||
PrimTy(hir::PrimTy), | ||
TyParam(DefId), | ||
Upvar(DefId, // def id of closed over local | ||
usize, // index in the freevars list of the closure | ||
ast::NodeId), // expr node that creates the closure | ||
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */), | ||
|
||
// If Def::Struct lives in type namespace it denotes a struct item and its DefId refers | ||
// to NodeId of the struct itself. | ||
// If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions) | ||
// it denotes a constructor and its DefId refers to NodeId of the struct's constructor. | ||
Struct(DefId), | ||
Union(DefId), | ||
Label(ast::NodeId), | ||
// Value namespace | ||
Fn(DefId), | ||
Const(DefId), | ||
Static(DefId, bool /* is_mutbl */), | ||
StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor | ||
VariantCtor(DefId, CtorKind), | ||
Method(DefId), | ||
AssociatedConst(DefId), | ||
Local(DefId), | ||
Upvar(DefId, // def id of closed over local | ||
usize, // index in the freevars list of the closure | ||
ast::NodeId), // expr node that creates the closure | ||
Label(ast::NodeId), | ||
|
||
// Both namespaces | ||
Err, | ||
} | ||
|
||
|
@@ -93,18 +105,35 @@ pub type ExportMap = NodeMap<Vec<Export>>; | |
|
||
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)] | ||
pub struct Export { | ||
pub name: ast::Name, // The name of the target. | ||
pub def_id: DefId, // The definition of the target. | ||
pub name: ast::Name, // The name of the target. | ||
pub def: Def, // The definition of the target. | ||
} | ||
|
||
impl CtorKind { | ||
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind { | ||
match *vdata { | ||
ast::VariantData::Tuple(..) => CtorKind::Fn, | ||
ast::VariantData::Unit(..) => CtorKind::Const, | ||
ast::VariantData::Struct(..) => CtorKind::Fictive, | ||
} | ||
} | ||
pub fn from_hir(vdata: &hir::VariantData) -> CtorKind { | ||
match *vdata { | ||
hir::VariantData::Tuple(..) => CtorKind::Fn, | ||
hir::VariantData::Unit(..) => CtorKind::Const, | ||
hir::VariantData::Struct(..) => CtorKind::Fictive, | ||
} | ||
} | ||
} | ||
|
||
impl Def { | ||
pub fn def_id(&self) -> DefId { | ||
match *self { | ||
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) | | ||
Def::Variant(id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(id) | | ||
Def::TyParam(id) | Def::Struct(id) | Def::Union(id) | Def::Trait(id) | | ||
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) | | ||
Def::Local(id) | Def::Upvar(id, ..) => { | ||
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) | | ||
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::Local(id) | Def::Upvar(id, ..) => { | ||
id | ||
} | ||
|
||
|
@@ -123,10 +152,16 @@ impl Def { | |
Def::Mod(..) => "module", | ||
Def::Static(..) => "static", | ||
Def::Variant(..) => "variant", | ||
Def::VariantCtor(.., CtorKind::Fn) => "tuple variant", | ||
Def::VariantCtor(.., CtorKind::Const) => "unit variant", | ||
Def::VariantCtor(.., CtorKind::Fictive) => "struct variant", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with more traditional, but less precise descriptions. |
||
Def::Enum(..) => "enum", | ||
Def::TyAlias(..) => "type", | ||
Def::TyAlias(..) => "type alias", | ||
Def::AssociatedTy(..) => "associated type", | ||
Def::Struct(..) => "struct", | ||
Def::StructCtor(.., CtorKind::Fn) => "tuple struct", | ||
Def::StructCtor(.., CtorKind::Const) => "unit struct", | ||
Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"), | ||
Def::Union(..) => "union", | ||
Def::Trait(..) => "trait", | ||
Def::Method(..) => "method", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DefId
alone is not enough for describing a reexport precisely.Variants can be exported separately in type and value namespaces, but have a single
DefId
(andNodeId
).