1
Fork 0

Auto merge of #43540 - petrochenkov:pathrelax, r=nikomatsakis

syntax: Relax path grammar

TLDR: Accept the disambiguator `::` in "type" paths (`Type::<Args>`), accept the disambiguator `::` before parenthesized generic arguments (`Fn::(Args)`).

The "turbofish" disambiguator `::<>` in expression paths is a necessary evil required for path parsing to be both simple and to give reasonable results.
Since paths in expressions usually refer to values (but not necessarily, e.g. `Struct::<u8> { field: 0 }` is disambiguated, but refers to a type), people often consider `::<>` to be inherent to *values*, and not *expressions* and want to write disambiguated paths for values even in contexts where disambiguation is not strictly necessary, for example when a path is passed to a macro `m!(Vec::<i32>::new)`.
The problem is that currently, if the disambiguator is not *required*, then it's *prohibited*. This results in confusion - see https://github.com/rust-lang/rust/issues/41740, https://internals.rust-lang.org/t/macro-path-uses-novel-syntax/5561.

This PR makes the disambiguator *optional* instead of prohibited in contexts where it's not strictly required, so people can pass paths to macros in whatever form they consider natural (e.g. disambiguated form for value paths).
This PR also accepts the disambiguator in paths with parenthesized arguments (`Fn::(Args)`) for consistency and to simplify testing of stuff like https://github.com/rust-lang/rust/pull/41856#issuecomment-301219194.

Closes https://github.com/rust-lang/rust/issues/41740

cc @rust-lang/lang
r? @nikomatsakis
This commit is contained in:
bors 2017-08-21 23:03:57 +00:00
commit 8df670b6a6
6 changed files with 49 additions and 44 deletions

View file

@ -599,9 +599,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
panic!(FatalError) panic!(FatalError)
} }
}, },
"path" => { "path" => token::NtPath(panictry!(p.parse_path_common(PathStyle::Type, false))),
token::NtPath(panictry!(p.parse_path(PathStyle::Type)))
},
"meta" => token::NtMeta(panictry!(p.parse_meta_item())), "meta" => token::NtMeta(panictry!(p.parse_meta_item())),
"vis" => token::NtVis(panictry!(p.parse_visibility(true))), "vis" => token::NtVis(panictry!(p.parse_visibility(true))),
// this is not supposed to happen, since it has been checked // this is not supposed to happen, since it has been checked

View file

@ -84,7 +84,7 @@ pub enum PathStyle {
Expr, Expr,
/// In other contexts, notably in types, no ambiguity exists and paths can be written /// In other contexts, notably in types, no ambiguity exists and paths can be written
/// without the disambiguator, e.g. `x<y>` - unambiguously a path. /// without the disambiguator, e.g. `x<y>` - unambiguously a path.
/// Paths with disambiguators are rejected for now, but may be allowed in the future. /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
Type, Type,
/// A path with generic arguments disallowed, e.g. `foo::bar::Baz`, used in imports, /// A path with generic arguments disallowed, e.g. `foo::bar::Baz`, used in imports,
/// visibilities or attributes. /// visibilities or attributes.
@ -1755,7 +1755,7 @@ impl<'a> Parser<'a> {
self.expect(&token::ModSep)?; self.expect(&token::ModSep)?;
let qself = QSelf { ty, position: path.segments.len() }; let qself = QSelf { ty, position: path.segments.len() };
self.parse_path_segments(&mut path.segments, style)?; self.parse_path_segments(&mut path.segments, style, true)?;
Ok((qself, ast::Path { segments: path.segments, span: lo.to(self.prev_span) })) Ok((qself, ast::Path { segments: path.segments, span: lo.to(self.prev_span) }))
} }
@ -1770,8 +1770,12 @@ impl<'a> Parser<'a> {
/// `a::b::C::<D>` (with disambiguator) /// `a::b::C::<D>` (with disambiguator)
/// `Fn(Args)` (without disambiguator) /// `Fn(Args)` (without disambiguator)
/// `Fn::(Args)` (with disambiguator) /// `Fn::(Args)` (with disambiguator)
pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> {
{ self.parse_path_common(style, true)
}
pub fn parse_path_common(&mut self, style: PathStyle, enable_warning: bool)
-> PResult<'a, ast::Path> {
maybe_whole!(self, NtPath, |x| x); maybe_whole!(self, NtPath, |x| x);
let lo = self.meta_var_span.unwrap_or(self.span); let lo = self.meta_var_span.unwrap_or(self.span);
@ -1779,7 +1783,7 @@ impl<'a> Parser<'a> {
if self.eat(&token::ModSep) { if self.eat(&token::ModSep) {
segments.push(PathSegment::crate_root(lo)); segments.push(PathSegment::crate_root(lo));
} }
self.parse_path_segments(&mut segments, style)?; self.parse_path_segments(&mut segments, style, enable_warning)?;
Ok(ast::Path { segments, span: lo.to(self.prev_span) }) Ok(ast::Path { segments, span: lo.to(self.prev_span) })
} }
@ -1804,10 +1808,10 @@ impl<'a> Parser<'a> {
self.parse_path(style) self.parse_path(style)
} }
fn parse_path_segments(&mut self, segments: &mut Vec<PathSegment>, style: PathStyle) fn parse_path_segments(&mut self, segments: &mut Vec<PathSegment>, style: PathStyle,
-> PResult<'a, ()> { enable_warning: bool) -> PResult<'a, ()> {
loop { loop {
segments.push(self.parse_path_segment(style)?); segments.push(self.parse_path_segment(style, enable_warning)?);
if self.is_import_coupler() || !self.eat(&token::ModSep) { if self.is_import_coupler() || !self.eat(&token::ModSep) {
return Ok(()); return Ok(());
@ -1815,7 +1819,8 @@ impl<'a> Parser<'a> {
} }
} }
fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> { fn parse_path_segment(&mut self, style: PathStyle, enable_warning: bool)
-> PResult<'a, PathSegment> {
let ident_span = self.span; let ident_span = self.span;
let ident = self.parse_path_segment_ident()?; let ident = self.parse_path_segment_ident()?;
@ -1835,17 +1840,9 @@ impl<'a> Parser<'a> {
&& self.look_ahead(1, |t| is_args_start(t)) { && self.look_ahead(1, |t| is_args_start(t)) {
// Generic arguments are found - `<`, `(`, `::<` or `::(`. // Generic arguments are found - `<`, `(`, `::<` or `::(`.
let lo = self.span; let lo = self.span;
if self.eat(&token::ModSep) { if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning {
// These errors are not strictly necessary and may be removed in the future. self.diagnostic().struct_span_warn(self.prev_span, "unnecessary path disambiguator")
if style == PathStyle::Type { .span_label(self.prev_span, "try removing `::`").emit();
let mut err = self.diagnostic().struct_span_err(self.prev_span,
"unnecessary path disambiguator");
err.span_label(self.prev_span, "try removing `::`");
err.emit();
} else if self.token == token::OpenDelim(token::Paren) {
self.diagnostic().span_err(self.prev_span,
"`::` is not supported before parenthesized generic arguments")
}
} }
let parameters = if self.eat_lt() { let parameters = if self.eat_lt() {
@ -2382,7 +2379,7 @@ impl<'a> Parser<'a> {
// Assuming we have just parsed `.`, continue parsing into an expression. // Assuming we have just parsed `.`, continue parsing into an expression.
fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> { fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
let segment = self.parse_path_segment(PathStyle::Expr)?; let segment = self.parse_path_segment(PathStyle::Expr, true)?;
Ok(match self.token { Ok(match self.token {
token::OpenDelim(token::Paren) => { token::OpenDelim(token::Paren) => {
// Method call `expr.f()` // Method call `expr.f()`

View file

@ -19,15 +19,11 @@ fn main() {
//~^ ERROR parenthesized parameters may only be used with a trait //~^ ERROR parenthesized parameters may only be used with a trait
//~| WARN previously accepted //~| WARN previously accepted
macro_rules! pathexpr { let p = ::std::str::()::from_utf8(b"foo").unwrap();
($p:path) => { $p }
}
let p = pathexpr!(::std::str()::from_utf8)(b"foo").unwrap();
//~^ ERROR parenthesized parameters may only be used with a trait //~^ ERROR parenthesized parameters may only be used with a trait
//~| WARN previously accepted //~| WARN previously accepted
let p = pathexpr!(::std::str::from_utf8())(b"foo").unwrap(); let p = ::std::str::from_utf8::()(b"foo").unwrap();
//~^ ERROR parenthesized parameters may only be used with a trait //~^ ERROR parenthesized parameters may only be used with a trait
//~| WARN previously accepted //~| WARN previously accepted

View file

@ -8,16 +8,30 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// Unnecessary path disambiguator is ok
#![feature(rustc_attrs)]
#![allow(unused)]
macro_rules! m {
($p: path) => {
let _ = $p(0);
let _: $p;
}
}
struct Foo<T> { struct Foo<T> {
_a: T, _a: T,
} }
fn main() { struct S<T>(T);
let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>);
//~^ ERROR unnecessary path disambiguator
//~| NOTE try removing `::`
let g: Foo::<i32> = Foo { _a: 42 }; fn f() {
//~^ ERROR unnecessary path disambiguator let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>); //~ WARN unnecessary path disambiguator
//~| NOTE try removing `::` let g: Foo::<i32> = Foo { _a: 42 }; //~ WARN unnecessary path disambiguator
m!(S::<u8>); // OK, no warning
} }
#[rustc_error]
fn main() {} //~ ERROR compilation successful

View file

@ -8,9 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// compile-flags: -Z parse-only // Test that parentheses form parses in expression paths.
// Test that parentheses form doesn't work in expression paths.
struct Bar<A,R> { struct Bar<A,R> {
f: A, r: R f: A, r: R
@ -21,10 +19,10 @@ impl<A,B> Bar<A,B> {
} }
fn bar() { fn bar() {
let b = Box::Bar::<isize,usize>::new(); // OK let b = Bar::<isize, usize>::new(); // OK
let b = Box::Bar::()::new(); let b = Bar::(isize, usize)::new(); // OK too (for the parser)
//~^ ERROR `::` is not supported before parenthesized generic arguments //~^ ERROR parenthesized parameters may only be used with a trait
} }
fn main() { } fn main() {}

View file

@ -24,4 +24,6 @@ fn main() {
//~^ ERROR field expressions may not have generic arguments //~^ ERROR field expressions may not have generic arguments
f.x::<>; f.x::<>;
//~^ ERROR field expressions may not have generic arguments //~^ ERROR field expressions may not have generic arguments
f.x::();
//~^ ERROR field expressions may not have generic arguments
} }