Skip to content

Commit c82e9e8

Browse files
committed
Do not attemt to continue parsing after pub ident
Try to identify the following code in order to provide better diagnostics, but return the error to bail out early during the parse.
1 parent 7c0387e commit c82e9e8

19 files changed

+74
-282
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ impl<'a> LoweringContext<'a> {
17571757
bounds,
17581758
items)
17591759
}
1760-
ItemKind::Placeholder | ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
1760+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
17611761
panic!("Shouldn't still be around")
17621762
}
17631763
}

src/librustc/hir/map/def_collector.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
113113
return visit::walk_item(self, i);
114114
}
115115
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
116-
ItemKind::Placeholder |
117116
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
118117
DefPathData::ValueNs(i.ident.name.as_str()),
119118
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<'a> Resolver<'a> {
269269
self.define(parent, ident, TypeNS, imported_binding);
270270
}
271271

272-
ItemKind::GlobalAsm(..) | ItemKind::Placeholder => {}
272+
ItemKind::GlobalAsm(..) => {}
273273

274274
ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
275275

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,8 +1947,7 @@ impl<'a> Resolver<'a> {
19471947
}
19481948
}
19491949

1950-
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_) |
1951-
ItemKind::Placeholder => {
1950+
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_) => {
19521951
// do nothing, these are just around to be encoded
19531952
}
19541953

src/librustc_save_analysis/sig.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ impl Sig for ast::Item {
548548
// FIXME should implement this (e.g., pub use).
549549
ast::ItemKind::Use(_) => Err("import"),
550550
ast::ItemKind::Mac(..) | ast::ItemKind::MacroDef(_) => Err("Macro"),
551-
ast::ItemKind::Placeholder => Err("placeholder"),
552551
}
553552
}
554553
}

src/libsyntax/ast.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,6 @@ pub enum ItemKind {
19771977

19781978
/// A macro definition.
19791979
MacroDef(MacroDef),
1980-
Placeholder,
19811980
}
19821981

19831982
impl ItemKind {
@@ -1999,7 +1998,6 @@ impl ItemKind {
19991998
ItemKind::Mac(..) |
20001999
ItemKind::MacroDef(..) |
20012000
ItemKind::Impl(..) |
2002-
ItemKind::Placeholder |
20032001
ItemKind::AutoImpl(..) => "item"
20042002
}
20052003
}

src/libsyntax/fold.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,6 @@ pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
935935
),
936936
ItemKind::Mac(m) => ItemKind::Mac(folder.fold_mac(m)),
937937
ItemKind::MacroDef(def) => ItemKind::MacroDef(folder.fold_macro_def(def)),
938-
ItemKind::Placeholder => ItemKind::Placeholder,
939938
}
940939
}
941940

src/libsyntax/parse/parser.rs

Lines changed: 23 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -72,50 +72,6 @@ bitflags! {
7272

7373
type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute> >);
7474

75-
enum HasRecovered<'a, T> {
76-
Success(T),
77-
Recovered(T, DiagnosticBuilder<'a>),
78-
}
79-
80-
impl<'a, T> HasRecovered<'a, T> {
81-
fn new(t: T, err: Option<DiagnosticBuilder<'a>>) -> HasRecovered<'a, T> {
82-
if let Some(err) = err {
83-
HasRecovered::Recovered(t, err)
84-
} else {
85-
HasRecovered::Success(t)
86-
}
87-
}
88-
89-
fn map<O, F: FnOnce(T) -> O>(self, f: F) -> HasRecovered<'a, O> {
90-
let (t, e) = self.full_unwrap();
91-
HasRecovered::new(f(t), e)
92-
}
93-
94-
fn emit(self) -> T {
95-
match self {
96-
HasRecovered::Recovered(t, mut err) => {
97-
err.emit();
98-
t
99-
}
100-
HasRecovered::Success(t) => t,
101-
}
102-
}
103-
104-
fn full_unwrap(self) -> (T, Option<DiagnosticBuilder<'a>>) {
105-
match self {
106-
HasRecovered::Recovered(t, err) => (t, Some(err)),
107-
HasRecovered::Success(t) => (t, None),
108-
}
109-
}
110-
111-
fn into_result(self) -> PResult<'a, T> {
112-
match self {
113-
HasRecovered::Recovered(_, err) => Err(err),
114-
HasRecovered::Success(t) => Ok(t),
115-
}
116-
}
117-
}
118-
11975
/// How to parse a path.
12076
#[derive(Copy, Clone, PartialEq)]
12177
pub enum PathStyle {
@@ -1411,7 +1367,6 @@ impl<'a> Parser<'a> {
14111367
debug!("parse_trait_methods(): parsing provided method");
14121368
*at_end = true;
14131369
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1414-
let body = body.emit();
14151370
attrs.extend(inner_attrs.iter().cloned());
14161371
Some(body)
14171372
}
@@ -2447,7 +2402,7 @@ impl<'a> Parser<'a> {
24472402
let mut attrs = outer_attrs;
24482403
attrs.extend(self.parse_inner_attributes()?);
24492404

2450-
let blk = self.parse_block_tail(lo, blk_mode)?.emit();
2405+
let blk = self.parse_block_tail(lo, blk_mode)?;
24512406
return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), attrs));
24522407
}
24532408

