Skip to content

Commit 869b816

Browse files
committed
syntax: Rename parse_ty -> parse_ty_no_plus, parse_ty_sum -> parse_ty
1 parent 03620db commit 869b816

File tree

4 files changed

+44
-40
lines changed

4 files changed

+44
-40
lines changed

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl<'a> Parser<'a> {
540540
}
541541
ExpansionKind::Expr => Expansion::Expr(self.parse_expr()?),
542542
ExpansionKind::OptExpr => Expansion::OptExpr(Some(self.parse_expr()?)),
543-
ExpansionKind::Ty => Expansion::Ty(self.parse_ty()?),
543+
ExpansionKind::Ty => Expansion::Ty(self.parse_ty_no_plus()?),
544544
ExpansionKind::Pat => Expansion::Pat(self.parse_pat()?),
545545
})
546546
}

src/libsyntax/ext/quote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub fn parse_arm_panic(parser: &mut Parser) -> Arm {
388388
}
389389

390390
pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> {
391-
panictry!(parser.parse_ty())
391+
panictry!(parser.parse_ty_no_plus())
392392
}
393393

394394
pub fn parse_stmt_panic(parser: &mut Parser) -> Option<Stmt> {

src/libsyntax/ext/tt/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
522522
},
523523
"pat" => token::NtPat(panictry!(p.parse_pat())),
524524
"expr" => token::NtExpr(panictry!(p.parse_expr())),
525-
"ty" => token::NtTy(panictry!(p.parse_ty())),
525+
"ty" => token::NtTy(panictry!(p.parse_ty_no_plus())),
526526
// this could be handled like a token, since it is one
527527
"ident" => match p.token {
528528
token::Ident(sn) => {

src/libsyntax/parse/parser.rs

+41-37
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ impl<'a> Parser<'a> {
11281128
self.expect_keyword(keywords::Const)?;
11291129
let ident = self.parse_ident()?;
11301130
self.expect(&token::Colon)?;
1131-
let ty = self.parse_ty_sum()?;
1131+
let ty = self.parse_ty()?;
11321132
let default = if self.check(&token::Eq) {
11331133
self.bump();
11341134
let expr = self.parse_expr()?;
@@ -1244,24 +1244,24 @@ impl<'a> Parser<'a> {
12441244
/// Parse a possibly mutable type
12451245
pub fn parse_mt(&mut self) -> PResult<'a, MutTy> {
12461246
let mutbl = self.parse_mutability()?;
1247-
let t = self.parse_ty()?;
1247+
let t = self.parse_ty_no_plus()?;
12481248
Ok(MutTy { ty: t, mutbl: mutbl })
12491249
}
12501250

12511251
/// Parse optional return type [ -> TY ] in function decl
12521252
pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
12531253
if self.eat(&token::RArrow) {
1254-
Ok(FunctionRetTy::Ty(self.parse_ty()?))
1254+
Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
12551255
} else {
12561256
let pos = self.span.lo;
12571257
Ok(FunctionRetTy::Default(mk_sp(pos, pos)))
12581258
}
12591259
}
12601260

1261-
/// Parse a type in a context where `T1+T2` is allowed.
1262-
pub fn parse_ty_sum(&mut self) -> PResult<'a, P<Ty>> {
1261+
/// Parse a type.
1262+
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
12631263
let lo = self.span.lo;
1264-
let lhs = self.parse_ty()?;
1264+
let lhs = self.parse_ty_no_plus()?;
12651265

12661266
if !self.eat(&token::BinOp(token::Plus)) {
12671267
return Ok(lhs);
@@ -1331,8 +1331,12 @@ impl<'a> Parser<'a> {
13311331
Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp}))
13321332
}
13331333

1334-
/// Parse a type.
1335-
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
1334+
/// Parse a type in restricted contexts where `+` is not permitted.
1335+
/// Example 1: `&'a TYPE`
1336+
/// `+` is prohibited to maintain operator priority (P(+) < P(&)).
1337+
/// Example 2: `value1 as TYPE + value2`
1338+
/// `+` is prohibited to avoid interactions with expression grammar.
1339+
pub fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
13361340
maybe_whole!(self, NtTy, |x| x);
13371341

13381342
let lo = self.span.lo;
@@ -1346,7 +1350,7 @@ impl<'a> Parser<'a> {
13461350
let mut ts = vec![];
13471351
let mut last_comma = false;
13481352
while self.token != token::CloseDelim(token::Paren) {
1349-
ts.push(self.parse_ty_sum()?);
1353+
ts.push(self.parse_ty()?);
13501354
if self.check(&token::Comma) {
13511355
last_comma = true;
13521356
self.bump();
@@ -1371,7 +1375,7 @@ impl<'a> Parser<'a> {
13711375
} else if self.check(&token::OpenDelim(token::Bracket)) {
13721376
// VECTOR
13731377
self.expect(&token::OpenDelim(token::Bracket))?;
1374-
let t = self.parse_ty_sum()?;
1378+
let t = self.parse_ty()?;
13751379

13761380
// Parse the `; e` in `[ i32; e ]`
13771381
// where `e` is a const expression
@@ -1452,7 +1456,7 @@ impl<'a> Parser<'a> {
14521456
`*mut T` or `*const T` as appropriate)");
14531457
Mutability::Immutable
14541458
};
1455-
let t = self.parse_ty()?;
1459+
let t = self.parse_ty_no_plus()?;
14561460
Ok(MutTy { ty: t, mutbl: mutbl })
14571461
}
14581462

@@ -1499,7 +1503,7 @@ impl<'a> Parser<'a> {
14991503
})
15001504
};
15011505

1502-
let t = self.parse_ty_sum()?;
1506+
let t = self.parse_ty()?;
15031507

15041508
Ok(Arg {
15051509
ty: t,
@@ -1517,7 +1521,7 @@ impl<'a> Parser<'a> {
15171521
pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
15181522
let pat = self.parse_pat()?;
15191523
let t = if self.eat(&token::Colon) {
1520-
self.parse_ty_sum()?
1524+
self.parse_ty()?
15211525
} else {
15221526
P(Ty {
15231527
id: ast::DUMMY_NODE_ID,
@@ -1658,7 +1662,7 @@ impl<'a> Parser<'a> {
16581662
pub fn parse_qualified_path(&mut self, mode: PathStyle)
16591663
-> PResult<'a, (QSelf, ast::Path)> {
16601664
let span = self.prev_span;
1661-
let self_type = self.parse_ty_sum()?;
1665+
let self_type = self.parse_ty()?;
16621666
let mut path = if self.eat_keyword(keywords::As) {
16631667
self.parse_path(PathStyle::Type)?
16641668
} else {
@@ -1768,10 +1772,10 @@ impl<'a> Parser<'a> {
17681772
let inputs = self.parse_seq_to_end(
17691773
&token::CloseDelim(token::Paren),
17701774
SeqSep::trailing_allowed(token::Comma),
1771-
|p| p.parse_ty_sum())?;
1775+
|p| p.parse_ty())?;
17721776

17731777
let output_ty = if self.eat(&token::RArrow) {
1774-
Some(self.parse_ty()?)
1778+
Some(self.parse_ty_no_plus()?)
17751779
} else {
17761780
None
17771781
};
@@ -2981,12 +2985,12 @@ impl<'a> Parser<'a> {
29812985
}
29822986
// Special cases:
29832987
if op == AssocOp::As {
2984-
let rhs = self.parse_ty()?;
2988+
let rhs = self.parse_ty_no_plus()?;
29852989
let (lo, hi) = (lhs_span.lo, rhs.span.hi);
29862990
lhs = self.mk_expr(lo, hi, ExprKind::Cast(lhs, rhs), ThinVec::new());
29872991
continue
29882992
} else if op == AssocOp::Colon {
2989-
let rhs = self.parse_ty()?;
2993+
let rhs = self.parse_ty_no_plus()?;
29902994
let (lo, hi) = (lhs_span.lo, rhs.span.hi);
29912995
lhs = self.mk_expr(lo, hi, ExprKind::Type(lhs, rhs), ThinVec::new());
29922996
continue
@@ -3754,7 +3758,7 @@ impl<'a> Parser<'a> {
37543758

37553759
let mut ty = None;
37563760
if self.eat(&token::Colon) {
3757-
ty = Some(self.parse_ty_sum()?);
3761+
ty = Some(self.parse_ty()?);
37583762
}
37593763
let init = self.parse_initializer()?;
37603764
Ok(P(ast::Local {
@@ -3775,7 +3779,7 @@ impl<'a> Parser<'a> {
37753779
-> PResult<'a, StructField> {
37763780
let name = self.parse_ident()?;
37773781
self.expect(&token::Colon)?;
3778-
let ty = self.parse_ty_sum()?;
3782+
let ty = self.parse_ty()?;
37793783
Ok(StructField {
37803784
span: mk_sp(lo, self.prev_span.hi),
37813785
ident: Some(name),
@@ -4250,7 +4254,7 @@ impl<'a> Parser<'a> {
42504254

42514255
let default = if self.check(&token::Eq) {
42524256
self.bump();
4253-
Some(self.parse_ty_sum()?)
4257+
Some(self.parse_ty()?)
42544258
} else {
42554259
None
42564260
};
@@ -4345,7 +4349,7 @@ impl<'a> Parser<'a> {
43454349
let mut err = self.diagnostic().struct_span_err(self.span, &msg);
43464350

43474351
let span_hi = self.span.hi;
4348-
let span_hi = match self.parse_ty() {
4352+
let span_hi = match self.parse_ty_no_plus() {
43494353
Ok(..) => self.span.hi,
43504354
Err(ref mut err) => {
43514355
self.cancel(err);
@@ -4368,7 +4372,7 @@ impl<'a> Parser<'a> {
43684372
if p.look_ahead(1, |t| t == &token::Eq) {
43694373
Ok(None)
43704374
} else {
4371-
Ok(Some(p.parse_ty_sum()?))
4375+
Ok(Some(p.parse_ty()?))
43724376
}
43734377
}
43744378
)?;
@@ -4386,7 +4390,7 @@ impl<'a> Parser<'a> {
43864390
let lo = p.span.lo;
43874391
let ident = p.parse_ident()?;
43884392
p.expect(&token::Eq)?;
4389-
let ty = p.parse_ty()?;
4393+
let ty = p.parse_ty_no_plus()?;
43904394
let hi = ty.span.hi;
43914395
let span = mk_sp(lo, hi);
43924396
return Ok(TypeBinding{id: ast::DUMMY_NODE_ID,
@@ -4484,7 +4488,7 @@ impl<'a> Parser<'a> {
44844488
vec![]
44854489
};
44864490

4487-
let bounded_ty = self.parse_ty()?;
4491+
let bounded_ty = self.parse_ty_no_plus()?;
44884492

44894493
if self.eat(&token::Colon) {
44904494
let bounds = self.parse_ty_param_bounds()?;
@@ -4507,7 +4511,7 @@ impl<'a> Parser<'a> {
45074511

45084512
parsed_something = true;
45094513
} else if self.eat(&token::Eq) {
4510-
// let ty = try!(self.parse_ty());
4514+
// let ty = try!(self.parse_ty_no_plus());
45114515
let hi = self.prev_span.hi;
45124516
let span = mk_sp(lo, hi);
45134517
// where_clause.predicates.push(
@@ -4679,7 +4683,7 @@ impl<'a> Parser<'a> {
46794683
// self: TYPE
46804684
let eself_ident = expect_ident(self);
46814685
if self.eat(&token::Colon) {
4682-
let ty = self.parse_ty_sum()?;
4686+
let ty = self.parse_ty()?;
46834687
(SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
46844688
} else {
46854689
(SelfKind::Value(Mutability::Immutable), eself_ident)
@@ -4691,7 +4695,7 @@ impl<'a> Parser<'a> {
46914695
self.bump();
46924696
let eself_ident = expect_ident(self);
46934697
if self.eat(&token::Colon) {
4694-
let ty = self.parse_ty_sum()?;
4698+
let ty = self.parse_ty()?;
46954699
(SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
46964700
} else {
46974701
(SelfKind::Value(Mutability::Mutable), eself_ident)
@@ -4848,14 +4852,14 @@ impl<'a> Parser<'a> {
48484852
let (name, node) = if self.eat_keyword(keywords::Type) {
48494853
let name = self.parse_ident()?;
48504854
self.expect(&token::Eq)?;
4851-
let typ = self.parse_ty_sum()?;
4855+
let typ = self.parse_ty()?;
48524856
self.expect(&token::Semi)?;
48534857
(name, ast::ImplItemKind::Type(typ))
48544858
} else if self.is_const_item() {
48554859
self.expect_keyword(keywords::Const)?;
48564860
let name = self.parse_ident()?;
48574861
self.expect(&token::Colon)?;
4858-
let typ = self.parse_ty_sum()?;
4862+
let typ = self.parse_ty()?;
48594863
self.expect(&token::Eq)?;
48604864
let expr = self.parse_expr()?;
48614865
self.expect(&token::Semi)?;
@@ -4979,7 +4983,7 @@ impl<'a> Parser<'a> {
49794983
};
49804984

49814985
// Parse the trait.
4982-
let mut ty = self.parse_ty_sum()?;
4986+
let mut ty = self.parse_ty()?;
49834987

49844988
// Parse traits, if necessary.
49854989
let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
@@ -5020,7 +5024,7 @@ impl<'a> Parser<'a> {
50205024
ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
50215025
} else {
50225026
if opt_trait.is_some() {
5023-
ty = self.parse_ty_sum()?;
5027+
ty = self.parse_ty()?;
50245028
}
50255029
generics.where_clause = self.parse_where_clause()?;
50265030

@@ -5172,15 +5176,15 @@ impl<'a> Parser<'a> {
51725176
let mut vis = p.parse_visibility(false)?;
51735177
let ty_is_interpolated =
51745178
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
5175-
let mut ty = p.parse_ty_sum()?;
5179+
let mut ty = p.parse_ty()?;
51765180

51775181
// Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
51785182
if vis == Visibility::Public && !ty_is_interpolated &&
51795183
p.token != token::Comma && p.token != token::CloseDelim(token::Paren) {
51805184
ty = if let TyKind::Paren(ref path_ty) = ty.node {
51815185
if let TyKind::Path(None, ref path) = path_ty.node {
51825186
vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
5183-
Some(p.parse_ty_sum()?)
5187+
Some(p.parse_ty()?)
51845188
} else {
51855189
None
51865190
}
@@ -5298,7 +5302,7 @@ impl<'a> Parser<'a> {
52985302
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
52995303
let id = self.parse_ident()?;
53005304
self.expect(&token::Colon)?;
5301-
let ty = self.parse_ty_sum()?;
5305+
let ty = self.parse_ty()?;
53025306
self.expect(&token::Eq)?;
53035307
let e = self.parse_expr()?;
53045308
self.expect(&token::Semi)?;
@@ -5539,7 +5543,7 @@ impl<'a> Parser<'a> {
55395543

55405544
let ident = self.parse_ident()?;
55415545
self.expect(&token::Colon)?;
5542-
let ty = self.parse_ty_sum()?;
5546+
let ty = self.parse_ty()?;
55435547
let hi = self.span.hi;
55445548
self.expect(&token::Semi)?;
55455549
Ok(ForeignItem {
@@ -5628,7 +5632,7 @@ impl<'a> Parser<'a> {
56285632
let mut tps = self.parse_generics()?;
56295633
tps.where_clause = self.parse_where_clause()?;
56305634
self.expect(&token::Eq)?;
5631-
let ty = self.parse_ty_sum()?;
5635+
let ty = self.parse_ty()?;
56325636
self.expect(&token::Semi)?;
56335637
Ok((ident, ItemKind::Ty(ty, tps), None))
56345638
}

0 commit comments

Comments
 (0)