-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Parse unnamed struct and union fields #99754
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,8 @@ | |
#![feature(let_chains)] | ||
#![feature(never_type)] | ||
#![recursion_limit = "256"] | ||
#![deny(rustc::untranslatable_diagnostic)] | ||
#![deny(rustc::diagnostic_outside_of_impl)] | ||
// #![deny(rustc::untranslatable_diagnostic)] | ||
// #![deny(rustc::diagnostic_outside_of_impl)] | ||
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. Putting a comment so we don't forget about this. |
||
|
||
#[macro_use] | ||
extern crate tracing; | ||
|
@@ -1237,6 +1237,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { | |
let kind = match &t.kind { | ||
TyKind::Infer => hir::TyKind::Infer, | ||
TyKind::Err => hir::TyKind::Err, | ||
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS | ||
TyKind::AnonymousStruct(ref _fields, _recovered) => { | ||
self.tcx.sess.struct_span_err(t.span, "anonymous structs are unimplemented").emit(); | ||
hir::TyKind::Err | ||
} | ||
TyKind::AnonymousUnion(ref _fields, _recovered) => { | ||
self.tcx.sess.struct_span_err(t.span, "anonymous unions are unimplemented").emit(); | ||
hir::TyKind::Err | ||
} | ||
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)), | ||
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)), | ||
TyKind::Ref(region, mt) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,10 +243,30 @@ impl<'a> AstValidator<'a> { | |
} | ||
} | ||
} | ||
TyKind::AnonymousStruct(ref fields, ..) | TyKind::AnonymousUnion(ref fields, ..) => { | ||
self.with_banned_assoc_ty_bound(|this| { | ||
walk_list!(this, visit_struct_field_def, fields) | ||
}); | ||
} | ||
_ => visit::walk_ty(self, t), | ||
} | ||
} | ||
|
||
fn visit_struct_field_def(&mut self, field: &'a FieldDef) { | ||
if let Some(ident) = field.ident { | ||
if ident.name == kw::Underscore { | ||
self.check_anonymous_field(field); | ||
self.visit_vis(&field.vis); | ||
self.visit_ident(ident); | ||
self.visit_ty_common(&field.ty); | ||
self.walk_ty(&field.ty); | ||
walk_list!(self, visit_attribute, &field.attrs); | ||
return; | ||
} | ||
} | ||
self.visit_field_def(field); | ||
} | ||
|
||
fn err_handler(&self) -> &rustc_errors::Handler { | ||
&self.session.diagnostic() | ||
} | ||
|
@@ -288,6 +308,66 @@ impl<'a> AstValidator<'a> { | |
} | ||
} | ||
|
||
fn check_anonymous_field(&self, field: &FieldDef) { | ||
let FieldDef { ty, .. } = field; | ||
match &ty.kind { | ||
TyKind::AnonymousStruct(..) | TyKind::AnonymousUnion(..) => { | ||
// We already checked for `kw::Underscore` before calling this function, | ||
// so skip the check | ||
} | ||
TyKind::Path(..) => { | ||
// If the anonymous field contains a Path as type, we can't determine | ||
// if the path is a valid struct or union, so skip the check | ||
} | ||
_ => { | ||
let msg = "unnamed fields can only have struct or union types"; | ||
let label = "not a struct or union"; | ||
self.err_handler() | ||
.struct_span_err(field.span, msg) | ||
.span_label(ty.span, label) | ||
.emit(); | ||
} | ||
} | ||
} | ||
|
||
fn deny_anonymous_struct(&self, ty: &Ty) { | ||
match &ty.kind { | ||
TyKind::AnonymousStruct(..) => { | ||
self.err_handler() | ||
.struct_span_err( | ||
ty.span, | ||
"anonymous structs are not allowed outside of unnamed struct or union fields", | ||
) | ||
.span_label(ty.span, "anonymous struct declared here") | ||
.emit(); | ||
} | ||
TyKind::AnonymousUnion(..) => { | ||
self.err_handler() | ||
.struct_span_err( | ||
ty.span, | ||
"anonymous unions are not allowed outside of unnamed struct or union fields", | ||
) | ||
.span_label(ty.span, "anonymous union declared here") | ||
.emit(); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn deny_anonymous_field(&self, field: &FieldDef) { | ||
if let Some(ident) = field.ident { | ||
if ident.name == kw::Underscore { | ||
self.err_handler() | ||
.struct_span_err( | ||
field.span, | ||
"anonymous fields are not allowed outside of structs or unions", | ||
) | ||
.span_label(ident.span, "anonymous field declared here") | ||
.emit(); | ||
} | ||
} | ||
} | ||
|
||
fn check_trait_fn_not_const(&self, constness: Const) { | ||
if let Const::Yes(span) = constness { | ||
self.session.emit_err(TraitFnConst { span }); | ||
|
@@ -974,6 +1054,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
|
||
fn visit_ty(&mut self, ty: &'a Ty) { | ||
self.visit_ty_common(ty); | ||
self.deny_anonymous_struct(ty); | ||
self.walk_ty(ty) | ||
} | ||
|
||
|
@@ -988,6 +1069,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
} | ||
|
||
fn visit_field_def(&mut self, field: &'a FieldDef) { | ||
self.deny_anonymous_field(field); | ||
visit::walk_field_def(self, field) | ||
} | ||
|
||
|
@@ -1179,10 +1261,42 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
self.check_mod_file_item_asciionly(item.ident); | ||
} | ||
} | ||
ItemKind::Union(vdata, ..) => { | ||
ItemKind::Struct(vdata, generics) => match vdata { | ||
// Duplicating the `Visitor` logic allows catching all cases | ||
// of `Anonymous(Struct, Union)` outside of a field struct or union. | ||
// | ||
// Inside `visit_ty` the validator catches every `Anonymous(Struct, Union)` it | ||
// encounters, and only on `ItemKind::Struct` and `ItemKind::Union` | ||
// it uses `visit_ty_common`, which doesn't contain that specific check. | ||
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'm not sure we need this duplication. |
||
VariantData::Struct(fields, ..) => { | ||
self.visit_vis(&item.vis); | ||
self.visit_ident(item.ident); | ||
self.visit_generics(generics); | ||
self.with_banned_assoc_ty_bound(|this| { | ||
walk_list!(this, visit_struct_field_def, fields); | ||
}); | ||
walk_list!(self, visit_attribute, &item.attrs); | ||
return; | ||
} | ||
_ => {} | ||
}, | ||
ItemKind::Union(vdata, generics) => { | ||
if vdata.fields().is_empty() { | ||
self.err_handler().span_err(item.span, "unions cannot have zero fields"); | ||
} | ||
match vdata { | ||
VariantData::Struct(fields, ..) => { | ||
self.visit_vis(&item.vis); | ||
self.visit_ident(item.ident); | ||
self.visit_generics(generics); | ||
self.with_banned_assoc_ty_bound(|this| { | ||
walk_list!(this, visit_struct_field_def, fields); | ||
}); | ||
walk_list!(self, visit_attribute, &item.attrs); | ||
return; | ||
} | ||
_ => {} | ||
} | ||
} | ||
ItemKind::Const(def, .., None) => { | ||
self.check_defaultness(item.span, *def); | ||
|
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.
Please explicitly ignore fields in visitors, so we have a compiler error when we change them.