@@ -3204,7 +3159,7 @@ impl<'a> Parser<'a> {
32043159

32053160
let hi = self.prev_span;
32063161
Ok(self.mk_expr(span_lo.to(hi),
3207-
ExprKind::ForLoop(pat, expr, loop_block.emit(), opt_ident),
3162+
ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
32083163
attrs))
32093164
}
32103165

@@ -3217,7 +3172,6 @@ impl<'a> Parser<'a> {
32173172
}
32183173
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
32193174
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3220-
let body = body.emit();
32213175
attrs.extend(iattrs);
32223176
let span = span_lo.to(body.span);
32233177
return Ok(self.mk_expr(span, ExprKind::While(cond, body, opt_ident), attrs));
@@ -3233,7 +3187,6 @@ impl<'a> Parser<'a> {
32333187
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
32343188
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
32353189
attrs.extend(iattrs);
3236-
let body = body.emit();
32373190
let span = span_lo.to(body.span);
32383191
return Ok(self.mk_expr(span, ExprKind::WhileLet(pat, expr, body, opt_ident), attrs));
32393192
}
@@ -3243,7 +3196,6 @@ impl<'a> Parser<'a> {
32433196
span_lo: Span,
32443197
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
32453198
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3246-
let body = body.emit();
32473199
attrs.extend(iattrs);
32483200
let span = span_lo.to(body.span);
32493201
Ok(self.mk_expr(span, ExprKind::Loop(body, opt_ident), attrs))
@@ -3254,7 +3206,6 @@ impl<'a> Parser<'a> {
32543206
-> PResult<'a, P<Expr>>
32553207
{
32563208
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3257-
let body = body.emit();
32583209
attrs.extend(iattrs);
32593210
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::Catch(body), attrs))
32603211
}
@@ -4301,14 +4252,14 @@ impl<'a> Parser<'a> {
43014252
return Err(e);
43024253
}
43034254

4304-
Ok(self.parse_block_tail(lo, BlockCheckMode::Default)?.emit())
4255+
Ok(self.parse_block_tail(lo, BlockCheckMode::Default)?)
43054256
}
43064257

43074258
/// Parse a block. Inner attrs are allowed.
43084259
fn parse_inner_attrs_and_block(&mut self)
4309-
-> PResult<'a, (Vec<Attribute>, HasRecovered<'a, P<Block>>)>
4260+
-> PResult<'a, (Vec<Attribute>, P<Block>)>
43104261
{
4311-
maybe_whole!(self, NtBlock, |x| (Vec::new(), HasRecovered::Success(x)));
4262+
maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
43124263

43134264
let lo = self.span;
43144265
self.expect(&token::OpenDelim(token::Brace))?;
@@ -4319,15 +4270,14 @@ impl<'a> Parser<'a> {
43194270
/// Parse the rest of a block expression or function body
43204271
/// Precondition: already parsed the '{'.
43214272
fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode)
4322-
-> PResult<'a, HasRecovered<'a, P<Block>>>
4273+
-> PResult<'a, P<Block>>
43234274
{
43244275
let mut stmts = vec![];
43254276

4326-
let mut error = None;
43274277
while !self.eat(&token::CloseDelim(token::Brace)) {
43284278
let stmt = match self.parse_full_stmt(false) {
4329-
Err(err) => {
4330-
error = Some(err);
4279+
Err(mut err) => {
4280+
err.emit();
43314281
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Break);
43324282
break;
43334283
}
@@ -4342,14 +4292,12 @@ impl<'a> Parser<'a> {
43424292
continue;
43434293
};
43444294
}
4345-
let block = HasRecovered::new(P(ast::Block {
4295+
Ok(P(ast::Block {
43464296
stmts,
43474297
id: ast::DUMMY_NODE_ID,
43484298
rules: s,
43494299
span: lo.to(self.prev_span),
4350-
}), error);
4351-
4352-
Ok(block)
4300+
}))
43534301
}
43544302

43554303
/// Parse a statement, including the trailing semicolon.
@@ -4984,22 +4932,11 @@ impl<'a> Parser<'a> {
49844932
constness: Spanned<Constness>,
49854933
abi: abi::Abi)
49864934
-> PResult<'a, ItemInfo> {
4987-
4988-
self.parse_item_fn_recoverable(unsafety, constness, abi)?.into_result()
4989-
}
4990-
4991-
fn parse_item_fn_recoverable(&mut self,
4992-
unsafety: Unsafety,
4993-
constness: Spanned<Constness>,
4994-
abi: abi::Abi)
4995-
-> PResult<'a, HasRecovered<'a, ItemInfo>> {
49964935
let (ident, mut generics) = self.parse_fn_header()?;
49974936
let decl = self.parse_fn_decl(false)?;
49984937
generics.where_clause = self.parse_where_clause()?;
49994938
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
5000-
Ok(body.map(|body| (ident,
5001-
ItemKind::Fn(decl, unsafety, constness, abi, generics, body),
5002-
Some(inner_attrs))))
4939+
Ok((ident, ItemKind::Fn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs)))
50034940
}
50044941

