Skip to content

Commit f433209

Browse files
authored
Rollup merge of rust-lang#36868 - petrochenkov:adtstab, r=nikomatsakis
Partially stabilize RFC 1506 "Clarify relationships between ADTs" Lifted restrictions on tuple structs/variants are stabilized, i.e. `S{..}` can be used with any structs and empty tuple structs are permitted without feature gate. Numeric fields in struct expressions/patterns `S { 0: a, 1: b }` are **NOT** stabilized. This was implemented 1.5 months ago in Rust 1.12, but this is a tiny technical change that could probably go even without RFC/stabilization period. cc rust-lang#35626 rust-lang#36871 r? @nikomatsakis
2 parents 02aa428 + ab5ba04 commit f433209

19 files changed

+23
-115
lines changed

src/librustc_typeck/check/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -3261,13 +3261,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32613261
};
32623262

32633263
if let Some((variant, did, substs)) = variant {
3264-
if variant.ctor_kind == CtorKind::Fn &&
3265-
!self.tcx.sess.features.borrow().relaxed_adts {
3266-
emit_feature_err(&self.tcx.sess.parse_sess,
3267-
"relaxed_adts", path.span, GateIssue::Language,
3268-
"tuple structs and variants in struct patterns are unstable");
3269-
}
3270-
32713264
// Check bounds on type arguments used in the path.
32723265
let type_predicates = self.tcx.lookup_predicates(did);
32733266
let bounds = self.instantiate_bounds(path.span, substs, &type_predicates);

src/libsyntax/feature_gate.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ declare_features! (
271271
// Allows `impl Trait` in function return types.
272272
(active, conservative_impl_trait, "1.12.0", Some(34511)),
273273

274-
// Allows tuple structs and variants in more contexts,
275274
// Permits numeric fields in struct expressions and patterns.
276275
(active, relaxed_adts, "1.12.0", Some(35626)),
277276

@@ -996,6 +995,10 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
996995
}
997996
}
998997

998+
fn starts_with_digit(s: &str) -> bool {
999+
s.as_bytes().first().cloned().map_or(false, |b| b >= b'0' && b <= b'9')
1000+
}
1001+
9991002
impl<'a> Visitor for PostExpansionVisitor<'a> {
10001003
fn visit_attribute(&mut self, attr: &ast::Attribute) {
10011004
if !self.context.cm.span_allows_unstable(attr.span) {
@@ -1175,6 +1178,11 @@ impl<'a> Visitor for PostExpansionVisitor<'a> {
11751178
gate_feature_post!(&self, field_init_shorthand, field.span,
11761179
"struct field shorthands are unstable");
11771180
}
1181+
if starts_with_digit(&field.ident.node.name.as_str()) {
1182+
gate_feature_post!(&self, relaxed_adts,
1183+
field.span,
1184+
"numeric fields in struct expressions are unstable");
1185+
}
11781186
}
11791187
}
11801188
_ => {}
@@ -1201,10 +1209,14 @@ impl<'a> Visitor for PostExpansionVisitor<'a> {
12011209
pattern.span,
12021210
"box pattern syntax is experimental");
12031211
}
1204-
PatKind::TupleStruct(_, ref fields, ddpos)
1205-
if ddpos.is_none() && fields.is_empty() => {
1206-
gate_feature_post!(&self, relaxed_adts, pattern.span,
1207-
"empty tuple structs patterns are unstable");
1212+
PatKind::Struct(_, ref fields, _) => {
1213+
for field in fields {
1214+
if starts_with_digit(&field.node.ident.name.as_str()) {
1215+
gate_feature_post!(&self, relaxed_adts,
1216+
field.span,
1217+
"numeric fields in struct patterns are unstable");
1218+
}
1219+
}
12081220
}
12091221
_ => {}
12101222
}
@@ -1287,19 +1299,6 @@ impl<'a> Visitor for PostExpansionVisitor<'a> {
12871299
visit::walk_impl_item(self, ii);
12881300
}
12891301

