aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/items/traits.rs
blob: 0614e8ab681ecc88cb4181f60060fe8d408de5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use super::*;

// test trait_item
// trait T<U>: Hash + Clone where U: Copy {}
pub(super) fn trait_item(p: &mut Parser) {
    assert!(p.at(TRAIT_KW));
    p.bump();
    name(p);
    type_params::type_param_list(p);
    if p.at(COLON) {
        type_params::bounds(p);
    }
    type_params::where_clause(p);
    p.expect(L_CURLY);
    p.expect(R_CURLY);
}

// test impl_item
// impl Foo {}
pub(super) fn impl_item(p: &mut Parser) {
    assert!(p.at(IMPL_KW));
    p.bump();
    if choose_type_params_over_qpath(p) {
        type_params::type_param_list(p);
    }

    // TODO: never type
    // impl ! {}

    // test impl_item_neg
    // impl !Send for X {}
    p.eat(EXCL);
    types::type_(p);
    if p.eat(FOR_KW) {
        types::type_(p);
    }
    type_params::where_clause(p);
    p.expect(L_CURLY);

    // test impl_item_items
    // impl F {
    //     type A = i32;
    //     const B: i32 = 92;
    //     fn foo() {}
    //     fn bar(&self) {}
    // }
    while !p.at(EOF) && !p.at(R_CURLY) {
        item(p, true);
    }
    p.expect(R_CURLY);
}

fn choose_type_params_over_qpath(p: &Parser) -> bool {
    // There's an ambiguity between generic parameters and qualified paths in impls.
    // If we see `<` it may start both, so we have to inspect some following tokens.
    // The following combinations can only start generics,
    // but not qualified paths (with one exception):
    //     `<` `>` - empty generic parameters
    //     `<` `#` - generic parameters with attributes
    //     `<` (LIFETIME|IDENT) `>` - single generic parameter
    //     `<` (LIFETIME|IDENT) `,` - first generic parameter in a list
    //     `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
    //     `<` (LIFETIME|IDENT) `=` - generic parameter with a default
    // The only truly ambiguous case is
    //     `<` IDENT `>` `::` IDENT ...
    // we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
    // because this is what almost always expected in practice, qualified paths in impls
    // (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment.
    if !p.at(L_ANGLE) {
        return false;
    }
    if p.nth(1) == POUND || p.nth(1) == R_ANGLE {
        return true;
    }
    (p.nth(1) == LIFETIME || p.nth(1) == IDENT)
        && (p.nth(2) == R_ANGLE || p.nth(2) == COMMA || p.nth(2) == COLON || p.nth(2) == EQ)
}