50054942
/// true if we are looking at `const ID`, false for things like `const fn` etc
@@ -5183,7 +5120,6 @@ impl<'a> Parser<'a> {
51835120
generics.where_clause = self.parse_where_clause()?;
51845121
*at_end = true;
51855122
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
5186-
let body = body.into_result()?;
51875123
Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method(ast::MethodSig {
51885124
abi,
51895125
unsafety,
@@ -6369,49 +6305,31 @@ impl<'a> Parser<'a> {
63696305
let mut err = self.diagnostic()
63706306
.struct_span_err(sp, "missing `struct` for struct definition");
63716307
err.span_suggestion_short(sp, &msg, " struct ".into());
6372-
err.emit();
6373-
self.consume_block(token::Brace);
6374-
let prev_span = self.prev_span;
6375-
let item = self.mk_item(lo.to(prev_span),
6376-
ident,
6377-
ItemKind::Placeholder,
6378-
visibility,
6379-
vec![]);
6380-
return Ok(Some(item));
6308+
return Err(err);
63816309
} else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
63826310
let ident = self.parse_ident().unwrap();
63836311
self.consume_block(token::Paren);
6384-
let (kw, ambiguous) = if self.check(&token::OpenDelim(token::Brace)) {
6385-
self.consume_block(token::Brace);
6386-
("fn", false)
6312+
let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) ||
6313+
self.check(&token::OpenDelim(token::Brace))
6314+
{
6315+
("fn", "method", false)
63876316
} else if self.check(&token::Colon) {
63886317
let kw = "struct";
6389-
(kw, false)
6318+
(kw, kw, false)
63906319
} else {
6391-
("fn` or `struct", true)
6320+
("fn` or `struct", "method or struct", true)
63926321
};
63936322

6394-
let msg = format!("missing `{}`{}", kw,
6395-
if ambiguous {
6396-
"".to_string()
6397-
} else {
6398-
format!(" for {} definition", kw)
6399-
});
6323+
let msg = format!("missing `{}` for {} definition", kw, kw_name);
64006324
let mut err = self.diagnostic().struct_span_err(sp, &msg);
64016325
if !ambiguous {
6402-
let suggestion = format!("add `{kw}` here to parse `{}` as a public {kw}",
6326+
let suggestion = format!("add `{}` here to parse `{}` as a public {}",
6327+
kw,
64036328
ident,
6404-
kw=kw);
6329+
kw_name);
64056330
err.span_suggestion_short(sp, &suggestion, format!(" {} ", kw));
64066331
}
6407-
err.emit();
6408-
let prev_span = self.prev_span;
6409-
let item = self.mk_item(lo.to(prev_span),
6410-
ident,
6411-
ItemKind::Placeholder,
6412-
visibility,
6413-
vec![]);
6414-
return Ok(Some(item));
6332+
return Err(err);
64156333
}
64166334
}
64176335
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility)

src/libsyntax/print/pprust.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,12 +1386,6 @@ impl<'a> State<'a> {
13861386
self.s.word(";")?;
13871387
self.end()?;
13881388
}
1389-
ast::ItemKind::Placeholder => {
1390-
self.s.word("<placeholder ")?;
1391-
self.print_ident(item.ident)?;
1392-
self.s.word(">")?;
1393-
self.end()?;
1394-
}
13951389
}
13961390
self.ann.post(self, NodeItem(item))
13971391
}

src/libsyntax/visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
307307
}
308308
ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
309309
ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
310-
ItemKind::Placeholder => (),
311310
}
312311
walk_list!(visitor, visit_attribute, &item.attrs);
313312
}

src/test/ui/pub/pub-restricted-error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ struct Foo {
1616
pub(crate) () foo: usize,
1717
}
1818

19-
19+
fn main() {}

src/test/ui/suggestions/pub-ident-missing-kw-unclosed-block.rs renamed to src/test/ui/suggestions/pub-ident-fn-2.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub Struct {
12-
y: usize,
13-
}
14-
15-
pub Y {
16-
x: usize,
17-
18-
19-
pub struct X {
20-
foo();
21-
}
22-
23-
pub Z {
24-
x->foo(),
25-
}
26-
27-
pub foo(foo) {
28-
foo
29-
}
11+
pub foo(s: usize) { bar() }
3012

31-
pub struct X {
32-
foo();
13+
fn main() {
14+
foo(2);
3315
}
34-
35-
pub Z {
36-
x->foo(),
37-
}
38-
39-
fn main(){}

0 commit comments

Comments
 (0)