1290-
fn visit_variant_data(&mut self, vdata: &ast::VariantData, _: ast::Ident,
1291-
_: &ast::Generics, _: NodeId, span: Span) {
1292-
if vdata.fields().is_empty() {
1293-
if vdata.is_tuple() {
1294-
gate_feature_post!(&self, relaxed_adts, span,
1295-
"empty tuple structs and enum variants are unstable, \
1296-
use unit structs and enum variants instead");
1297-
}
1298-
}
1299-
1300-
visit::walk_struct_def(self, vdata)
1301-
}
1302-
13031302
fn visit_vis(&mut self, vis: &ast::Visibility) {
13041303
let span = match *vis {
13051304
ast::Visibility::Crate(span) => span,

src/test/compile-fail/auxiliary/empty-struct.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(relaxed_adts)]
12-
1311
pub struct XEmpty1 {}
1412
pub struct XEmpty2;
1513
pub struct XEmpty6();

src/test/compile-fail/auxiliary/namespace-mix-new.rs

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

11-
#![feature(item_like_imports, relaxed_adts)]
11+
#![feature(item_like_imports)]
1212

1313
pub mod c {
1414
pub struct S {}

src/test/compile-fail/auxiliary/namespace-mix-old.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// FIXME: Remove when `item_like_imports` is stabilized.
1212

13-
#![feature(relaxed_adts)]
14-
1513
pub mod c {
1614
pub struct S {}
1715
pub struct TS();

src/test/compile-fail/empty-struct-braces-pat-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// aux-build:empty-struct.rs
1414

15-
#![feature(relaxed_adts)]
16-
1715
extern crate empty_struct;
1816
use empty_struct::*;
1917

src/test/compile-fail/empty-struct-braces-pat-3.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// aux-build:empty-struct.rs
1414

15-
#![feature(relaxed_adts)]
16-
1715
extern crate empty_struct;
1816
use empty_struct::*;
1917

src/test/compile-fail/empty-struct-tuple-pat.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// aux-build:empty-struct.rs
1414

15-
#![feature(relaxed_adts)]
16-
1715
extern crate empty_struct;
1816
use empty_struct::*;
1917

src/test/compile-fail/empty-struct-unit-pat.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// aux-build:empty-struct.rs
1414

15-
#![feature(relaxed_adts)]
16-
1715
extern crate empty_struct;
1816
use empty_struct::*;
1917

src/test/compile-fail/feature-gate-relaxed-adts-2.rs

-27
This file was deleted.

src/test/compile-fail/issue-16819.rs

-26
This file was deleted.

src/test/compile-fail/issue-17800.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(relaxed_adts)]
12-
1311
enum MyOption<T> {
1412
MySome(T),
1513
MyNone,

src/test/compile-fail/issue-4736.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(relaxed_adts)]
12-
1311
struct NonCopyable(());
1412

1513
fn main() {

src/test/compile-fail/namespace-mix-new.rs

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

1111
// aux-build:namespace-mix-new.rs
1212

13-
#![feature(item_like_imports, relaxed_adts)]
13+
#![feature(item_like_imports)]
1414

1515
extern crate namespace_mix_new;
1616
use namespace_mix_new::*;

src/test/compile-fail/namespace-mix-old.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// aux-build:namespace-mix-old.rs
1414

15-
#![feature(relaxed_adts)]
16-
1715
extern crate namespace_mix_old;
1816
use namespace_mix_old::{xm1, xm2, xm3, xm4, xm5, xm6, xm7, xm8, xm9, xmA, xmB, xmC};
1917

src/test/compile-fail/feature-gate-relaxed-adts.rs src/test/compile-fail/numeric-fields-feature-gate.rs

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

11-
struct S(); //~ ERROR empty tuple structs and enum variants are unstable
12-
struct Z(u8, u8);
13-
14-
enum E {
15-
V(), //~ ERROR empty tuple structs and enum variants are unstable
16-
U(u8, u8),
17-
}
11+
struct S(u8);
1812

1913
fn main() {
20-
match S() {
21-
S() => {} //~ ERROR empty tuple structs patterns are unstable
22-
}
23-
match E::V() {
24-
E::V() => {} //~ ERROR empty tuple structs patterns are unstable
14+
let s = S{0: 10}; //~ ERROR numeric fields in struct expressions are unstable
15+
match s {
16+
S{0: a, ..} => {} //~ ERROR numeric fields in struct patterns are unstable
2517
}
2618
}

src/test/run-pass-fulldeps/empty-struct-braces-derive.rs

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

1111
// `#[derive(Trait)]` works for empty structs/variants with braces or parens.
1212

13-
#![feature(relaxed_adts)]
1413
#![feature(rustc_private)]
1514

1615
extern crate serialize as rustc_serialize;

src/test/run-pass/auxiliary/empty-struct.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(relaxed_adts)]
12-
1311
pub struct XEmpty1 {}
1412
pub struct XEmpty2;
1513
pub struct XEmpty7();

src/test/run-pass/empty-struct-braces.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
// aux-build:empty-struct.rs
1515

16-
#![feature(relaxed_adts)]
17-
1816
extern crate empty_struct;
1917
use empty_struct::*;
2018

0 commit comments

Comments
 (0)