Skip to content

Commit 0ad2026

Browse files
committed
Auto merge of #25171 - quantheory:associated_time_long_paths, r=nikomatsakis
It is currently broken to use syntax such as `<T as Foo>::U::static_method()` where `<T as Foo>::U` is an associated type. I was able to fix this and simplify the parser a bit at the same time. This also fixes the corresponding issue with associated types (#22139), but that's somewhat irrelevant because #22519 is still open, so this syntax still causes an error in type checking. Similarly, although this fix applies to associated consts, #25046 forbids associated constants from using type parameters or `Self`, while #19559 means that associated types have to always have one of those two. Therefore, I think that you can't use an associated const from an associated type anyway.
2 parents 67dfc17 + efb3872 commit 0ad2026

File tree

3 files changed

+83
-45
lines changed

3 files changed

+83
-45
lines changed

Diff for: src/librustc_resolve/lib.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -2688,18 +2688,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26882688
check_ribs: bool)
26892689
-> AssocItemResolveResult
26902690
{
2691+
let max_assoc_types;
2692+
26912693
match maybe_qself {
2692-
Some(&ast::QSelf { position: 0, .. }) =>
2693-
return TypecheckRequired,
2694-
_ => {}
2694+
Some(qself) => {
2695+
if qself.position == 0 {
2696+
return TypecheckRequired;
2697+
}
2698+
max_assoc_types = path.segments.len() - qself.position;
2699+
// Make sure the trait is valid.
2700+
let _ = self.resolve_trait_reference(id, path, max_assoc_types);
2701+
}
2702+
None => {
2703+
max_assoc_types = path.segments.len();
2704+
}
26952705
}
2696-
let max_assoc_types = if let Some(qself) = maybe_qself {
2697-
// Make sure the trait is valid.
2698-
let _ = self.resolve_trait_reference(id, path, 1);
2699-
path.segments.len() - qself.position
2700-
} else {
2701-
path.segments.len()
2702-
};
27032706

27042707
let mut resolution = self.with_no_errors(|this| {
27052708
this.resolve_path(id, path, 0, namespace, check_ribs)

Diff for: src/libsyntax/parse/parser.rs

+15-35
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ pub enum PathParsingMode {
109109
LifetimeAndTypesWithColons,
110110
}
111111

112-
/// How to parse a qualified path, whether to allow trailing parameters.
113-
#[derive(Copy, Clone, PartialEq)]
114-
pub enum QPathParsingMode {
115-
/// No trailing parameters, e.g. `<T as Trait>::Item`
116-
NoParameters,
117-
/// Optional parameters, e.g. `<T as Trait>::item::<'a, U>`
118-
MaybeParameters,
119-
}
120-
121112
/// How to parse a bound, whether to allow bound modifiers such as `?`.
122113
#[derive(Copy, Clone, PartialEq)]
123114
pub enum BoundParsingMode {
@@ -1359,7 +1350,7 @@ impl<'a> Parser<'a> {
13591350
} else if try!(self.eat_lt()) {
13601351

13611352
let (qself, path) =
1362-
try!(self.parse_qualified_path(QPathParsingMode::NoParameters));
1353+
try!(self.parse_qualified_path(NoTypesAllowed));
13631354

13641355
TyPath(Some(qself), path)
13651356
} else if self.check(&token::ModSep) ||
@@ -1578,7 +1569,7 @@ impl<'a> Parser<'a> {
15781569

15791570
// QUALIFIED PATH `<TYPE [as TRAIT_REF]>::IDENT[::<PARAMS>]`
15801571
// Assumes that the leading `<` has been parsed already.
1581-
pub fn parse_qualified_path(&mut self, mode: QPathParsingMode)
1572+
pub fn parse_qualified_path(&mut self, mode: PathParsingMode)
15821573
-> PResult<(QSelf, ast::Path)> {
15831574
let self_type = try!(self.parse_ty_sum());
15841575
let mut path = if try!(self.eat_keyword(keywords::As)) {
@@ -1599,29 +1590,18 @@ impl<'a> Parser<'a> {
15991590
try!(self.expect(&token::Gt));
16001591
try!(self.expect(&token::ModSep));
16011592

1602-
let item_name = try!(self.parse_ident());
1603-
let parameters = match mode {
1604-
QPathParsingMode::NoParameters => ast::PathParameters::none(),
1605-
QPathParsingMode::MaybeParameters => {
1606-
if try!(self.eat(&token::ModSep)) {
1607-
try!(self.expect_lt());
1608-
// Consumed `item::<`, go look for types
1609-
let (lifetimes, types, bindings) =
1610-
try!(self.parse_generic_values_after_lt());
1611-
ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1612-
lifetimes: lifetimes,
1613-
types: OwnedSlice::from_vec(types),
1614-
bindings: OwnedSlice::from_vec(bindings),
1615-
})
1616-
} else {
1617-
ast::PathParameters::none()
1618-
}
1593+
let segments = match mode {
1594+
LifetimeAndTypesWithoutColons => {
1595+
try!(self.parse_path_segments_without_colons())
1596+
}
1597+
LifetimeAndTypesWithColons => {
1598+
try!(self.parse_path_segments_with_colons())
1599+
}
1600+
NoTypesAllowed => {
1601+
try!(self.parse_path_segments_without_types())
16191602
}
16201603
};
1621-
path.segments.push(ast::PathSegment {
1622-
identifier: item_name,
1623-
parameters: parameters
1624-
});
1604+
path.segments.extend(segments);
16251605

16261606
if path.segments.len() == 1 {
16271607
path.span.lo = self.last_span.lo;
@@ -2096,7 +2076,7 @@ impl<'a> Parser<'a> {
20962076
if try!(self.eat_lt()){
20972077

20982078
let (qself, path) =
2099-
try!(self.parse_qualified_path(QPathParsingMode::MaybeParameters));
2079+
try!(self.parse_qualified_path(LifetimeAndTypesWithColons));
21002080

21012081
return Ok(self.mk_expr(lo, hi, ExprPath(Some(qself), path)));
21022082
}
@@ -3176,7 +3156,7 @@ impl<'a> Parser<'a> {
31763156
let (qself, path) = if try!(self.eat_lt()) {
31773157
// Parse a qualified path
31783158
let (qself, path) =
3179-
try!(self.parse_qualified_path(QPathParsingMode::NoParameters));
3159+
try!(self.parse_qualified_path(NoTypesAllowed));
31803160
(Some(qself), path)
31813161
} else {
31823162
// Parse an unqualified path
@@ -3270,7 +3250,7 @@ impl<'a> Parser<'a> {
32703250
let (qself, path) = if try!(self.eat_lt()) {
32713251
// Parse a qualified path
32723252
let (qself, path) =
3273-
try!(self.parse_qualified_path(QPathParsingMode::NoParameters));
3253+
try!(self.parse_qualified_path(NoTypesAllowed));
32743254
(Some(qself), path)
32753255
} else {
32763256
// Parse an unqualified path

Diff for: src/test/run-pass/associated-item-long-paths.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2015 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+
use std::mem::size_of;
12+
13+
// The main point of this test is to ensure that we can parse and resolve
14+
// associated items on associated types.
15+
16+
trait Foo {
17+
type U;
18+
}
19+
20+
trait Bar {
21+
// Note 1: Chains of associated items in a path won't type-check.
22+
// Note 2: Associated consts can't depend on type parameters or `Self`,
23+
// which are the only types that an associated type can be referenced on for
24+
// now, so we can only test methods.
25+
fn method() -> u32;
26+
fn generic_method<T>() -> usize;
27+
}
28+
29+
struct MyFoo;
30+
struct MyBar;
31+
32+
impl Foo for MyFoo {
33+
type U = MyBar;
34+
}
35+
36+
impl Bar for MyBar {
37+
fn method() -> u32 {
38+
2u32
39+
}
40+
fn generic_method<T>() -> usize {
41+
size_of::<T>()
42+
}
43+
}
44+
45+
fn foo<T>()
46+
where T: Foo,
47+
T::U: Bar,
48+
{
49+
assert_eq!(2u32, <T as Foo>::U::method());
50+
assert_eq!(8usize, <T as Foo>::U::generic_method::<f64>());
51+
}
52+
53+
fn main() {
54+
foo::<MyFoo>();
55+
}

0 commit comments

Comments
 (0)