Skip to content

Commit aad7547

Browse files
committed
auto merge of #18994 : sfackler/rust/struct-variants-pt2, r=jakub-
Struct variant field visibility is now inherited. Remove `pub` keywords from declarations. Closes #18641 [breaking-change] r? @alexcrichton
2 parents 321488b + 579c65d commit aad7547

File tree

65 files changed

+60
-114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+60
-114
lines changed

src/etc/generate-deriving-span-tests.py

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
3838
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
3939
40-
#![feature(struct_variant)]
4140
extern crate rand;
4241
4342
{error_deriving}

src/librustc/lint/builtin.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,9 @@ pub struct MissingDoc {
14021402
/// Stack of IDs of struct definitions.
14031403
struct_def_stack: Vec<ast::NodeId>,
14041404

1405+
/// True if inside variant definition
1406+
in_variant: bool,
1407+
14051408
/// Stack of whether #[doc(hidden)] is set
14061409
/// at each level which has lint attributes.
14071410
doc_hidden_stack: Vec<bool>,
@@ -1411,6 +1414,7 @@ impl MissingDoc {
14111414
pub fn new() -> MissingDoc {
14121415
MissingDoc {
14131416
struct_def_stack: vec!(),
1417+
in_variant: false,
14141418
doc_hidden_stack: vec!(false),
14151419
}
14161420
}
@@ -1525,7 +1529,7 @@ impl LintPass for MissingDoc {
15251529

15261530
fn check_struct_field(&mut self, cx: &Context, sf: &ast::StructField) {
15271531
match sf.node.kind {
1528-
ast::NamedField(_, vis) if vis == ast::Public => {
1532+
ast::NamedField(_, vis) if vis == ast::Public || self.in_variant => {
15291533
let cur_struct_def = *self.struct_def_stack.last()
15301534
.expect("empty struct_def_stack");
15311535
self.check_missing_docs_attrs(cx, Some(cur_struct_def),
@@ -1539,6 +1543,13 @@ impl LintPass for MissingDoc {
15391543
fn check_variant(&mut self, cx: &Context, v: &ast::Variant, _: &ast::Generics) {
15401544
self.check_missing_docs_attrs(cx, Some(v.node.id), v.node.attrs.as_slice(),
15411545
v.span, "a variant");
1546+
assert!(!self.in_variant);
1547+
self.in_variant = true;
1548+
}
1549+
1550+
fn check_variant_post(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) {
1551+
assert!(self.in_variant);
1552+
self.in_variant = false;
15421553
}
15431554
}
15441555

src/librustc/lint/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
665665
self.with_lint_attrs(v.node.attrs.as_slice(), |cx| {
666666
run_lints!(cx, check_variant, v, g);
667667
visit::walk_variant(cx, v, g);
668+
run_lints!(cx, check_variant_post, v, g);
668669
})
669670
}
670671

src/librustc/lint/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ pub trait LintPass {
149149
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
150150
fn check_struct_field(&mut self, _: &Context, _: &ast::StructField) { }
151151
fn check_variant(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
152+
fn check_variant_post(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
152153
fn check_opt_lifetime_ref(&mut self, _: &Context, _: Span, _: &Option<ast::Lifetime>) { }
153154
fn check_lifetime_ref(&mut self, _: &Context, _: &ast::Lifetime) { }
154155
fn check_lifetime_decl(&mut self, _: &Context, _: &ast::LifetimeDef) { }

src/librustc/middle/privacy.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ struct VisiblePrivateTypesVisitor<'a, 'tcx: 'a> {
12391239
tcx: &'a ty::ctxt<'tcx>,
12401240
exported_items: &'a ExportedItems,
12411241
public_items: &'a PublicItems,
1242+
in_variant: bool,
12421243
}
12431244

12441245
struct CheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
@@ -1514,13 +1515,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
15141515

15151516
fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) {
15161517
if self.exported_items.contains(&v.node.id) {
1518+
self.in_variant = true;
15171519
visit::walk_variant(self, v, g);
1520+
self.in_variant = false;
15181521
}
15191522
}
15201523

15211524
fn visit_struct_field(&mut self, s: &ast::StructField) {
15221525
match s.node.kind {
1523-
ast::NamedField(_, ast::Public) => {
1526+
ast::NamedField(_, vis) if vis == ast::Public || self.in_variant => {
15241527
visit::walk_struct_field(self, s);
15251528
}
15261529
_ => {}
@@ -1598,7 +1601,8 @@ pub fn check_crate(tcx: &ty::ctxt,
15981601
let mut visitor = VisiblePrivateTypesVisitor {
15991602
tcx: tcx,
16001603
exported_items: &exported_items,
1601-
public_items: &public_items
1604+
public_items: &public_items,
1605+
in_variant: false,
16021606
};
16031607
visit::walk_crate(&mut visitor, krate);
16041608
}

src/librustc/middle/resolve.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ pub enum LastPrivate {
102102
// and whether the import is in fact used for each.
103103
// If the Option<PrivateDep> fields are None, it means there is no definition
104104
// in that namespace.
105-
LastImport{pub value_priv: Option<PrivateDep>,
106-
pub value_used: ImportUse,
107-
pub type_priv: Option<PrivateDep>,
108-
pub type_used: ImportUse},
105+
LastImport{value_priv: Option<PrivateDep>,
106+
value_used: ImportUse,
107+
type_priv: Option<PrivateDep>,
108+
type_used: ImportUse},
109109
}
110110

111111
#[deriving(Show)]

src/librustc/middle/trans/adt.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ pub enum Repr {
101101
* otherwise it indicates the other case.
102102
*/
103103
RawNullablePointer {
104-
pub nndiscr: Disr,
105-
pub nnty: ty::t,
106-
pub nullfields: Vec<ty::t>
104+
nndiscr: Disr,
105+
nnty: ty::t,
106+
nullfields: Vec<ty::t>
107107
},
108108
/**
109109
* Two cases distinguished by a nullable pointer: the case with discriminant
@@ -117,10 +117,10 @@ pub enum Repr {
117117
* identity function.
118118
*/
119119
StructWrappedNullablePointer {
120-
pub nonnull: Struct,
121-
pub nndiscr: Disr,
122-
pub ptrfield: PointerField,
123-
pub nullfields: Vec<ty::t>,
120+
nonnull: Struct,
121+
nndiscr: Disr,
122+
ptrfield: PointerField,
123+
nullfields: Vec<ty::t>,
124124
}
125125
}
126126

src/librustdoc/clean/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1080,9 +1080,9 @@ impl Clean<Item> for ty::ImplOrTraitItem {
10801080
pub enum Type {
10811081
/// structs/enums/traits (anything that'd be an ast::TyPath)
10821082
ResolvedPath {
1083-
pub path: Path,
1084-
pub typarams: Option<Vec<TyParamBound>>,
1085-
pub did: ast::DefId,
1083+
path: Path,
1084+
typarams: Option<Vec<TyParamBound>>,
1085+
did: ast::DefId,
10861086
},
10871087
// I have no idea how to usefully use this.
10881088
TyParamBinder(ast::NodeId),
@@ -1105,9 +1105,9 @@ pub enum Type {
11051105
Unique(Box<Type>),
11061106
RawPointer(Mutability, Box<Type>),
11071107
BorrowedRef {
1108-
pub lifetime: Option<Lifetime>,
1109-
pub mutability: Mutability,
1110-
pub type_: Box<Type>,
1108+
lifetime: Option<Lifetime>,
1109+
mutability: Mutability,
1110+
type_: Box<Type>,
11111111
},
11121112
// region, raw, other boxes, mutable
11131113
}

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1293,8 +1293,8 @@ pub type Variant = Spanned<Variant_>;
12931293

12941294
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
12951295
pub enum PathListItem_ {
1296-
PathListIdent { pub name: Ident, pub id: NodeId },
1297-
PathListMod { pub id: NodeId }
1296+
PathListIdent { name: Ident, id: NodeId },
1297+
PathListMod { id: NodeId }
12981298
}
12991299

13001300
impl PathListItem_ {

src/libsyntax/feature_gate.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::slice;
3737
static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
3838
("globs", Active),
3939
("macro_rules", Active),
40-
("struct_variant", Active),
40+
("struct_variant", Accepted),
4141
("asm", Active),
4242
("managed_boxes", Removed),
4343
("non_ascii_idents", Active),
@@ -184,19 +184,6 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
184184
}
185185
}
186186
match i.node {
187-
ast::ItemEnum(ref def, _) => {
188-
for variant in def.variants.iter() {
189-
match variant.node.kind {
190-
ast::StructVariantKind(..) => {
191-
self.gate_feature("struct_variant", variant.span,
192-
"enum struct variants are \
193-
experimental and possibly buggy");
194-
}
195-
_ => {}
196-
}
197-
}
198-
}
199-
200187
ast::ItemForeignMod(ref foreign_module) => {
201188
if attr::contains_name(i.attrs.as_slice(), "link_args") {
202189
self.gate_feature("link_args", i.span,

src/libsyntax/parse/parser.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -4612,7 +4612,7 @@ impl<'a> Parser<'a> {
46124612
is_tuple_like = false;
46134613
fields = Vec::new();
46144614
while self.token != token::CloseDelim(token::Brace) {
4615-
fields.push(self.parse_struct_decl_field());
4615+
fields.push(self.parse_struct_decl_field(true));
46164616
}
46174617
if fields.len() == 0 {
46184618
self.fatal(format!("unit-like struct definition should be \
@@ -4689,12 +4689,16 @@ impl<'a> Parser<'a> {
46894689
}
46904690

46914691
/// Parse an element of a struct definition
4692-
fn parse_struct_decl_field(&mut self) -> StructField {
4692+
fn parse_struct_decl_field(&mut self, allow_pub: bool) -> StructField {
46934693

46944694
let attrs = self.parse_outer_attributes();
46954695

46964696
if self.eat_keyword(keywords::Pub) {
4697-
return self.parse_single_struct_field(Public, attrs);
4697+
if !allow_pub {
4698+
let span = self.last_span;
4699+
self.span_err(span, "`pub` is not allowed here");
4700+
}
4701+
return self.parse_single_struct_field(Public, attrs);
46984702
}
46994703

47004704
return self.parse_single_struct_field(Inherited, attrs);
@@ -5142,7 +5146,7 @@ impl<'a> Parser<'a> {
51425146
fn parse_struct_def(&mut self) -> P<StructDef> {
51435147
let mut fields: Vec<StructField> = Vec::new();
51445148
while self.token != token::CloseDelim(token::Brace) {
5145-
fields.push(self.parse_struct_decl_field());
5149+
fields.push(self.parse_struct_decl_field(false));
51465150
}
51475151
self.bump();
51485152

src/test/compile-fail/deriving-primitive.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(struct_variant)]
12-
1311
use std::num::FromPrimitive;
1412
use std::int;
1513

src/test/compile-fail/deriving-span-Clone-enum-struct-variant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Clone-enum.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Clone-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Clone-tuple-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Default-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Default-tuple-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Hash-enum-struct-variant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Hash-enum.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Hash-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-Hash-tuple-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-PartialEq-enum-struct-variant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-PartialEq-enum.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-PartialEq-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-PartialEq-tuple-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615

src/test/compile-fail/deriving-span-PartialOrd-enum-struct-variant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615
#[deriving(PartialEq)]

src/test/compile-fail/deriving-span-PartialOrd-enum.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615
#[deriving(PartialEq)]

src/test/compile-fail/deriving-span-PartialOrd-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615
#[deriving(PartialEq)]

src/test/compile-fail/deriving-span-PartialOrd-tuple-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
1212

13-
#![feature(struct_variant)]
1413
extern crate rand;
1514

1615
#[deriving(PartialEq)]

0 commit comments

Comments
 (0)