Skip to content

Commit 4ec07ed

Browse files
committed
syntax: Allow where strings to be parsed independent from generics
This allows quasiquoting to insert where clauses.
1 parent c3f4fba commit 4ec07ed

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/libsyntax/ext/quote.rs

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ pub mod rt {
175175
impl_to_source! { ast::Block, block_to_string }
176176
impl_to_source! { ast::Arg, arg_to_string }
177177
impl_to_source! { Generics, generics_to_string }
178+
impl_to_source! { ast::WhereClause, where_clause_to_string }
178179
impl_to_source! { P<ast::Item>, item_to_string }
179180
impl_to_source! { P<ast::ImplItem>, impl_item_to_string }
180181
impl_to_source! { P<ast::TraitItem>, trait_item_to_string }
@@ -318,6 +319,7 @@ pub mod rt {
318319
impl_to_tokens! { ast::Ty }
319320
impl_to_tokens_lifetime! { &'a [ast::Ty] }
320321
impl_to_tokens! { Generics }
322+
impl_to_tokens! { ast::WhereClause }
321323
impl_to_tokens! { P<ast::Stmt> }
322324
impl_to_tokens! { P<ast::Expr> }
323325
impl_to_tokens! { ast::Block }

src/libsyntax/parse/parser.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ impl<'a> Parser<'a> {
11261126
p.parse_arg_general(false)
11271127
});
11281128

1129-
p.parse_where_clause(&mut generics);
1129+
generics.where_clause = p.parse_where_clause();
11301130
let sig = ast::MethodSig {
11311131
unsafety: style,
11321132
decl: d,
@@ -3932,9 +3932,14 @@ impl<'a> Parser<'a> {
39323932
/// ```
39333933
/// where T : Trait<U, V> + 'b, 'a : 'b
39343934
/// ```
3935-
fn parse_where_clause(&mut self, generics: &mut ast::Generics) {
3935+
fn parse_where_clause(&mut self) -> ast::WhereClause {
3936+
let mut where_clause = WhereClause {
3937+
id: ast::DUMMY_NODE_ID,
3938+
predicates: Vec::new(),
3939+
};
3940+
39363941
if !self.eat_keyword(keywords::Where) {
3937-
return
3942+
return where_clause;
39383943
}
39393944

39403945
let mut parsed_something = false;
@@ -3957,7 +3962,7 @@ impl<'a> Parser<'a> {
39573962
let hi = self.span.hi;
39583963
let span = mk_sp(lo, hi);
39593964

3960-
generics.where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
3965+
where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
39613966
ast::WhereRegionPredicate {
39623967
span: span,
39633968
lifetime: bounded_lifetime,
@@ -3992,7 +3997,7 @@ impl<'a> Parser<'a> {
39923997
at least one bound in it");
39933998
}
39943999

3995-
generics.where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4000+
where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
39964001
ast::WhereBoundPredicate {
39974002
span: span,
39984003
bound_lifetimes: bound_lifetimes,
@@ -4005,7 +4010,7 @@ impl<'a> Parser<'a> {
40054010
// let ty = self.parse_ty();
40064011
let hi = self.span.hi;
40074012
let span = mk_sp(lo, hi);
4008-
// generics.where_clause.predicates.push(
4013+
// where_clause.predicates.push(
40094014
// ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
40104015
// id: ast::DUMMY_NODE_ID,
40114016
// span: span,
@@ -4036,6 +4041,8 @@ impl<'a> Parser<'a> {
40364041
"a `where` clause must have at least one predicate \
40374042
in it");
40384043
}
4044+
4045+
where_clause
40394046
}
40404047

40414048
fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
@@ -4354,7 +4361,7 @@ impl<'a> Parser<'a> {
43544361
fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> ItemInfo {
43554362
let (ident, mut generics) = self.parse_fn_header();
43564363
let decl = self.parse_fn_decl(false);
4357-
self.parse_where_clause(&mut generics);
4364+
generics.where_clause = self.parse_where_clause();
43584365
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
43594366
(ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))
43604367
}
@@ -4439,7 +4446,7 @@ impl<'a> Parser<'a> {
44394446
let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
44404447
p.parse_arg()
44414448
});
4442-
self.parse_where_clause(&mut generics);
4449+
generics.where_clause = self.parse_where_clause();
44434450
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
44444451
(ident, inner_attrs, MethodImplItem(ast::MethodSig {
44454452
generics: generics,
@@ -4460,7 +4467,7 @@ impl<'a> Parser<'a> {
44604467
// Parse supertrait bounds.
44614468
let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
44624469

4463-
self.parse_where_clause(&mut tps);
4470+
tps.where_clause = self.parse_where_clause();
44644471

44654472
let meths = self.parse_trait_items();
44664473
(ident, ItemTrait(unsafety, tps, bounds, meths), None)
@@ -4531,7 +4538,7 @@ impl<'a> Parser<'a> {
45314538
if opt_trait.is_some() {
45324539
ty = self.parse_ty_sum();
45334540
}
4534-
self.parse_where_clause(&mut generics);
4541+
generics.where_clause = self.parse_where_clause();
45354542

45364543
self.expect(&token::OpenDelim(token::Brace));
45374544
let attrs = self.parse_inner_attributes();
@@ -4603,7 +4610,7 @@ impl<'a> Parser<'a> {
46034610
// struct.
46044611

46054612
let (fields, ctor_id) = if self.token.is_keyword(keywords::Where) {
4606-
self.parse_where_clause(&mut generics);
4613+
generics.where_clause = self.parse_where_clause();
46074614
if self.eat(&token::Semi) {
46084615
// If we see a: `struct Foo<T> where T: Copy;` style decl.
46094616
(Vec::new(), Some(ast::DUMMY_NODE_ID))
@@ -4684,12 +4691,12 @@ impl<'a> Parser<'a> {
46844691
token::get_ident(class_name.clone())));
46854692
}
46864693

4687-
self.parse_where_clause(generics);
4694+
generics.where_clause = self.parse_where_clause();
46884695
self.expect(&token::Semi);
46894696
fields
46904697
// This is the case where we just see struct Foo<T> where T: Copy;
46914698
} else if self.token.is_keyword(keywords::Where) {
4692-
self.parse_where_clause(generics);
4699+
generics.where_clause = self.parse_where_clause();
46934700
self.expect(&token::Semi);
46944701
Vec::new()
46954702
// This case is where we see: `struct Foo<T>;`
@@ -4937,7 +4944,7 @@ impl<'a> Parser<'a> {
49374944

49384945
let (ident, mut generics) = self.parse_fn_header();
49394946
let decl = self.parse_fn_decl(true);
4940-
self.parse_where_clause(&mut generics);
4947+
generics.where_clause = self.parse_where_clause();
49414948
let hi = self.span.hi;
49424949
self.expect(&token::Semi);
49434950
P(ast::ForeignItem {
@@ -5082,7 +5089,7 @@ impl<'a> Parser<'a> {
50825089
fn parse_item_type(&mut self) -> ItemInfo {
50835090
let ident = self.parse_ident();
50845091
let mut tps = self.parse_generics();
5085-
self.parse_where_clause(&mut tps);
5092+
tps.where_clause = self.parse_where_clause();
50865093
self.expect(&token::Eq);
50875094
let ty = self.parse_ty_sum();
50885095
self.expect(&token::Semi);
@@ -5182,7 +5189,7 @@ impl<'a> Parser<'a> {
51825189
fn parse_item_enum(&mut self) -> ItemInfo {
51835190
let id = self.parse_ident();
51845191
let mut generics = self.parse_generics();
5185-
self.parse_where_clause(&mut generics);
5192+
generics.where_clause = self.parse_where_clause();
51865193
self.expect(&token::OpenDelim(token::Brace));
51875194

51885195
let enum_definition = self.parse_enum_def(&generics);

0 commit comments

Comments
 (0)