Skip to content
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

syntax: Support parentheses around trait bounds #41077

Merged
merged 3 commits into from
Apr 22, 2017

Conversation

petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Apr 4, 2017

An implementation for #39318 (comment)

Closes #39318
r? @nikomatsakis

self.bump(); // `+`
let pt = PolyTraitRef::new(Vec::new(), path.clone(), lo.to(self.prev_span));
let mut bounds = vec![TraitTyParamBound(pt, TraitBoundModifier::None)];
bounds.append(&mut self.parse_ty_param_bounds()?);
TyKind::TraitObject(bounds)
}
TyKind::TraitObject(ref bounds) if maybe_bounds && bounds.len() == 1 => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: Need to reject (Trait +) + Trait.

@@ -4048,17 +4059,24 @@ impl<'a> Parser<'a> {
// Parse bounds of a type parameter `BOUND + BOUND + BOUND` without trailing `+`.
// BOUND = TY_BOUND | LT_BOUND
// LT_BOUND = LIFETIME (e.g. `'a`)
// TY_BOUND = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
// TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikomatsakis
You wanted a grammar, here it is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Sorry for my high lag, traveling this week.

@nikomatsakis
Copy link
Contributor

OK, so, I'm not sure I'm 100% happy with these rules around parens, but honestly I'm ready to stop thinking about it. =) And besides if anything I would want to accept more, so it seems ok to accept less for now.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 14, 2017
@carols10cents
Copy link
Member

Soooo do i hear an r, @nikomatsakis? What are the next steps here?

@nikomatsakis
Copy link
Contributor

@carols10cents heh, thanks for keeping me on my toes. Got distracted before deciding what the next step was -- in particular, whether to consult with @rust-lang/lang or what. I think I'm inclined to r+ regardless, let me do another quick review though, since I think that this can only be a subset of the behavior we would eventually want. (And maybe it's fine to just stop here for now.)

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments.

self.bump(); // `+`
let pt = PolyTraitRef::new(Vec::new(), path.clone(), lo.to(self.prev_span));
let mut bounds = vec![TraitTyParamBound(pt, TraitBoundModifier::None)];
bounds.append(&mut self.parse_ty_param_bounds()?);
TyKind::TraitObject(bounds)
}
TyKind::TraitObject(ref bounds)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is an example where this case gets hit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it occurs with (for<'a> Foo)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it occurs with (for<'a> Foo)?

Exactly

@@ -4070,6 +4088,9 @@ impl<'a> Parser<'a> {
TraitBoundModifier::None
};
bounds.push(TraitTyParamBound(poly_trait, modifier));
if has_parens {
self.expect(&token::CloseDelim(token::Paren))?;
}
} else {
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we consumed an open-paren at the start of the loop, don't we want to consume the close paren here somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In general the handling of parens here feels complex.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks happens only if the code is already syntactically incorrect, something like

(Trait) + (Trait) + ($*&^#&*%%a;ch

so there may be no closing paren.
I can move expect(closing_paren) below else { ... } though, it should look a bit nicer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov can you add some comments about this, at minimum? moving the expect(closing_patern) also sounds good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, the break is actually wrong in presence of ( or ?.
fn f<T: ?>(){} in particular compiles on all Rust versions since 1.0 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this too.

@carols10cents carols10cents added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2017
@petrochenkov petrochenkov force-pushed the boundparen branch 3 times, most recently from b01f7b3 to 45a7ef2 Compare April 20, 2017 21:34
@petrochenkov
Copy link
Contributor Author

Updated.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 22, 2017

📌 Commit 8838cd1 has been approved by nikomatsakis

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Apr 22, 2017
…akis

syntax: Support parentheses around trait bounds

An implementation for rust-lang#39318 (comment)

r? @nikomatsakis
bors added a commit that referenced this pull request Apr 22, 2017
Rollup of 3 pull requests

- Successful merges: #41077, #41355, #41450
- Failed merges:
@bors
Copy link
Contributor

bors commented Apr 22, 2017

⌛ Testing commit 8838cd1 with merge 9cc77d7...

@bors bors merged commit 8838cd1 into rust-lang:master Apr 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants