Skip to content

Commit 128f565

Browse files
committed
Give span to angle bracketed generic arguments
1 parent ce3beb6 commit 128f565

File tree

15 files changed

+166
-58
lines changed

15 files changed

+166
-58
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ impl<'a> LoweringContext<'a> {
865865
data: &AngleBracketedParameterData,
866866
param_mode: ParamMode)
867867
-> hir::AngleBracketedParameterData {
868-
let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings } = data;
868+
let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings, .. } = data;
869869
hir::AngleBracketedParameterData {
870870
lifetimes: self.lower_lifetimes(lifetimes),
871871
types: types.iter().map(|ty| self.lower_ty(ty)).collect(),

src/librustc_passes/ast_validation.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
195195
match item.node {
196196
ItemKind::Use(ref view_path) => {
197197
let path = view_path.node.path();
198-
if path.segments.iter().any(|segment| segment.parameters.is_some()) {
199-
self.err_handler()
200-
.span_err(path.span, "generic arguments in import path");
201-
}
198+
path.segments.iter().find(|segment| segment.parameters.is_some()).map(|segment| {
199+
self.err_handler().span_err(segment.parameters.as_ref().unwrap().span(),
200+
"generic arguments in import path");
201+
});
202202
}
203203
ItemKind::Impl(.., Some(..), _, ref impl_items) => {
204204
self.invalid_visibility(&item.vis, item.span, None);
@@ -297,10 +297,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
297297
fn visit_vis(&mut self, vis: &'a Visibility) {
298298
match *vis {
299299
Visibility::Restricted { ref path, .. } => {
300-
if path.segments.iter().any(|segment| segment.parameters.is_some()) {
301-
self.err_handler()
302-
.span_err(path.span, "generic arguments in visibility path");
303-
}
300+
path.segments.iter().find(|segment| segment.parameters.is_some()).map(|segment| {
301+
self.err_handler().span_err(segment.parameters.as_ref().unwrap().span(),
302+
"generic arguments in visibility path");
303+
});
304304
}
305305
_ => {}
306306
}

src/librustc_resolve/macros.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,10 @@ impl<'a> Resolver<'a> {
386386
fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
387387
-> Result<Def, Determinacy> {
388388
let ast::Path { ref segments, span } = *path;
389-
if segments.iter().any(|segment| segment.parameters.is_some()) {
390-
self.session.span_err(span, "generic arguments in macro path");
391-
}
389+
segments.iter().find(|segment| segment.parameters.is_some()).map(|segment| {
390+
self.session.span_err(segment.parameters.as_ref().unwrap().span(),
391+
"generic arguments in macro path");
392+
});
392393

393394
let path: Vec<_> = segments.iter().map(|seg| respan(seg.span, seg.identifier)).collect();
394395
let invocation = self.invocations[&scope];

src/libsyntax/ast.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,19 @@ pub enum PathParameters {
153153
}
154154

155155
impl PathParameters {
156-
pub fn span(&self, fallback: Span) -> Span {
156+
pub fn span(&self) -> Span {
157157
match *self {
158-
AngleBracketed(ref data) => {
159-
data.lifetimes.get(0).map(|x| x.span).or_else(||
160-
data.types.get(0).map(|x| x.span)).or_else(||
161-
data.bindings.get(0).map(|x| x.span)).unwrap_or(fallback)
162-
}
163-
Parenthesized(ref data) => data.span
158+
AngleBracketed(ref data) => data.span,
159+
Parenthesized(ref data) => data.span,
164160
}
165161
}
166162
}
167163

168164
/// A path like `Foo<'a, T>`
169165
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Default)]
170166
pub struct AngleBracketedParameterData {
167+
/// Overall span
168+
pub span: Span,
171169
/// The lifetime parameters for this path segment.
172170
pub lifetimes: Vec<Lifetime>,
173171
/// The type parameters for this path segment, if present.

src/libsyntax/ext/build.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
312312
self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
313313
}
314314
fn path_all(&self,
315-
sp: Span,
315+
span: Span,
316316
global: bool,
317317
mut idents: Vec<ast::Ident> ,
318318
lifetimes: Vec<ast::Lifetime>,
@@ -322,24 +322,17 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
322322
let last_identifier = idents.pop().unwrap();
323323
let mut segments: Vec<ast::PathSegment> = Vec::new();
324324
if global {
325-
segments.push(ast::PathSegment::crate_root(sp));
325+
segments.push(ast::PathSegment::crate_root(span));
326326
}
327327

328-
segments.extend(idents.into_iter().map(|i| ast::PathSegment::from_ident(i, sp)));
328+
segments.extend(idents.into_iter().map(|i| ast::PathSegment::from_ident(i, span)));
329329
let parameters = if !lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty() {
330-
ast::AngleBracketedParameterData { lifetimes, types, bindings }.into()
330+
ast::AngleBracketedParameterData { lifetimes, types, bindings, span }.into()
331331
} else {
332332
None
333333
};
334-
segments.push(ast::PathSegment {
335-
identifier: last_identifier,
336-
span: sp,
337-
parameters: parameters
338-
});
339-
ast::Path {
340-
span: sp,
341-
segments: segments,
342-
}
334+
segments.push(ast::PathSegment { identifier: last_identifier, span, parameters });
335+
ast::Path { span, segments }
343336
}
344337

345338
/// Constructs a qualified path.
@@ -366,7 +359,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
366359
-> (ast::QSelf, ast::Path) {
367360
let mut path = trait_path;
368361
let parameters = if !lifetimes.is_empty() || !types.is_empty() || !bindings.is_empty() {
369-
ast::AngleBracketedParameterData { lifetimes, types, bindings }.into()
362+
ast::AngleBracketedParameterData { lifetimes, types, bindings, span: ident.span }.into()
370363
} else {
371364
None
372365
};

src/libsyntax/fold.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,11 @@ pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedP
471471
fld: &mut T)
472472
-> AngleBracketedParameterData
473473
{
474-
let AngleBracketedParameterData { lifetimes, types, bindings } = data;
474+
let AngleBracketedParameterData { lifetimes, types, bindings, span } = data;
475475
AngleBracketedParameterData { lifetimes: fld.fold_lifetimes(lifetimes),
476476
types: types.move_map(|ty| fld.fold_ty(ty)),
477-
bindings: bindings.move_map(|b| fld.fold_ty_binding(b)) }
477+
bindings: bindings.move_map(|b| fld.fold_ty_binding(b)),
478+
span: fld.new_span(span) }
478479
}
479480

480481
pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,8 +1808,8 @@ impl<'a> Parser<'a> {
18081808
// `<'a, T, A = U>`
18091809
let (lifetimes, types, bindings) = self.parse_generic_args()?;
18101810
self.expect_gt()?;
1811-
let _span = lo.to(self.prev_span);
1812-
AngleBracketedParameterData { lifetimes, types, bindings }.into()
1811+
let span = lo.to(self.prev_span);
1812+
AngleBracketedParameterData { lifetimes, types, bindings, span }.into()
18131813
} else {
18141814
// `(T, U) -> R`
18151815
self.bump(); // `(`
@@ -2357,7 +2357,7 @@ impl<'a> Parser<'a> {
23572357
_ => {
23582358
// Field access `expr.f`
23592359
if let Some(parameters) = segment.parameters {
2360-
self.span_err(parameters.span(segment.span),
2360+
self.span_err(parameters.span(),
23612361
"field expressions may not have generic arguments");
23622362
}
23632363

src/libsyntax_pos/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ pub struct SpanLabel {
239239
pub label: Option<String>,
240240
}
241241

242+
impl Default for Span {
243+
fn default() -> Self {
244+
DUMMY_SP
245+
}
246+
}
247+
242248
impl serialize::UseSpecializedEncodable for Span {
243249
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
244250
s.emit_struct("Span", 2, |s| {

src/test/compile-fail/macro-with-seps-err-msg.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,8 @@
1010

1111
// gate-test-use_extern_macros
1212

13-
macro_rules! m {
14-
($p1: path) => {
15-
#[derive($p1)] struct U;
16-
}
17-
}
18-
1913
fn main() {
2014
globnar::brotz!(); //~ ERROR non-ident macro paths are experimental
2115
#[derive(foo::Bar)] struct T; //~ ERROR non-ident macro paths are experimental
2216
::foo!(); //~ ERROR non-ident macro paths are experimental
23-
24-
foo::<T>!();
25-
//~^ ERROR generic arguments in macro path
26-
//~| ERROR generic arguments in macro path
27-
//~| ERROR generic arguments in macro path
28-
foo::<>!();
29-
//~^ ERROR generic arguments in macro path
30-
//~| ERROR generic arguments in macro path
31-
//~| ERROR generic arguments in macro path
32-
m!(MyTrait<>);
33-
//~^ ERROR generic arguments in macro path
34-
//~| ERROR generic arguments in macro path
35-
//~| ERROR generic arguments in macro path
36-
//~| ERROR generic arguments in macro path
3717
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: generic arguments in import path
2+
--> $DIR/import-ty-params.rs:24:25
3+
|
4+
24 | import! { a::b::c::S<u8> } //~ ERROR generic arguments in import path
5+
| ^^^^
6+
7+
error: generic arguments in import path
8+
--> $DIR/import-ty-params.rs:27:25
9+
|
10+
27 | import! { a::b::c::S<> } //~ ERROR generic arguments in import path
11+
| ^^
12+
13+
error: aborting due to 2 previous errors
14+

src/test/ui/span/macro-ty-params.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! m {
12+
($p1: path) => {
13+
#[derive($p1)] struct U;
14+
}
15+
}
16+
17+
fn main() {
18+
foo::<T>!();
19+
//~^ ERROR generic arguments in macro path
20+
//~| ERROR generic arguments in macro path
21+
//~| ERROR generic arguments in macro path
22+
foo::<>!();
23+
//~^ ERROR generic arguments in macro path
24+
//~| ERROR generic arguments in macro path
25+
//~| ERROR generic arguments in macro path
26+
m!(MyTrait<>);
27+
//~^ ERROR generic arguments in macro path
28+
//~| ERROR generic arguments in macro path
29+
//~| ERROR generic arguments in macro path
30+
//~| ERROR generic arguments in macro path
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: generic arguments in macro path
2+
--> $DIR/macro-ty-params.rs:18:8
3+
|
4+
18 | foo::<T>!();
5+
| ^^^^^
6+
7+
error: generic arguments in macro path
8+
--> $DIR/macro-ty-params.rs:22:8
9+
|
10+
22 | foo::<>!();
11+
| ^^^^
12+
13+
error: generic arguments in macro path
14+
--> $DIR/macro-ty-params.rs:26:15
15+
|
16+
26 | m!(MyTrait<>);
17+
| ^^
18+
19+
error: generic arguments in macro path
20+
--> $DIR/macro-ty-params.rs:26:15
21+
|
22+
26 | m!(MyTrait<>);
23+
| ^^
24+
25+
error: generic arguments in macro path
26+
--> $DIR/macro-ty-params.rs:26:15
27+
|
28+
26 | m!(MyTrait<>);
29+
| ^^
30+
31+
error: generic arguments in macro path
32+
--> $DIR/macro-ty-params.rs:22:8
33+
|
34+
22 | foo::<>!();
35+
| ^^^^
36+
37+
error: generic arguments in macro path
38+
--> $DIR/macro-ty-params.rs:18:8
39+
|
40+
18 | foo::<T>!();
41+
| ^^^^^
42+
43+
error: generic arguments in macro path
44+
--> $DIR/macro-ty-params.rs:18:8
45+
|
46+
18 | foo::<T>!();
47+
| ^^^^^
48+
49+
error: generic arguments in macro path
50+
--> $DIR/macro-ty-params.rs:22:8
51+
|
52+
22 | foo::<>!();
53+
| ^^^^
54+
55+
error: generic arguments in macro path
56+
--> $DIR/macro-ty-params.rs:26:15
57+
|
58+
26 | m!(MyTrait<>);
59+
| ^^
60+
61+
error: aborting due to 10 previous errors
62+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0577]: expected module, found struct `S`
2+
--> $DIR/visibility-ty-params.rs:16:5
3+
|
4+
16 | m!{ S<u8> } //~ ERROR generic arguments in visibility path
5+
| -^^^^
6+
| |
7+
| did you mean `m`?
8+
9+
error: generic arguments in visibility path
10+
--> $DIR/visibility-ty-params.rs:16:6
11+
|
12+
16 | m!{ S<u8> } //~ ERROR generic arguments in visibility path
13+
| ^^^^
14+
15+
error: generic arguments in visibility path
16+
--> $DIR/visibility-ty-params.rs:20:10
17+
|
18+
20 | m!{ m<> } //~ ERROR generic arguments in visibility path
19+
| ^^
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)