Skip to content

libsyntax: no inheritance with tuple/unit structs #15117

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

Closed
wants to merge 1 commit into from
Closed
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
81 changes: 49 additions & 32 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4064,43 +4064,19 @@ impl<'a> Parser<'a> {

// parse struct Foo { ... }
fn parse_item_struct(&mut self, is_virtual: bool) -> ItemInfo {
let name_span = self.span;
let class_name = self.parse_ident();
let generics = self.parse_generics();

let super_struct = if self.eat(&token::COLON) {
let ty = self.parse_ty(true);
match ty.node {
TyPath(_, None, _) => {
Some(ty)
}
_ => {
self.span_err(ty.span, "not a struct");
None
}
}
} else {
None
};
let super_struct;

let mut fields: Vec<StructField>;
let is_tuple_like;

if self.eat(&token::LBRACE) {
// It's a record-like struct.
is_tuple_like = false;
fields = Vec::new();
while self.token != token::RBRACE {
fields.push(self.parse_struct_decl_field());
}
if fields.len() == 0 {
self.fatal(format!("unit-like struct definition should be \
written as `struct {};`",
token::get_ident(class_name)).as_slice());
}
self.bump();
} else if self.token == token::LPAREN {
if self.token == token::LPAREN {
// It's a tuple-like struct.
is_tuple_like = true;
super_struct = None;
fields = self.parse_unspanned_seq(
&token::LPAREN,
&token::RPAREN,
Expand All @@ -4125,12 +4101,53 @@ impl<'a> Parser<'a> {
} else if self.eat(&token::SEMI) {
// It's a unit-like struct.
is_tuple_like = true;
super_struct = None;
fields = Vec::new();
} else {
let token_str = self.this_token_to_str();
self.fatal(format!("expected `{}`, `(`, or `;` after struct \
name but found `{}`", "{",
token_str).as_slice())
// It can only be a record-like struct now.
is_tuple_like = false;
super_struct = if self.eat(&token::COLON) {
let ty = self.parse_ty(true);
match ty.node {
TyPath(_, None, _) => {
Some(ty)
}
_ => {
self.span_err(ty.span, "not a struct");
None
}
}
} else {
None
};

if !self.eat(&token::LBRACE) {
let token_str = self.this_token_to_str();
let expected = if super_struct.is_some() {
"`{` after super-struct path"
} else {
// not advertising struct inheritance here
"`{`, `(`, or `;` after struct name"
};
self.fatal(format!("expected {} but found `{}`",
expected, token_str).as_slice())
}

fields = Vec::new();
while self.token != token::RBRACE {
fields.push(self.parse_struct_decl_field());
}
if fields.len() == 0 {
self.fatal(format!("unit-like struct definition should be \
written as `struct {};`",
token::get_ident(class_name)).as_slice());
}
self.bump();
}

if is_virtual && is_tuple_like {
self.span_err(name_span,
"unit-like and tuple structs cannot be virtual");
}

let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug.
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/inherit-struct1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#![feature(struct_inherit)]


struct S6 : int; //~ ERROR super-struct could not be resolved
struct S6 : int { x: () } //~ ERROR super-struct could not be resolved

pub fn main() {
}
17 changes: 17 additions & 0 deletions src/test/compile-fail/inherit-struct10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test struct inheritance.
#![feature(struct_inherit)]

virtual struct UnitLikeVirtual; //~ ERROR unit-like and tuple structs cannot be virtual
virtual struct TupleStructVirtual(int); //~ ERROR unit-like and tuple structs cannot be virtual

fn main() {}
17 changes: 17 additions & 0 deletions src/test/compile-fail/inherit-struct11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test struct inheritance.
#![feature(struct_inherit)]

virtual struct RecordLikeVirtual { i: int }
struct UnitLike : RecordLikeVirtual; //~ ERROR expected `{` after super-struct path but found `;`

fn main() {}
17 changes: 17 additions & 0 deletions src/test/compile-fail/inherit-struct12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test struct inheritance.
#![feature(struct_inherit)]

virtual struct RecordLikeVirtual { i: int }
struct TupleStruct(int) : RecordLikeVirtual; //~ ERROR expected `;` but found `:`

fn main() {}
18 changes: 18 additions & 0 deletions src/test/compile-fail/inherit-struct13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test struct inheritance.
#![feature(struct_inherit)]

virtual struct RecordLikeVirtual { i: int }
struct TupleStruct : RecordLikeVirtual (int);
//~^ ERROR expected `{` after super-struct path but found `(`

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/inherit-struct5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// aux-build:inherit_struct_lib.rs
extern crate inherit_struct_lib;

struct S3 : inherit_struct_lib::S1; //~ ERROR super-struct is defined in a different crate
struct S3 : inherit_struct_lib::S1 { x: () } //~ ERROR super-struct is defined in a different crate

pub fn main() {
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/inherit-struct9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// Test struct inheritance.
#![feature(struct_inherit)]

struct s9;
struct s10 : s9; //~ ERROR struct inheritance is only allowed from virtual structs
struct s9 { i: int }
struct s10 : s9 { j: int } //~ ERROR struct inheritance is only allowed from virtual structs

pub fn main() {
}