LCOV - code coverage report
Current view: top level - gcc/rust/parse - rust-parse-impl.hxx (source / functions) Coverage Total Hit
Test: gcc.info Lines: 77.7 % 3250 2525
Test Date: 2026-11-14 16:39:50 Functions: 79.7 % 212 169
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : /* Template implementation for Rust::Parser. Previously in rust-parse.cc (before
      20              :  * Parser was template). Separated from rust-parse.h for readability. */
      21              : 
      22              : /* DO NOT INCLUDE ANYWHERE - this is automatically included
      23              :  *   by rust-parse-impl-*.cc
      24              :  * This is also the reason why there are no include guards. */
      25              : 
      26              : #include "expected.h"
      27              : #include "rust-ast.h"
      28              : #include "rust-common.h"
      29              : #include "rust-expr.h"
      30              : #include "rust-item.h"
      31              : #include "rust-common.h"
      32              : #include "rust-parse.h"
      33              : #include "rust-token.h"
      34              : #define INCLUDE_ALGORITHM
      35              : #include "rust-diagnostics.h"
      36              : #include "rust-dir-owner.h"
      37              : #include "rust-keyword-values.h"
      38              : #include "rust-edition.h"
      39              : #include "rust-parse-error.h"
      40              : 
      41              : #include "optional.h"
      42              : 
      43              : namespace Rust {
      44              : 
      45              : /* HACK-y special handling for skipping a right angle token at the end of
      46              :  * generic arguments.
      47              :  * Currently, this replaces the "current token" with one that is identical
      48              :  * except has the leading '>' removed (e.g. '>>' becomes '>'). This is bad
      49              :  * for several reasons - it modifies the token stream to something that
      50              :  * actually doesn't make syntactic sense, it may not worked if the token
      51              :  * has already been skipped, etc. It was done because it would not
      52              :  * actually require inserting new items into the token stream (which I
      53              :  * thought would take more work to not mess up) and because I wasn't sure
      54              :  * if the "already seen right angle" flag in the parser would work
      55              :  * correctly.
      56              :  * Those two other approaches listed are in my opinion actually better
      57              :  * long-term - insertion is probably best as it reflects syntactically
      58              :  * what occurs. On the other hand, I need to do a code audit to make sure
      59              :  * that insertion doesn't mess anything up. So that's a FIXME. */
      60              : template <typename ManagedTokenSource>
      61              : bool
      62       166283 : Parser<ManagedTokenSource>::skip_generics_right_angle ()
      63              : {
      64              :   /* OK, new great idea. Have a lexer method called
      65              :    * "split_current_token(TokenType newLeft, TokenType newRight)", which is
      66              :    * called here with whatever arguments are appropriate. That lexer method
      67              :    * handles "replacing" the current token with the "newLeft" and "inserting"
      68              :    * the next token with the "newRight" (and creating a location, etc. for it)
      69              :    */
      70              : 
      71              :   /* HACK: special handling for right shift '>>', greater or equal '>=', and
      72              :    * right shift assig */
      73              :   // '>>='
      74       166283 :   const_TokenPtr tok = lexer.peek_token ();
      75       166283 :   switch (tok->get_id ())
      76              :     {
      77       138219 :     case RIGHT_ANGLE:
      78              :       // this is good - skip token
      79       138219 :       lexer.skip_token ();
      80       138219 :       return true;
      81        28063 :     case RIGHT_SHIFT:
      82              :       {
      83              :         // new implementation that should be better
      84        28063 :         lexer.split_current_token (RIGHT_ANGLE, RIGHT_ANGLE);
      85        28063 :         lexer.skip_token ();
      86        28063 :         return true;
      87              :       }
      88            0 :     case GREATER_OR_EQUAL:
      89              :       {
      90              :         // new implementation that should be better
      91            0 :         lexer.split_current_token (RIGHT_ANGLE, EQUAL);
      92            0 :         lexer.skip_token ();
      93            0 :         return true;
      94              :       }
      95            0 :     case RIGHT_SHIFT_EQ:
      96              :       {
      97              :         // new implementation that should be better
      98            0 :         lexer.split_current_token (RIGHT_ANGLE, GREATER_OR_EQUAL);
      99            0 :         lexer.skip_token ();
     100            0 :         return true;
     101              :       }
     102            1 :     default:
     103            1 :       add_error (Error (tok->get_locus (),
     104              :                         "expected %<>%> at end of generic argument - found %qs",
     105              :                         tok->get_token_description ()));
     106            1 :       return false;
     107              :     }
     108       166283 : }
     109              : 
     110              : /* Gets left binding power for specified token.
     111              :  * Not suitable for use at the moment or possibly ever because binding power
     112              :  * cannot be purely determined from operator token with Rust grammar - e.g.
     113              :  * method call and field access have
     114              :  * different left binding powers but the same operator token. */
     115              : template <typename ManagedTokenSource>
     116              : int
     117      6226401 : Parser<ManagedTokenSource>::left_binding_power (const_TokenPtr token)
     118              : {
     119              :   // HACK: called with "peek_token()", so lookahead is "peek_token(1)"
     120      6226401 :   switch (token->get_id ())
     121              :     {
     122              :       /* TODO: issue here - distinguish between method calls and field access
     123              :        * somehow? Also would have to distinguish between paths and function
     124              :        * calls (:: operator), maybe more stuff. */
     125              :       /* Current plan for tackling LBP - don't do it based on token, use
     126              :        * lookahead. Or alternatively, only use Pratt parsing for OperatorExpr
     127              :        * and handle other expressions without it. rustc only considers
     128              :        * arithmetic, logical/relational, 'as',
     129              :        * '?=', ranges, colons, and assignment to have operator precedence and
     130              :        * associativity rules applicable. It then has
     131              :        * a separate "ExprPrecedence" that also includes binary operators. */
     132              : 
     133              :       // TODO: handle operator overloading - have a function replace the
     134              :       // operator?
     135              : 
     136              :       /*case DOT:
     137              :           return LBP_DOT;*/
     138              : 
     139            0 :     case SCOPE_RESOLUTION:
     140            0 :       rust_debug (
     141              :         "possible error - looked up LBP of scope resolution operator. should "
     142              :         "be handled elsewhere.");
     143              :       return LBP_PATH;
     144              : 
     145              :     /* Resolved by lookahead HACK that should work with current code. If next
     146              :      * token is identifier and token after that isn't parenthesised expression
     147              :      * list, it is a field reference. */
     148       315172 :     case DOT:
     149       630344 :       if (lexer.peek_token (1)->get_id () == IDENTIFIER
     150       564667 :           && lexer.peek_token (2)->get_id () != LEFT_PAREN)
     151              :         {
     152              :           return LBP_FIELD_EXPR;
     153              :         }
     154              :       return LBP_METHOD_CALL;
     155              : 
     156              :     case LEFT_PAREN:
     157              :       return LBP_FUNCTION_CALL;
     158              : 
     159              :     case LEFT_SQUARE:
     160              :       return LBP_ARRAY_REF;
     161              : 
     162              :     // postfix question mark (i.e. error propagation expression)
     163         1664 :     case QUESTION_MARK:
     164         1664 :       return LBP_QUESTION_MARK;
     165              : 
     166        43974 :     case AS:
     167        43974 :       return LBP_AS;
     168              : 
     169              :     case ASTERISK:
     170              :       return LBP_MUL;
     171              :     case DIV:
     172              :       return LBP_DIV;
     173              :     case PERCENT:
     174              :       return LBP_MOD;
     175              : 
     176              :     case PLUS:
     177              :       return LBP_PLUS;
     178              :     case MINUS:
     179              :       return LBP_MINUS;
     180              : 
     181              :     case LEFT_SHIFT:
     182              :       return LBP_L_SHIFT;
     183              :     case RIGHT_SHIFT:
     184              :       return LBP_R_SHIFT;
     185              : 
     186              :     // binary & operator
     187        34627 :     case AMP:
     188        34627 :       return LBP_AMP;
     189              : 
     190              :     // binary ^ operator
     191          738 :     case CARET:
     192          738 :       return LBP_CARET;
     193              : 
     194              :     // binary | operator
     195        25656 :     case PIPE:
     196        25656 :       return LBP_PIPE;
     197              : 
     198              :     case EQUAL_EQUAL:
     199              :       return LBP_EQUAL;
     200              :     case NOT_EQUAL:
     201              :       return LBP_NOT_EQUAL;
     202              :     case RIGHT_ANGLE:
     203              :       return LBP_GREATER_THAN;
     204              :     case GREATER_OR_EQUAL:
     205              :       return LBP_GREATER_EQUAL;
     206              :     case LEFT_ANGLE:
     207              :       return LBP_SMALLER_THAN;
     208              :     case LESS_OR_EQUAL:
     209              :       return LBP_SMALLER_EQUAL;
     210              : 
     211         6063 :     case LOGICAL_AND:
     212         6063 :       return LBP_LOGICAL_AND;
     213              : 
     214         5046 :     case OR:
     215         5046 :       return LBP_LOGICAL_OR;
     216              : 
     217              :     case DOT_DOT:
     218              :       return LBP_DOT_DOT;
     219              : 
     220              :     case DOT_DOT_EQ:
     221              :       return LBP_DOT_DOT_EQ;
     222              : 
     223              :     case EQUAL:
     224              :       return LBP_ASSIG;
     225              :     case PLUS_EQ:
     226              :       return LBP_PLUS_ASSIG;
     227              :     case MINUS_EQ:
     228              :       return LBP_MINUS_ASSIG;
     229              :     case ASTERISK_EQ:
     230              :       return LBP_MULT_ASSIG;
     231              :     case DIV_EQ:
     232              :       return LBP_DIV_ASSIG;
     233              :     case PERCENT_EQ:
     234              :       return LBP_MOD_ASSIG;
     235              :     case AMP_EQ:
     236              :       return LBP_AMP_ASSIG;
     237              :     case PIPE_EQ:
     238              :       return LBP_PIPE_ASSIG;
     239              :     case CARET_EQ:
     240              :       return LBP_CARET_ASSIG;
     241              :     case LEFT_SHIFT_EQ:
     242              :       return LBP_L_SHIFT_ASSIG;
     243              :     case RIGHT_SHIFT_EQ:
     244              :       return LBP_R_SHIFT_ASSIG;
     245              : 
     246              :     /* HACK: float literal due to lexer misidentifying a dot then an integer as
     247              :      * a float */
     248              :     case FLOAT_LITERAL:
     249              :       return LBP_FIELD_EXPR;
     250              :       // field expr is same as tuple expr in precedence, i imagine
     251              :       // TODO: is this needed anymore? lexer shouldn't do that anymore
     252              : 
     253              :     // anything that can't appear in an infix position is given lowest priority
     254      5582709 :     default:
     255      5582709 :       return LBP_LOWEST;
     256              :     }
     257              : }
     258              : 
     259              : // Returns true when current token is EOF.
     260              : template <typename ManagedTokenSource>
     261              : bool
     262            0 : Parser<ManagedTokenSource>::done_end_of_file ()
     263              : {
     264            0 :   return lexer.peek_token ()->get_id () == END_OF_FILE;
     265              : }
     266              : 
     267              : // Parses a sequence of items within a module or the implicit top-level module
     268              : // in a crate
     269              : template <typename ManagedTokenSource>
     270              : tl::expected<std::vector<std::unique_ptr<AST::Item>>, Parse::Error::Items>
     271         7174 : Parser<ManagedTokenSource>::parse_items ()
     272              : {
     273         7174 :   std::vector<std::unique_ptr<AST::Item>> items;
     274              : 
     275         7174 :   const_TokenPtr t = lexer.peek_token ();
     276        73762 :   while (t->get_id () != END_OF_FILE)
     277              :     {
     278        66588 :       auto item = parse_item (false);
     279        66588 :       if (!item)
     280           87 :         return Parse::Error::Items::make_malformed (std::move (items));
     281              : 
     282        66501 :       items.push_back (std::move (item.value ()));
     283              : 
     284        66501 :       t = lexer.peek_token ();
     285              :     }
     286              : 
     287              :     // GCC 5->7 bug doesn't threat lvalue as an rvalue for the overload
     288              : #if __GNUC__ <= 7
     289              :   return std::move (items);
     290              : #else
     291         7087 :   return items;
     292              : #endif
     293         7174 : }
     294              : 
     295              : // Parses a crate (compilation unit) - entry point
     296              : template <typename ManagedTokenSource>
     297              : std::unique_ptr<AST::Crate>
     298         5478 : Parser<ManagedTokenSource>::parse_crate ()
     299              : {
     300              :   // parse inner attributes
     301         5478 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
     302              : 
     303              :   // parse items
     304        10956 :   auto items
     305         5478 :     = parse_items ().value_or (std::vector<std::unique_ptr<AST::Item>>{});
     306              : 
     307              :   // emit all errors
     308         5667 :   for (const auto &error : error_table)
     309          189 :     error.emit ();
     310              : 
     311              :   return std::unique_ptr<AST::Crate> (
     312         5478 :     new AST::Crate (std::move (items), std::move (inner_attrs)));
     313         5478 : }
     314              : 
     315              : // Parses an identifier/keyword as a Token
     316              : template <typename ManagedTokenSource>
     317              : tl::expected<std::unique_ptr<AST::Token>, Parse::Error::Node>
     318       103292 : Parser<ManagedTokenSource>::parse_identifier_or_keyword_token ()
     319              : {
     320       103292 :   const_TokenPtr t = lexer.peek_token ();
     321              : 
     322       103292 :   if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ()))
     323              :     {
     324       103270 :       lexer.skip_token ();
     325       103270 :       return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
     326              :     }
     327              :   else
     328              :     {
     329           22 :       add_error (Error (t->get_locus (), "expected keyword or identifier"));
     330           22 :       return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
     331              :     }
     332       103292 : }
     333              : 
     334              : template <typename ManagedTokenSource>
     335              : bool
     336        31458 : Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
     337              : {
     338        31458 :   auto macro_name = lexer.peek_token (2)->get_id ();
     339              : 
     340        31458 :   bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
     341              : 
     342        31458 :   return t->get_str () == Values::WeakKeywords::MACRO_RULES
     343        31508 :          && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
     344              : }
     345              : 
     346              : // Parses a single item
     347              : template <typename ManagedTokenSource>
     348              : tl::expected<std::unique_ptr<AST::Item>, Parse::Error::Item>
     349       157432 : Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
     350              : {
     351              :   // has a "called_from_statement" parameter for better error message handling
     352              : 
     353              :   // TODO: GCC 5 does not handle implicit return type correctly so we're forced
     354              :   // to specify it almost every time until the baseline GCC gets bumped.
     355              :   // Since this type is quite long and the code is dense we use an alias.
     356              :   //
     357              :   // When support for GCC 5 stops: remove this alias as well as the explicit
     358              :   // ctor calls.
     359              :   using RType = tl::expected<std::unique_ptr<AST::Item>, Parse::Error::Item>;
     360              : 
     361              :   // parse outer attributes for item
     362       157432 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
     363       157432 :   const_TokenPtr t = lexer.peek_token ();
     364              : 
     365       157432 :   switch (t->get_id ())
     366              :     {
     367            7 :     case END_OF_FILE:
     368              :       // not necessarily an error, unless we just read outer
     369              :       // attributes which needs to be attached
     370            7 :       if (!outer_attrs.empty ())
     371              :         {
     372            0 :           Rust::AST::Attribute attr = outer_attrs.back ();
     373            0 :           Error error (attr.get_locus (),
     374              :                        "expected item after outer attribute or doc comment");
     375            0 :           add_error (std::move (error));
     376            0 :         }
     377            7 :       return Parse::Error::Item::make_end_of_file ();
     378              : 
     379       126301 :     case ASYNC:
     380              :     case PUB:
     381              :     case MOD:
     382              :     case EXTERN_KW:
     383              :     case USE:
     384              :     case FN_KW:
     385              :     case TYPE:
     386              :     case STRUCT_KW:
     387              :     case ENUM_KW:
     388              :     case CONST:
     389              :     case STATIC_KW:
     390              :     case AUTO:
     391              :     case TRAIT:
     392              :     case IMPL:
     393              :     case MACRO:
     394              :     /* TODO: implement union keyword but not really because of
     395              :      * context-dependence crappy hack way to parse a union written below to
     396              :      * separate it from the good code. */
     397              :     // case UNION:
     398              :     case UNSAFE: // maybe - unsafe traits are a thing
     399              :       // if any of these (should be all possible VisItem prefixes), parse a
     400              :       // VisItem
     401              :       {
     402       126301 :         auto errors_before = error_table.size ();
     403       126301 :         auto vis_item = parse_vis_item (std::move (outer_attrs));
     404       126301 :         if (!vis_item)
     405              :           {
     406              :             // e.g. `pub` followed by something that isn't an item
     407           63 :             if (error_table.size () == errors_before)
     408              :               {
     409            1 :                 const_TokenPtr t = lexer.peek_token ();
     410            1 :                 add_error (Error (t->get_locus (), "expected item, found %qs",
     411              :                                   t->get_token_description ()));
     412            1 :               }
     413           63 :             return Parse::Error::Item::make_malformed ();
     414              :           }
     415       126238 :         return RType{std::move (vis_item)};
     416       126301 :       }
     417            4 :     case SUPER:
     418              :     case SELF:
     419              :     case CRATE:
     420              :     case DOLLAR_SIGN:
     421              :       // almost certainly macro invocation semi
     422              :       {
     423            4 :         auto macro_invoc_semi
     424            4 :           = parse_macro_invocation_semi (std::move (outer_attrs));
     425            4 :         if (!macro_invoc_semi)
     426            1 :           return Parse::Error::Item::make_malformed ();
     427            3 :         return RType{std::move (macro_invoc_semi)};
     428            4 :       }
     429              :     // crappy hack to do union "keyword"
     430        31120 :     case IDENTIFIER:
     431              :       // TODO: ensure std::string and literal comparison works
     432        31120 :       if (t->get_str () == Values::WeakKeywords::UNION
     433        31188 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
     434              :         {
     435           68 :           auto vis_item = parse_vis_item (std::move (outer_attrs));
     436           68 :           if (!vis_item)
     437            0 :             return Parse::Error::Item::make_malformed ();
     438           68 :           return RType{std::move (vis_item)};
     439              :           // or should this go straight to parsing union?
     440           68 :         }
     441        31052 :       else if (t->get_str () == Values::WeakKeywords::DEFAULT
     442        31054 :                && lexer.peek_token (1)->get_id () != EXCLAM)
     443              :         {
     444              :           // parse normal functions with `default` qualifier
     445              :           // they will be rejected in ASTValidation pass
     446            1 :           return parse_vis_item (std::move (outer_attrs));
     447              :         }
     448        62102 :       else if (is_macro_rules_def (t))
     449              :         {
     450              :           // macro_rules! macro item
     451         2707 :           auto macro_rule_def = parse_macro_rules_def (std::move (outer_attrs));
     452         2707 :           if (!macro_rule_def)
     453           13 :             return Parse::Error::Item::make_malformed ();
     454         2694 :           return RType{std::move (macro_rule_def)};
     455         2707 :         }
     456        56688 :       else if (lexer.peek_token (1)->get_id () == SCOPE_RESOLUTION
     457        56687 :                || lexer.peek_token (1)->get_id () == EXCLAM)
     458              :         {
     459              :           /* path (probably) or macro invocation, so probably a macro invocation
     460              :            * semi */
     461        28341 :           auto macro_invocation_semi
     462        28341 :             = parse_macro_invocation_semi (std::move (outer_attrs));
     463        28341 :           if (!macro_invocation_semi)
     464            2 :             return Parse::Error::Item::make_malformed ();
     465        28339 :           return RType{std::move (macro_invocation_semi)};
     466        28341 :         }
     467              :       gcc_fallthrough ();
     468              :     default:
     469              :       // otherwise unrecognised
     470            6 :       add_error (Error (t->get_locus (),
     471              :                         "unrecognised token %qs for start of %s",
     472              :                         t->get_token_description (),
     473              :                         called_from_statement ? "statement" : "item"));
     474              : 
     475              :       // skip somewhere?
     476            3 :       return Parse::Error::Item::make_malformed ();
     477              :       break;
     478              :     }
     479       157432 : }
     480              : 
     481              : // Parses a VisItem (item that can have non-default visibility).
     482              : template <typename ManagedTokenSource>
     483              : std::unique_ptr<AST::VisItem>
     484       131039 : Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
     485              : {
     486              :   // parse visibility, which may or may not exist
     487       131039 :   auto vis_res = parse_visibility ();
     488       131039 :   if (!vis_res)
     489            0 :     return nullptr;
     490       131039 :   auto vis = vis_res.value ();
     491              : 
     492              :   // select VisItem to create depending on keyword
     493       131039 :   const_TokenPtr t = lexer.peek_token ();
     494              : 
     495       131039 :   switch (t->get_id ())
     496              :     {
     497         4079 :     case MOD:
     498         4079 :       return parse_module (std::move (vis), std::move (outer_attrs));
     499         2295 :     case EXTERN_KW:
     500              :       // lookahead to resolve syntactical production
     501         2295 :       t = lexer.peek_token (1);
     502              : 
     503         2295 :       switch (t->get_id ())
     504              :         {
     505           70 :         case CRATE:
     506           70 :           return parse_extern_crate (std::move (vis), std::move (outer_attrs));
     507            0 :         case FN_KW: // extern function
     508            0 :           return parse_function (std::move (vis), std::move (outer_attrs));
     509            1 :         case LEFT_CURLY: // extern block
     510            1 :           return parse_extern_block (std::move (vis), std::move (outer_attrs));
     511         2224 :         case STRING_LITERAL: // for specifying extern ABI
     512              :           // could be extern block or extern function, so more lookahead
     513         2224 :           t = lexer.peek_token (2);
     514              : 
     515         2224 :           switch (t->get_id ())
     516              :             {
     517            2 :             case FN_KW:
     518            2 :               return parse_function (std::move (vis), std::move (outer_attrs));
     519         2222 :             case LEFT_CURLY:
     520         2222 :               return parse_extern_block (std::move (vis),
     521         2222 :                                          std::move (outer_attrs));
     522            0 :             default:
     523            0 :               add_error (
     524            0 :                 Error (t->get_locus (),
     525              :                        "unexpected token %qs in some sort of extern production",
     526              :                        t->get_token_description ()));
     527              : 
     528            0 :               lexer.skip_token (2); // TODO: is this right thing to do?
     529            0 :               return nullptr;
     530              :             }
     531            0 :         default:
     532            0 :           add_error (
     533            0 :             Error (t->get_locus (),
     534              :                    "unexpected token %qs in some sort of extern production",
     535              :                    t->get_token_description ()));
     536              : 
     537            0 :           lexer.skip_token (1); // TODO: is this right thing to do?
     538            0 :           return nullptr;
     539              :         }
     540         8021 :     case USE:
     541         8021 :       return parse_use_decl (std::move (vis), std::move (outer_attrs));
     542         9760 :     case FN_KW:
     543         9760 :       return parse_function (std::move (vis), std::move (outer_attrs));
     544          190 :     case TYPE:
     545          190 :       return parse_type_alias (std::move (vis), std::move (outer_attrs));
     546         8469 :     case STRUCT_KW:
     547         8469 :       return parse_struct (std::move (vis), std::move (outer_attrs));
     548          926 :     case ENUM_KW:
     549          926 :       return parse_enum (std::move (vis), std::move (outer_attrs));
     550              :     // TODO: implement union keyword but not really because of
     551              :     // context-dependence case UNION: crappy hack to do union "keyword"
     552          132 :     case IDENTIFIER:
     553          132 :       if (t->get_str () == Values::WeakKeywords::UNION
     554          263 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
     555              :         {
     556          131 :           return parse_union (std::move (vis), std::move (outer_attrs));
     557              :           // or should item switch go straight to parsing union?
     558              :         }
     559            1 :       else if (t->get_str () == Values::WeakKeywords::DEFAULT)
     560              :         {
     561              :           // parse normal functions with `default` qualifier they will be
     562              :           // rejected in ASTValidation pass
     563            1 :           return parse_function (std::move (vis), std::move (outer_attrs));
     564              :         }
     565              :       break;
     566         5240 :     case CONST:
     567              :       // lookahead to resolve syntactical production
     568         5240 :       t = lexer.peek_token (1);
     569              : 
     570         5240 :       switch (t->get_id ())
     571              :         {
     572         4951 :         case IDENTIFIER:
     573              :         case UNDERSCORE:
     574         4951 :           return parse_const_item (std::move (vis), std::move (outer_attrs));
     575            1 :         case ASYNC:
     576            1 :           return parse_async_item (std::move (vis), std::move (outer_attrs));
     577          287 :         case UNSAFE:
     578              :         case EXTERN_KW:
     579              :         case FN_KW:
     580          287 :           return parse_function (std::move (vis), std::move (outer_attrs));
     581            1 :         default:
     582            1 :           add_error (
     583            1 :             Error (t->get_locus (),
     584              :                    "unexpected token %qs in some sort of const production",
     585              :                    t->get_token_description ()));
     586              : 
     587            1 :           lexer.skip_token (1); // TODO: is this right thing to do?
     588            1 :           return nullptr;
     589              :         }
     590              :     // for async functions
     591            6 :     case ASYNC:
     592            6 :       return parse_async_item (std::move (vis), std::move (outer_attrs));
     593              : 
     594          381 :     case STATIC_KW:
     595          381 :       return parse_static_item (std::move (vis), std::move (outer_attrs));
     596         5604 :     case AUTO:
     597              :     case TRAIT:
     598         5604 :       return parse_trait (std::move (vis), std::move (outer_attrs));
     599        56323 :     case IMPL:
     600        56323 :       return parse_impl (std::move (vis), std::move (outer_attrs));
     601        29405 :     case UNSAFE: // unsafe traits, unsafe functions, unsafe impls (trait impls),
     602              :       // lookahead to resolve syntactical production
     603        29405 :       t = lexer.peek_token (1);
     604              : 
     605        29405 :       switch (t->get_id ())
     606              :         {
     607          201 :         case AUTO:
     608              :         case TRAIT:
     609          201 :           return parse_trait (std::move (vis), std::move (outer_attrs));
     610        27841 :         case EXTERN_KW:
     611              :         case FN_KW:
     612        27841 :           return parse_function (std::move (vis), std::move (outer_attrs));
     613         1361 :         case IMPL:
     614         1361 :           return parse_impl (std::move (vis), std::move (outer_attrs));
     615            1 :         case MOD:
     616            1 :           return parse_module (std::move (vis), std::move (outer_attrs));
     617            1 :         default:
     618            1 :           add_error (
     619            1 :             Error (t->get_locus (),
     620              :                    "unexpected token %qs in some sort of unsafe production",
     621              :                    t->get_token_description ()));
     622              : 
     623            1 :           lexer.skip_token (1); // TODO: is this right thing to do?
     624            1 :           return nullptr;
     625              :         }
     626          206 :     case MACRO:
     627          206 :       return parse_decl_macro_def (std::move (vis), std::move (outer_attrs));
     628              :     default:
     629              :       // otherwise vis item clearly doesn't exist, which is not an error
     630              :       // has a catch-all post-switch return to allow other breaks to occur
     631              :       break;
     632              :     }
     633            2 :   return nullptr;
     634       131039 : }
     635              : 
     636              : template <typename ManagedTokenSource>
     637              : std::unique_ptr<AST::Function>
     638            8 : Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
     639              :                                               AST::AttrVec outer_attrs)
     640              : {
     641            8 :   auto offset = (lexer.peek_token ()->get_id () == CONST) ? 1 : 0;
     642            8 :   const_TokenPtr t = lexer.peek_token (offset);
     643              : 
     644            8 :   if (get_rust_edition () == Edition::E2015)
     645              :     {
     646            1 :       add_error (Error (t->get_locus (), ErrorCode::E0670,
     647              :                         "%<async fn%> is not permitted in Rust 2015"));
     648            1 :       add_error (
     649            2 :         Error::Hint (t->get_locus (),
     650              :                      "to use %<async fn%>, switch to Rust 2018 or later"));
     651              :     }
     652              : 
     653            8 :   t = lexer.peek_token (offset + 1);
     654              : 
     655            8 :   switch (t->get_id ())
     656              :     {
     657            8 :     case UNSAFE:
     658              :     case FN_KW:
     659            8 :       return parse_function (std::move (vis), std::move (outer_attrs));
     660              : 
     661            0 :     default:
     662            0 :       add_error (
     663            0 :         Error (t->get_locus (), "expected item, found keyword %<async%>"));
     664              : 
     665            0 :       lexer.skip_token (1);
     666            0 :       return nullptr;
     667              :     }
     668            8 : }
     669              : 
     670              : // Parses a macro rules definition syntax extension whatever thing.
     671              : template <typename ManagedTokenSource>
     672              : std::unique_ptr<AST::MacroRulesDefinition>
     673         6789 : Parser<ManagedTokenSource>::parse_macro_rules_def (AST::AttrVec outer_attrs)
     674              : {
     675              :   // ensure that first token is identifier saying "macro_rules"
     676         6789 :   const_TokenPtr t = lexer.peek_token ();
     677         6789 :   if (t->get_id () != IDENTIFIER
     678         6789 :       || t->get_str () != Values::WeakKeywords::MACRO_RULES)
     679              :     {
     680            0 :       Error error (
     681              :         t->get_locus (),
     682              :         "macro rules definition does not start with %<macro_rules%>");
     683            0 :       add_error (std::move (error));
     684              : 
     685              :       // skip after somewhere?
     686            0 :       return nullptr;
     687            0 :     }
     688         6789 :   lexer.skip_token ();
     689         6789 :   location_t macro_locus = t->get_locus ();
     690              : 
     691         6789 :   if (!skip_token (EXCLAM))
     692              :     {
     693              :       // skip after somewhere?
     694            0 :       return nullptr;
     695              :     }
     696              : 
     697              :   // parse macro name
     698         6789 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
     699         6789 :   if (ident_tok == nullptr)
     700              :     {
     701            1 :       return nullptr;
     702              :     }
     703        13576 :   Identifier rule_name{ident_tok};
     704              : 
     705              :   // DEBUG
     706         6788 :   rust_debug ("in macro rules def, about to parse parens.");
     707              : 
     708              :   // save delim type to ensure it is reused later
     709         6788 :   AST::DelimType delim_type = AST::PARENS;
     710              : 
     711              :   // Map tokens to DelimType
     712         6788 :   t = lexer.peek_token ();
     713         6788 :   switch (t->get_id ())
     714              :     {
     715              :     case LEFT_PAREN:
     716              :       delim_type = AST::PARENS;
     717              :       break;
     718            0 :     case LEFT_SQUARE:
     719            0 :       delim_type = AST::SQUARE;
     720            0 :       break;
     721         6787 :     case LEFT_CURLY:
     722         6787 :       delim_type = AST::CURLY;
     723         6787 :       break;
     724            0 :     default:
     725            0 :       add_error (Error (t->get_locus (),
     726              :                         "unexpected token %qs - expecting delimiters (for a "
     727              :                         "macro rules definition)",
     728              :                         t->get_token_description ()));
     729              : 
     730            0 :       return nullptr;
     731              :     }
     732         6788 :   lexer.skip_token ();
     733              : 
     734              :   // parse actual macro rules
     735         6788 :   std::vector<AST::MacroRule> macro_rules;
     736              : 
     737              :   // must be at least one macro rule, so parse it
     738         6788 :   AST::MacroRule initial_rule = parse_macro_rule ();
     739         6788 :   if (initial_rule.is_error ())
     740              :     {
     741           12 :       Error error (lexer.peek_token ()->get_locus (),
     742              :                    "required first macro rule in macro rules definition "
     743              :                    "could not be parsed");
     744           12 :       add_error (std::move (error));
     745              : 
     746              :       // skip after somewhere?
     747           12 :       return nullptr;
     748           12 :     }
     749         6776 :   macro_rules.push_back (std::move (initial_rule));
     750              : 
     751              :   // DEBUG
     752         6776 :   rust_debug ("successfully pushed back initial macro rule");
     753              : 
     754         6776 :   t = lexer.peek_token ();
     755              :   // parse macro rules
     756         7221 :   while (t->get_id () == SEMICOLON)
     757              :     {
     758              :       // skip semicolon
     759         5866 :       lexer.skip_token ();
     760              : 
     761              :       // don't parse if end of macro rules
     762        11732 :       if (Parse::Utils::token_id_matches_delims (lexer.peek_token ()->get_id (),
     763              :                                                  delim_type))
     764              :         {
     765              :           // DEBUG
     766         5421 :           rust_debug (
     767              :             "broke out of parsing macro rules loop due to finding delim");
     768              : 
     769         5421 :           break;
     770              :         }
     771              : 
     772              :       // try to parse next rule
     773          445 :       AST::MacroRule rule = parse_macro_rule ();
     774          445 :       if (rule.is_error ())
     775              :         {
     776            0 :           Error error (lexer.peek_token ()->get_locus (),
     777              :                        "failed to parse macro rule in macro rules definition");
     778            0 :           add_error (std::move (error));
     779              : 
     780            0 :           return nullptr;
     781            0 :         }
     782              : 
     783          445 :       macro_rules.push_back (std::move (rule));
     784              : 
     785              :       // DEBUG
     786          445 :       rust_debug ("successfully pushed back another macro rule");
     787              : 
     788          445 :       t = lexer.peek_token ();
     789              :     }
     790              : 
     791              :   // parse end delimiters
     792         6776 :   t = lexer.peek_token ();
     793         6776 :   if (Parse::Utils::token_id_matches_delims (t->get_id (), delim_type))
     794              :     {
     795              :       // tokens match opening delimiter, so skip.
     796         6776 :       lexer.skip_token ();
     797              : 
     798         6776 :       if (delim_type != AST::CURLY)
     799              :         {
     800              :           // skip semicolon at end of non-curly macro definitions
     801            1 :           if (!skip_token (SEMICOLON))
     802              :             {
     803              :               // as this is the end, allow recovery (probably) - may change
     804              :               return std::unique_ptr<AST::MacroRulesDefinition> (
     805            0 :                 AST::MacroRulesDefinition::mbe (
     806              :                   std::move (rule_name), delim_type, std::move (macro_rules),
     807            0 :                   std::move (outer_attrs), macro_locus));
     808              :             }
     809              :         }
     810              : 
     811              :       return std::unique_ptr<AST::MacroRulesDefinition> (
     812        13552 :         AST::MacroRulesDefinition::mbe (std::move (rule_name), delim_type,
     813              :                                         std::move (macro_rules),
     814         6776 :                                         std::move (outer_attrs), macro_locus));
     815              :     }
     816              :   else
     817              :     {
     818              :       // tokens don't match opening delimiters, so produce error
     819            0 :       Error error (t->get_locus (),
     820              :                    "unexpected token %qs - expecting closing delimiter %qs "
     821              :                    "(for a macro rules definition)",
     822              :                    t->get_token_description (),
     823              :                    (delim_type == AST::PARENS
     824              :                       ? ")"
     825              :                       : (delim_type == AST::SQUARE ? "]" : "}")));
     826            0 :       add_error (std::move (error));
     827              : 
     828              :       /* return empty macro definiton despite possibly parsing mostly valid one
     829              :        * - TODO is this a good idea? */
     830            0 :       return nullptr;
     831            0 :     }
     832        20365 : }
     833              : 
     834              : // Parses a declarative macro 2.0 definition.
     835              : template <typename ManagedTokenSource>
     836              : std::unique_ptr<AST::MacroRulesDefinition>
     837          206 : Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis,
     838              :                                                   AST::AttrVec outer_attrs)
     839              : {
     840              :   // ensure that first token is identifier saying "macro"
     841          206 :   const_TokenPtr t = lexer.peek_token ();
     842          206 :   if (t->get_id () != MACRO)
     843              :     {
     844            0 :       Error error (
     845              :         t->get_locus (),
     846              :         "declarative macro definition does not start with %<macro%>");
     847            0 :       add_error (std::move (error));
     848              : 
     849              :       // skip after somewhere?
     850            0 :       return nullptr;
     851            0 :     }
     852          206 :   lexer.skip_token ();
     853          206 :   location_t macro_locus = t->get_locus ();
     854              : 
     855              :   // parse macro name
     856          206 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
     857          206 :   if (ident_tok == nullptr)
     858              :     {
     859            0 :       return nullptr;
     860              :     }
     861          412 :   Identifier rule_name{ident_tok};
     862              : 
     863          206 :   t = lexer.peek_token ();
     864          206 :   if (t->get_id () == LEFT_PAREN)
     865              :     {
     866              :       // single definiton of macro rule
     867              :       // e.g. `macro foo($e:expr) {}`
     868              : 
     869              :       // parse macro matcher
     870          181 :       location_t locus = lexer.peek_token ()->get_locus ();
     871          181 :       AST::MacroMatcher matcher = parse_macro_matcher ();
     872          181 :       if (matcher.is_error ())
     873            0 :         return nullptr;
     874              : 
     875              :       // check delimiter of macro matcher
     876          181 :       if (matcher.get_delim_type () != AST::DelimType::PARENS)
     877              :         {
     878            0 :           Error error (locus, "only parenthesis can be used for a macro "
     879              :                               "matcher in declarative macro definition");
     880            0 :           add_error (std::move (error));
     881            0 :           return nullptr;
     882            0 :         }
     883              : 
     884          181 :       location_t transcriber_loc = lexer.peek_token ()->get_locus ();
     885          181 :       auto delim_tok_tree = parse_delim_token_tree ();
     886          181 :       if (!delim_tok_tree)
     887            0 :         return nullptr;
     888              : 
     889          181 :       AST::MacroTranscriber transcriber (delim_tok_tree.value (),
     890              :                                          transcriber_loc);
     891              : 
     892          181 :       if (transcriber.get_token_tree ().get_delim_type ()
     893              :           != AST::DelimType::CURLY)
     894              :         {
     895            1 :           Error error (transcriber_loc,
     896              :                        "only braces can be used for a macro transcriber "
     897              :                        "in declarative macro definition");
     898            1 :           add_error (std::move (error));
     899            1 :           return nullptr;
     900            1 :         }
     901              : 
     902          180 :       std::vector<AST::MacroRule> macro_rules;
     903          180 :       macro_rules.emplace_back (std::move (matcher), std::move (transcriber),
     904              :                                 locus);
     905              : 
     906              :       return std::unique_ptr<AST::MacroRulesDefinition> (
     907          360 :         AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
     908              :                                                macro_rules,
     909              :                                                std::move (outer_attrs),
     910          180 :                                                macro_locus, vis));
     911          542 :     }
     912           25 :   else if (t->get_id () == LEFT_CURLY)
     913              :     {
     914              :       // multiple definitions of macro rule separated by comma
     915              :       // e.g. `macro foo { () => {}, ($e:expr) => {}, }`
     916              : 
     917              :       // parse left curly
     918           25 :       const_TokenPtr left_curly = expect_token (LEFT_CURLY);
     919           25 :       if (left_curly == nullptr)
     920              :         {
     921            0 :           return nullptr;
     922              :         }
     923              : 
     924              :       // parse actual macro rules
     925           25 :       std::vector<AST::MacroRule> macro_rules;
     926              : 
     927              :       // must be at least one macro rule, so parse it
     928           25 :       AST::MacroRule initial_rule = parse_macro_rule ();
     929           25 :       if (initial_rule.is_error ())
     930              :         {
     931            1 :           Error error (
     932            1 :             lexer.peek_token ()->get_locus (),
     933              :             "required first macro rule in declarative macro definition "
     934              :             "could not be parsed");
     935            1 :           add_error (std::move (error));
     936              : 
     937              :           // skip after somewhere?
     938            1 :           return nullptr;
     939            1 :         }
     940           24 :       macro_rules.push_back (std::move (initial_rule));
     941              : 
     942           24 :       t = lexer.peek_token ();
     943              :       // parse macro rules
     944           40 :       while (t->get_id () == COMMA)
     945              :         {
     946              :           // skip comma
     947           32 :           lexer.skip_token ();
     948              : 
     949              :           // don't parse if end of macro rules
     950           32 :           if (Parse::Utils::token_id_matches_delims (
     951           64 :                 lexer.peek_token ()->get_id (), AST::CURLY))
     952              :             {
     953              :               break;
     954              :             }
     955              : 
     956              :           // try to parse next rule
     957           16 :           AST::MacroRule rule = parse_macro_rule ();
     958           16 :           if (rule.is_error ())
     959              :             {
     960            0 :               Error error (
     961            0 :                 lexer.peek_token ()->get_locus (),
     962              :                 "failed to parse macro rule in declarative macro definition");
     963            0 :               add_error (std::move (error));
     964              : 
     965            0 :               return nullptr;
     966            0 :             }
     967              : 
     968           16 :           macro_rules.push_back (std::move (rule));
     969              : 
     970           16 :           t = lexer.peek_token ();
     971              :         }
     972              : 
     973              :       // parse right curly
     974           24 :       const_TokenPtr right_curly = expect_token (RIGHT_CURLY);
     975           24 :       if (right_curly == nullptr)
     976              :         {
     977            0 :           return nullptr;
     978              :         }
     979              : 
     980              :       return std::unique_ptr<AST::MacroRulesDefinition> (
     981           48 :         AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
     982              :                                                std::move (macro_rules),
     983              :                                                std::move (outer_attrs),
     984           24 :                                                macro_locus, vis));
     985           50 :     }
     986              :   else
     987              :     {
     988            0 :       add_error (Error (t->get_locus (),
     989              :                         "unexpected token %qs - expecting delimiters "
     990              :                         "(for a declarative macro definiton)",
     991              :                         t->get_token_description ()));
     992            0 :       return nullptr;
     993              :     }
     994          412 : }
     995              : 
     996              : /* Parses a visibility syntactical production (i.e. creating a non-default
     997              :  * visibility) */
     998              : template <typename ManagedTokenSource>
     999              : tl::expected<AST::Visibility, Parse::Error::Visibility>
    1000       263111 : Parser<ManagedTokenSource>::parse_visibility ()
    1001              : {
    1002              :   // check for no visibility
    1003       526222 :   if (lexer.peek_token ()->get_id () != PUB)
    1004              :     {
    1005       203526 :       return AST::Visibility::create_private ();
    1006              :     }
    1007              : 
    1008        59585 :   auto vis_loc = lexer.peek_token ()->get_locus ();
    1009        59585 :   lexer.skip_token ();
    1010              : 
    1011              :   // create simple pub visibility if
    1012              :   // - found no parentheses
    1013              :   // - found unit type `()`
    1014       119170 :   if (lexer.peek_token ()->get_id () != LEFT_PAREN
    1015        62199 :       || lexer.peek_token (1)->get_id () == RIGHT_PAREN)
    1016              :     {
    1017        56972 :       return AST::Visibility::create_public (vis_loc);
    1018              :       // or whatever
    1019              :     }
    1020              : 
    1021         2613 :   lexer.skip_token ();
    1022              : 
    1023         2613 :   const_TokenPtr t = lexer.peek_token ();
    1024         2613 :   auto path_loc = t->get_locus ();
    1025              : 
    1026         2613 :   switch (t->get_id ())
    1027              :     {
    1028         1771 :     case CRATE:
    1029         1771 :       lexer.skip_token ();
    1030              : 
    1031         1771 :       skip_token (RIGHT_PAREN);
    1032              : 
    1033         1771 :       return AST::Visibility::create_crate (path_loc, vis_loc);
    1034            0 :     case SELF:
    1035            0 :       lexer.skip_token ();
    1036              : 
    1037            0 :       skip_token (RIGHT_PAREN);
    1038              : 
    1039            0 :       return AST::Visibility::create_self (path_loc, vis_loc);
    1040          780 :     case SUPER:
    1041          780 :       lexer.skip_token ();
    1042              : 
    1043          780 :       skip_token (RIGHT_PAREN);
    1044              : 
    1045          780 :       return AST::Visibility::create_super (path_loc, vis_loc);
    1046           62 :     case IN:
    1047              :       {
    1048           62 :         lexer.skip_token ();
    1049              : 
    1050              :         // parse the "in" path as well
    1051           62 :         auto path = parse_simple_path ();
    1052           62 :         if (!path)
    1053              :           {
    1054            0 :             Error error (lexer.peek_token ()->get_locus (),
    1055              :                          "missing path in pub(in path) visibility");
    1056            0 :             add_error (std::move (error));
    1057              : 
    1058              :             // skip after somewhere?
    1059            0 :             return Parse::Error::Visibility::make_missing_path ();
    1060            0 :           }
    1061              : 
    1062           62 :         skip_token (RIGHT_PAREN);
    1063              : 
    1064          124 :         return AST::Visibility::create_in_path (std::move (path.value ()),
    1065           62 :                                                 vis_loc);
    1066           62 :       }
    1067            0 :     default:
    1068            0 :       add_error (Error (t->get_locus (), "unexpected token %qs in visibility",
    1069              :                         t->get_token_description ()));
    1070              : 
    1071            0 :       lexer.skip_token ();
    1072            0 :       return Parse::Error::Visibility::make_malformed ();
    1073              :     }
    1074         2613 : }
    1075              : 
    1076              : // Parses a module - either a bodied module or a module defined in another file.
    1077              : template <typename ManagedTokenSource>
    1078              : std::unique_ptr<AST::Module>
    1079         4080 : Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
    1080              :                                           AST::AttrVec outer_attrs)
    1081              : {
    1082         4080 :   location_t locus = lexer.peek_token ()->get_locus ();
    1083              : 
    1084         4080 :   Unsafety safety = Unsafety::Normal;
    1085         8160 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    1086              :     {
    1087            1 :       safety = Unsafety::Unsafe;
    1088            1 :       skip_token (UNSAFE);
    1089              :     }
    1090              : 
    1091         4080 :   skip_token (MOD);
    1092              : 
    1093         4080 :   const_TokenPtr module_name = expect_token (IDENTIFIER);
    1094         4080 :   if (module_name == nullptr)
    1095              :     {
    1096            0 :       return nullptr;
    1097              :     }
    1098         8160 :   Identifier name{module_name};
    1099              : 
    1100         4080 :   const_TokenPtr t = lexer.peek_token ();
    1101              : 
    1102         4080 :   switch (t->get_id ())
    1103              :     {
    1104         1805 :     case SEMICOLON:
    1105         1805 :       lexer.skip_token ();
    1106              : 
    1107              :       // Construct an external module
    1108              :       return std::unique_ptr<AST::Module> (
    1109         5415 :         new AST::Module (std::move (name), std::move (vis),
    1110              :                          std::move (outer_attrs), locus, safety,
    1111         5415 :                          lexer.get_filename (), inline_module_stack));
    1112         2275 :     case LEFT_CURLY:
    1113              :       {
    1114         2275 :         lexer.skip_token ();
    1115              : 
    1116              :         // parse inner attributes
    1117         2275 :         AST::AttrVec inner_attrs = parse_inner_attributes ();
    1118              : 
    1119         2275 :         std::string default_path = name.as_string ();
    1120              : 
    1121         2275 :         if (inline_module_stack.empty ())
    1122              :           {
    1123         1761 :             std::string filename = lexer.get_filename ();
    1124         1761 :             auto slash_idx = filename.rfind (file_separator);
    1125         1761 :             if (slash_idx == std::string::npos)
    1126              :               slash_idx = 0;
    1127              :             else
    1128         1754 :               slash_idx++;
    1129         1761 :             filename = filename.substr (slash_idx);
    1130              : 
    1131         1761 :             std::string subdir;
    1132         1761 :             if (get_file_subdir (filename, subdir))
    1133         1678 :               default_path = subdir + file_separator + name.as_string ();
    1134         1761 :           }
    1135              : 
    1136         2275 :         std::string module_path_name
    1137              :           = extract_module_path (inner_attrs, outer_attrs, default_path);
    1138         2275 :         InlineModuleStackScope scope (*this, std::move (module_path_name));
    1139              : 
    1140              :         // parse items
    1141         2275 :         std::vector<std::unique_ptr<AST::Item>> items;
    1142         2275 :         const_TokenPtr tok = lexer.peek_token ();
    1143        22910 :         while (tok->get_id () != RIGHT_CURLY)
    1144              :           {
    1145        20635 :             auto item = parse_item (false);
    1146        20635 :             if (!item)
    1147              :               {
    1148            1 :                 Error error (tok->get_locus (),
    1149              :                              "failed to parse item in module");
    1150            1 :                 add_error (std::move (error));
    1151              : 
    1152            1 :                 return nullptr;
    1153            1 :               }
    1154              : 
    1155        20634 :             items.push_back (std::move (item.value ()));
    1156              : 
    1157        20634 :             tok = lexer.peek_token ();
    1158              :           }
    1159              : 
    1160         2274 :         if (!skip_token (RIGHT_CURLY))
    1161              :           {
    1162              :             // skip somewhere?
    1163            0 :             return nullptr;
    1164              :           }
    1165              : 
    1166              :         return std::unique_ptr<AST::Module> (
    1167         2274 :           new AST::Module (std::move (name), locus, std::move (items),
    1168              :                            std::move (vis), safety, std::move (inner_attrs),
    1169         2274 :                            std::move (outer_attrs))); // module name?
    1170         2275 :       }
    1171            0 :     default:
    1172            0 :       add_error (
    1173            0 :         Error (t->get_locus (),
    1174              :                "unexpected token %qs in module declaration/definition item",
    1175              :                t->get_token_description ()));
    1176              : 
    1177            0 :       lexer.skip_token ();
    1178            0 :       return nullptr;
    1179              :     }
    1180         4080 : }
    1181              : 
    1182              : // Parses an extern crate declaration (dependency on external crate)
    1183              : template <typename ManagedTokenSource>
    1184              : std::unique_ptr<AST::ExternCrate>
    1185           70 : Parser<ManagedTokenSource>::parse_extern_crate (AST::Visibility vis,
    1186              :                                                 AST::AttrVec outer_attrs)
    1187              : {
    1188           70 :   location_t locus = lexer.peek_token ()->get_locus ();
    1189           70 :   if (!skip_token (EXTERN_KW))
    1190              :     {
    1191            0 :       skip_after_semicolon ();
    1192            0 :       return nullptr;
    1193              :     }
    1194              : 
    1195           70 :   if (!skip_token (CRATE))
    1196              :     {
    1197            0 :       skip_after_semicolon ();
    1198            0 :       return nullptr;
    1199              :     }
    1200              : 
    1201              :   /* parse crate reference name - this has its own syntactical rule in reference
    1202              :    * but seems to not be used elsewhere, so i'm putting it here */
    1203           70 :   const_TokenPtr crate_name_tok = lexer.peek_token ();
    1204           70 :   std::string crate_name;
    1205              : 
    1206           70 :   switch (crate_name_tok->get_id ())
    1207              :     {
    1208           69 :     case IDENTIFIER:
    1209           69 :       crate_name = crate_name_tok->get_str ();
    1210           69 :       lexer.skip_token ();
    1211           69 :       break;
    1212            1 :     case SELF:
    1213            1 :       crate_name = Values::Keywords::SELF;
    1214            1 :       lexer.skip_token ();
    1215            1 :       break;
    1216            0 :     default:
    1217            0 :       add_error (
    1218            0 :         Error (crate_name_tok->get_locus (),
    1219              :                "expecting crate name (identifier or %<self%>), found %qs",
    1220              :                crate_name_tok->get_token_description ()));
    1221              : 
    1222            0 :       skip_after_semicolon ();
    1223            0 :       return nullptr;
    1224              :     }
    1225              : 
    1226              :   // don't parse as clause if it doesn't exist
    1227          140 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    1228              :     {
    1229           61 :       lexer.skip_token ();
    1230              : 
    1231              :       return std::unique_ptr<AST::ExternCrate> (
    1232           61 :         new AST::ExternCrate (std::move (crate_name), std::move (vis),
    1233           61 :                               std::move (outer_attrs), locus));
    1234              :     }
    1235              : 
    1236              :   /* parse as clause - this also has its own syntactical rule in reference and
    1237              :    * also seems to not be used elsewhere, so including here again. */
    1238            9 :   if (!skip_token (AS))
    1239              :     {
    1240            0 :       skip_after_semicolon ();
    1241            0 :       return nullptr;
    1242              :     }
    1243              : 
    1244            9 :   const_TokenPtr as_name_tok = lexer.peek_token ();
    1245            9 :   std::string as_name;
    1246              : 
    1247            9 :   switch (as_name_tok->get_id ())
    1248              :     {
    1249            9 :     case IDENTIFIER:
    1250            9 :       as_name = as_name_tok->get_str ();
    1251            9 :       lexer.skip_token ();
    1252            9 :       break;
    1253            0 :     case UNDERSCORE:
    1254            0 :       as_name = Values::Keywords::UNDERSCORE;
    1255            0 :       lexer.skip_token ();
    1256            0 :       break;
    1257            0 :     default:
    1258            0 :       add_error (
    1259            0 :         Error (as_name_tok->get_locus (),
    1260              :                "expecting as clause name (identifier or %<_%>), found %qs",
    1261              :                as_name_tok->get_token_description ()));
    1262              : 
    1263            0 :       skip_after_semicolon ();
    1264            0 :       return nullptr;
    1265              :     }
    1266              : 
    1267            9 :   if (!skip_token (SEMICOLON))
    1268              :     {
    1269            0 :       skip_after_semicolon ();
    1270            0 :       return nullptr;
    1271              :     }
    1272              : 
    1273              :   return std::unique_ptr<AST::ExternCrate> (
    1274            9 :     new AST::ExternCrate (std::move (crate_name), std::move (vis),
    1275            9 :                           std::move (outer_attrs), locus, std::move (as_name)));
    1276          158 : }
    1277              : 
    1278              : // Parses a use declaration.
    1279              : template <typename ManagedTokenSource>
    1280              : std::unique_ptr<AST::UseDeclaration>
    1281         8021 : Parser<ManagedTokenSource>::parse_use_decl (AST::Visibility vis,
    1282              :                                             AST::AttrVec outer_attrs)
    1283              : {
    1284         8021 :   location_t locus = lexer.peek_token ()->get_locus ();
    1285         8021 :   if (!skip_token (USE))
    1286              :     {
    1287            0 :       skip_after_semicolon ();
    1288            0 :       return nullptr;
    1289              :     }
    1290              : 
    1291              :   // parse use tree, which is required
    1292         8021 :   std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    1293         8021 :   if (use_tree == nullptr)
    1294              :     {
    1295            1 :       Error error (lexer.peek_token ()->get_locus (),
    1296              :                    "could not parse use tree in use declaration");
    1297            1 :       add_error (std::move (error));
    1298              : 
    1299            1 :       skip_after_semicolon ();
    1300            1 :       return nullptr;
    1301            1 :     }
    1302              : 
    1303         8020 :   if (!skip_token (SEMICOLON))
    1304              :     {
    1305            0 :       skip_after_semicolon ();
    1306            0 :       return nullptr;
    1307              :     }
    1308              : 
    1309              :   return std::unique_ptr<AST::UseDeclaration> (
    1310         8020 :     new AST::UseDeclaration (std::move (use_tree), std::move (vis),
    1311         8020 :                              std::move (outer_attrs), locus));
    1312         8021 : }
    1313              : 
    1314              : // Parses a use tree (which can be recursive and is actually a base class).
    1315              : template <typename ManagedTokenSource>
    1316              : std::unique_ptr<AST::UseTree>
    1317        15197 : Parser<ManagedTokenSource>::parse_use_tree ()
    1318              : {
    1319              :   /* potential syntax definitions in attempt to get algorithm:
    1320              :    *  Glob:
    1321              :    *      <- SimplePath :: *
    1322              :    *      <- :: *
    1323              :    *      <- *
    1324              :    *  Nested tree thing:
    1325              :    *      <- SimplePath :: { COMPLICATED_INNER_TREE_THING }
    1326              :    *      <- :: COMPLICATED_INNER_TREE_THING }
    1327              :    *      <- { COMPLICATED_INNER_TREE_THING }
    1328              :    *  Rebind thing:
    1329              :    *      <- SimplePath as IDENTIFIER
    1330              :    *      <- SimplePath as _
    1331              :    *      <- SimplePath
    1332              :    */
    1333              : 
    1334              :   /* current plan of attack: try to parse SimplePath first - if fails, one of
    1335              :    * top two then try parse :: - if fails, one of top two. Next is deciding
    1336              :    * character for top two. */
    1337              : 
    1338              :   /* Thus, parsing smaller parts of use tree may require feeding into function
    1339              :    * via parameters (or could handle all in this single function because other
    1340              :    * use tree types aren't recognised as separate in the spec) */
    1341              : 
    1342              :   // TODO: I think this function is too complex, probably should split it
    1343              : 
    1344        15197 :   location_t locus = lexer.peek_token ()->get_locus ();
    1345              : 
    1346              :   // bool has_path = false;
    1347        15197 :   auto path = parse_simple_path ();
    1348              : 
    1349        15197 :   if (!path)
    1350              :     {
    1351              :       // has no path, so must be glob or nested tree UseTree type
    1352              : 
    1353            7 :       bool is_global = false;
    1354              : 
    1355              :       // check for global scope resolution operator
    1356           14 :       if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
    1357              :         {
    1358            1 :           lexer.skip_token ();
    1359            1 :           is_global = true;
    1360              :         }
    1361              : 
    1362            7 :       const_TokenPtr t = lexer.peek_token ();
    1363            7 :       switch (t->get_id ())
    1364              :         {
    1365            5 :         case ASTERISK:
    1366              :           // glob UseTree type
    1367            5 :           lexer.skip_token ();
    1368              : 
    1369            5 :           if (is_global)
    1370            1 :             return std::unique_ptr<AST::UseTreeGlob> (
    1371            2 :               new AST::UseTreeGlob (AST::UseTreeGlob::GLOBAL,
    1372            3 :                                     AST::SimplePath::create_empty (), locus));
    1373              :           else
    1374            4 :             return std::unique_ptr<AST::UseTreeGlob> (
    1375            8 :               new AST::UseTreeGlob (AST::UseTreeGlob::NO_PATH,
    1376           12 :                                     AST::SimplePath::create_empty (), locus));
    1377            2 :         case LEFT_CURLY:
    1378              :           {
    1379              :             // nested tree UseTree type
    1380            2 :             lexer.skip_token ();
    1381              : 
    1382            2 :             std::vector<std::unique_ptr<AST::UseTree>> use_trees;
    1383              : 
    1384            2 :             const_TokenPtr t = lexer.peek_token ();
    1385            5 :             while (t->get_id () != RIGHT_CURLY)
    1386              :               {
    1387            3 :                 std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    1388            3 :                 if (use_tree == nullptr)
    1389              :                   {
    1390              :                     break;
    1391              :                   }
    1392              : 
    1393            3 :                 use_trees.push_back (std::move (use_tree));
    1394              : 
    1395            6 :                 if (lexer.peek_token ()->get_id () != COMMA)
    1396              :                   break;
    1397              : 
    1398            1 :                 lexer.skip_token ();
    1399            1 :                 t = lexer.peek_token ();
    1400              :               }
    1401              : 
    1402              :             // skip end curly delimiter
    1403            2 :             if (!skip_token (RIGHT_CURLY))
    1404              :               {
    1405              :                 // skip after somewhere?
    1406            0 :                 return nullptr;
    1407              :               }
    1408              : 
    1409            2 :             if (is_global)
    1410            0 :               return std::unique_ptr<AST::UseTreeList> (
    1411            0 :                 new AST::UseTreeList (AST::UseTreeList::GLOBAL,
    1412            0 :                                       AST::SimplePath::create_empty (),
    1413            0 :                                       std::move (use_trees), locus));
    1414              :             else
    1415            2 :               return std::unique_ptr<AST::UseTreeList> (
    1416            4 :                 new AST::UseTreeList (AST::UseTreeList::NO_PATH,
    1417            4 :                                       AST::SimplePath::create_empty (),
    1418            2 :                                       std::move (use_trees), locus));
    1419            2 :           }
    1420            0 :         case AS:
    1421              :           // this is not allowed
    1422            0 :           add_error (Error (
    1423              :             t->get_locus (),
    1424              :             "use declaration with rebind %<as%> requires a valid simple path - "
    1425              :             "none found"));
    1426              : 
    1427            0 :           skip_after_semicolon ();
    1428            0 :           return nullptr;
    1429            0 :         default:
    1430            0 :           add_error (Error (t->get_locus (),
    1431              :                             "unexpected token %qs in use tree with "
    1432              :                             "no valid simple path (i.e. list"
    1433              :                             " or glob use tree)",
    1434              :                             t->get_token_description ()));
    1435              : 
    1436            0 :           skip_after_semicolon ();
    1437            0 :           return nullptr;
    1438              :         }
    1439            7 :     }
    1440              :   else
    1441              :     {
    1442        15190 :       const_TokenPtr t = lexer.peek_token ();
    1443              : 
    1444              :       switch (t->get_id ())
    1445              :         {
    1446          137 :         case AS:
    1447              :           {
    1448              :             // rebind UseTree type
    1449          137 :             lexer.skip_token ();
    1450              : 
    1451          137 :             const_TokenPtr t = lexer.peek_token ();
    1452          137 :             switch (t->get_id ())
    1453              :               {
    1454          135 :               case IDENTIFIER:
    1455              :                 // skip lexer token
    1456          135 :                 lexer.skip_token ();
    1457              : 
    1458          135 :                 return std::unique_ptr<AST::UseTreeRebind> (
    1459          405 :                   new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER,
    1460          405 :                                           std::move (path.value ()), locus, t));
    1461            2 :               case UNDERSCORE:
    1462              :                 // skip lexer token
    1463            2 :                 lexer.skip_token ();
    1464              : 
    1465            2 :                 return std::unique_ptr<AST::UseTreeRebind> (
    1466            6 :                   new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
    1467            2 :                                           std::move (path.value ()), locus,
    1468              :                                           {Values::Keywords::UNDERSCORE,
    1469            2 :                                            t->get_locus ()}));
    1470            0 :               default:
    1471            0 :                 add_error (Error (
    1472              :                   t->get_locus (),
    1473              :                   "unexpected token %qs in use tree with as clause - expected "
    1474              :                   "identifier or %<_%>",
    1475              :                   t->get_token_description ()));
    1476              : 
    1477            0 :                 skip_after_semicolon ();
    1478            0 :                 return nullptr;
    1479              :               }
    1480          137 :           }
    1481        11320 :         case SEMICOLON:
    1482              :           // rebind UseTree type without rebinding - path only
    1483              : 
    1484              :           // don't skip semicolon - handled in parse_use_tree
    1485              :           // lexer.skip_token();
    1486              :         case COMMA:
    1487              :         case RIGHT_CURLY:
    1488              :           // this may occur in recursive calls - assume it is ok and ignore it
    1489        11320 :           return std::unique_ptr<AST::UseTreeRebind> (
    1490        33960 :             new AST::UseTreeRebind (AST::UseTreeRebind::NONE,
    1491        11320 :                                     std::move (path.value ()), locus));
    1492              :         case SCOPE_RESOLUTION:
    1493              :           // keep going
    1494              :           break;
    1495            1 :         default:
    1496            1 :           add_error (Error (t->get_locus (),
    1497              :                             "unexpected token %qs in use tree with valid path",
    1498              :                             t->get_token_description ()));
    1499            1 :           return nullptr;
    1500              :         }
    1501              : 
    1502         3732 :       skip_token ();
    1503         3732 :       t = lexer.peek_token ();
    1504              : 
    1505         3732 :       switch (t->get_id ())
    1506              :         {
    1507         1425 :         case ASTERISK:
    1508              :           // glob UseTree type
    1509         1425 :           lexer.skip_token ();
    1510              : 
    1511         1425 :           return std::unique_ptr<AST::UseTreeGlob> (
    1512         4275 :             new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
    1513         1425 :                                   std::move (path.value ()), locus));
    1514         2307 :         case LEFT_CURLY:
    1515              :           {
    1516              :             // nested tree UseTree type
    1517         2307 :             lexer.skip_token ();
    1518              : 
    1519         2307 :             std::vector<std::unique_ptr<AST::UseTree>> use_trees;
    1520              : 
    1521              :             // TODO: think of better control structure
    1522         2307 :             const_TokenPtr t = lexer.peek_token ();
    1523         9480 :             while (t->get_id () != RIGHT_CURLY)
    1524              :               {
    1525         7173 :                 std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    1526         7173 :                 if (use_tree == nullptr)
    1527              :                   {
    1528              :                     break;
    1529              :                   }
    1530              : 
    1531         7173 :                 use_trees.push_back (std::move (use_tree));
    1532              : 
    1533        14346 :                 if (lexer.peek_token ()->get_id () != COMMA)
    1534              :                   break;
    1535              : 
    1536         5120 :                 lexer.skip_token ();
    1537         5120 :                 t = lexer.peek_token ();
    1538              :               }
    1539              : 
    1540              :             // skip end curly delimiter
    1541         2307 :             if (!skip_token (RIGHT_CURLY))
    1542              :               {
    1543              :                 // skip after somewhere?
    1544            0 :                 return nullptr;
    1545              :               }
    1546              : 
    1547         2307 :             return std::unique_ptr<AST::UseTreeList> (
    1548         4614 :               new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
    1549         2307 :                                     std::move (path.value ()),
    1550         2307 :                                     std::move (use_trees), locus));
    1551         2307 :           }
    1552            0 :         default:
    1553            0 :           add_error (Error (t->get_locus (),
    1554              :                             "unexpected token %qs in use tree with valid path",
    1555              :                             t->get_token_description ()));
    1556              : 
    1557              :           // skip_after_semicolon();
    1558            0 :           return nullptr;
    1559              :         }
    1560        15190 :     }
    1561        15197 : }
    1562              : 
    1563              : // Parses a function (not a method).
    1564              : template <typename ManagedTokenSource>
    1565              : std::unique_ptr<AST::Function>
    1566        53342 : Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
    1567              :                                             AST::AttrVec outer_attrs,
    1568              :                                             bool is_external)
    1569              : {
    1570        53342 :   location_t locus = lexer.peek_token ()->get_locus ();
    1571              :   // Get qualifiers for function if they exist
    1572        53342 :   auto qualifiers = parse_function_qualifiers ();
    1573        53342 :   if (!qualifiers)
    1574            1 :     return nullptr;
    1575              : 
    1576        53341 :   skip_token (FN_KW);
    1577              : 
    1578              :   // Save function name token
    1579        53341 :   const_TokenPtr function_name_tok = expect_token (IDENTIFIER);
    1580        53341 :   if (function_name_tok == nullptr)
    1581              :     {
    1582            0 :       skip_after_next_block ();
    1583            0 :       return nullptr;
    1584              :     }
    1585       106682 :   Identifier function_name{function_name_tok};
    1586              : 
    1587              :   // parse generic params - if exist
    1588        53341 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    1589              :     = parse_generic_params_in_angles ();
    1590              : 
    1591        53341 :   if (!skip_token (LEFT_PAREN))
    1592              :     {
    1593            0 :       Error error (lexer.peek_token ()->get_locus (),
    1594              :                    "function declaration missing opening parentheses before "
    1595              :                    "parameter list");
    1596            0 :       add_error (std::move (error));
    1597              : 
    1598            0 :       skip_after_next_block ();
    1599            0 :       return nullptr;
    1600            0 :     }
    1601              : 
    1602        53341 :   auto initial_param = parse_self_param ();
    1603              : 
    1604        53341 :   if (!initial_param.has_value ()
    1605        53341 :       && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
    1606            0 :     return nullptr;
    1607              : 
    1608        58306 :   if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
    1609         2961 :     skip_token ();
    1610              : 
    1611              :   // parse function parameters (only if next token isn't right paren)
    1612        53341 :   std::vector<std::unique_ptr<AST::Param>> function_params;
    1613              : 
    1614       106682 :   if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
    1615              :     function_params
    1616        30571 :       = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    1617              : 
    1618        53341 :   if (initial_param.has_value ())
    1619         4965 :     function_params.insert (function_params.begin (),
    1620         4965 :                             std::move (*initial_param));
    1621              : 
    1622        53341 :   if (!skip_token (RIGHT_PAREN))
    1623              :     {
    1624            1 :       Error error (lexer.peek_token ()->get_locus (),
    1625              :                    "function declaration missing closing parentheses after "
    1626              :                    "parameter list");
    1627            1 :       add_error (std::move (error));
    1628              : 
    1629            1 :       skip_after_next_block ();
    1630            1 :       return nullptr;
    1631            1 :     }
    1632              : 
    1633              :   // parse function return type - if exists
    1634        53340 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    1635              : 
    1636              :   // parse where clause - if exists
    1637        53340 :   AST::WhereClause where_clause = parse_where_clause ();
    1638              : 
    1639        53340 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    1640       106680 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    1641        13247 :     lexer.skip_token ();
    1642              :   else
    1643              :     {
    1644        40093 :       auto block_expr = parse_block_expr ();
    1645        40093 :       if (!block_expr)
    1646           30 :         return nullptr;
    1647        40063 :       body = std::move (block_expr.value ());
    1648        40093 :     }
    1649              : 
    1650       146683 :   return std::unique_ptr<AST::Function> (new AST::Function (
    1651        53310 :     std::move (function_name), std::move (qualifiers.value ()),
    1652              :     std::move (generic_params), std::move (function_params),
    1653              :     std::move (return_type), std::move (where_clause), std::move (body),
    1654        53310 :     std::move (vis), std::move (outer_attrs), locus, is_external));
    1655       160023 : }
    1656              : 
    1657              : // Parses function or method qualifiers (i.e. const, unsafe, and extern).
    1658              : template <typename ManagedTokenSource>
    1659              : tl::expected<AST::FunctionQualifiers, Parse::Error::Node>
    1660       136253 : Parser<ManagedTokenSource>::parse_function_qualifiers ()
    1661              : {
    1662       136253 :   location_t locus = lexer.peek_token ()->get_locus ();
    1663              : 
    1664       136253 :   auto parsed = parse_function_qualifiers_raw (locus);
    1665       136253 :   if (!parsed)
    1666            3 :     return tl::unexpected<Parse::Error::Node> (parsed.error ());
    1667              : 
    1668       272500 :   return function_qualifiers_from_keywords (locus, std::move (parsed->first),
    1669       272500 :                                             std::move (parsed->second));
    1670       136253 : }
    1671              : 
    1672              : // Take the list of parsed function qualifiers and convert it to
    1673              : // the corrresponding flags to pass to the AST item constructor.
    1674              : //
    1675              : // This assumes ``keywords`` contains only those tokens that
    1676              : // map to qualifiers.
    1677              : template <typename ManagedTokenSource>
    1678              : tl::expected<AST::FunctionQualifiers, Parse::Error::Node>
    1679       136250 : Parser<ManagedTokenSource>::function_qualifiers_from_keywords (
    1680              :   location_t locus, const std::vector<TokenId> keywords, std::string abi)
    1681              : {
    1682       136250 :   Default default_status = Default::No;
    1683       136250 :   Async async_status = Async::No;
    1684       136250 :   Const const_status = Const::No;
    1685       136250 :   Unsafety unsafe_status = Unsafety::Normal;
    1686       136250 :   bool has_extern = false;
    1687              : 
    1688       186486 :   for (auto qualifier : keywords)
    1689              :     {
    1690        50236 :       switch (qualifier)
    1691              :         {
    1692          270 :         case IDENTIFIER:
    1693              :           // only "default" is valid in this context
    1694          270 :           default_status = Default::Yes;
    1695          270 :           continue;
    1696        11719 :         case CONST:
    1697        11719 :           const_status = Const::Yes;
    1698        11719 :           continue;
    1699           11 :         case ASYNC:
    1700           11 :           async_status = Async::Yes;
    1701           11 :           continue;
    1702        32979 :         case UNSAFE:
    1703        32979 :           unsafe_status = Unsafety::Unsafe;
    1704        32979 :           continue;
    1705         5257 :         case EXTERN_KW:
    1706         5257 :           has_extern = true;
    1707         5257 :           continue;
    1708            0 :         default:
    1709              :           // non-qualifier token in input
    1710            0 :           rust_unreachable ();
    1711              :         }
    1712              :     }
    1713              : 
    1714       272500 :   return AST::FunctionQualifiers (locus, default_status, async_status,
    1715              :                                   const_status, unsafe_status, has_extern,
    1716       136250 :                                   std::move (abi));
    1717              : }
    1718              : 
    1719              : // this consumes as many function qualifier tokens while ensuring
    1720              : // uniqueness.
    1721              : template <typename ManagedTokenSource>
    1722              : tl::expected<std::pair<std::vector<TokenId>, std::string>, Parse::Error::Node>
    1723       136253 : Parser<ManagedTokenSource>::parse_function_qualifiers_raw (location_t locus)
    1724              : {
    1725       136253 :   std::vector<TokenId> found_order;
    1726       136253 :   std::string abi;
    1727              : 
    1728              :   // this will terminate on duplicates or the first non-qualifier token
    1729        50246 :   while (true)
    1730              :     {
    1731       186498 :       auto token = lexer.peek_token ();
    1732       186498 :       const TokenId token_id = token->get_id ();
    1733       186498 :       location_t locus = lexer.peek_token ()->get_locus ();
    1734              : 
    1735       186498 :       switch (token_id)
    1736              :         {
    1737          272 :         case IDENTIFIER:
    1738          272 :           if (token->get_str () != Values::WeakKeywords::DEFAULT)
    1739              :             {
    1740              :               // only "default" is valid in this context, so this must
    1741              :               // be a non-qualifier keyword
    1742            0 :               goto done;
    1743              :             }
    1744              :           // fallthrough
    1745              :         case CONST:
    1746              :         case ASYNC:
    1747              :         case UNSAFE:
    1748        44988 :           found_order.push_back (token_id);
    1749        44988 :           lexer.skip_token ();
    1750        44988 :           break;
    1751         5258 :         case EXTERN_KW:
    1752              :           {
    1753         5258 :             found_order.push_back (token_id);
    1754         5258 :             lexer.skip_token ();
    1755              : 
    1756              :             // detect optional abi name
    1757         5258 :             const_TokenPtr next_tok = lexer.peek_token ();
    1758         5258 :             if (next_tok->get_id () == STRING_LITERAL)
    1759              :               {
    1760         5258 :                 abi = next_tok->get_str ();
    1761         5258 :                 lexer.skip_token ();
    1762              :               }
    1763         5258 :           }
    1764         5258 :           break;
    1765       136252 :         default:
    1766              :           // non-qualifier keyword
    1767       136252 :           goto done;
    1768              :         }
    1769              : 
    1770       100492 :       if (std::count (found_order.cbegin (), found_order.cend (), token_id) > 1)
    1771              :         {
    1772              :           // qualifiers mustn't appear twice
    1773            1 :           Error error (locus, "encountered duplicate function qualifier %qs",
    1774              :                        token->get_token_description ());
    1775            1 :           add_error (std::move (error));
    1776              : 
    1777              :           return tl::unexpected<Parse::Error::Node> (
    1778            1 :             Parse::Error::Node::MALFORMED);
    1779            1 :         }
    1780              :     }
    1781       136252 : done:
    1782              : 
    1783       136252 :   if (!ensure_function_qualifier_order (locus, found_order))
    1784            2 :     return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    1785              : 
    1786       272500 :   return make_pair (found_order, abi);
    1787       136253 : }
    1788              : 
    1789              : // Validate the order of the list of function qualifiers; this assumes that
    1790              : // ``found_order`` consists only of function qualifier tokens.
    1791              : //
    1792              : // If the order is illegal, the generated error message gives both the wrong
    1793              : // order as found in the source and the correct order according to Rust syntax
    1794              : // rules.
    1795              : template <typename ManagedTokenSource>
    1796              : bool
    1797       136252 : Parser<ManagedTokenSource>::ensure_function_qualifier_order (
    1798              :   location_t locus, const std::vector<TokenId> &found_order)
    1799              : {
    1800              :   // Check in order of default, const, async, unsafe, extern
    1801        50243 :   auto token_priority = [] (const TokenId id) {
    1802        50243 :     switch (id)
    1803              :       {
    1804              :       case IDENTIFIER: // "default"; the only "weak" keyword considered here
    1805              :         return 1;
    1806        11720 :       case CONST:
    1807        11720 :         return 2;
    1808           12 :       case ASYNC:
    1809           12 :         return 3;
    1810        32981 :       case UNSAFE:
    1811        32981 :         return 4;
    1812         5258 :       case EXTERN_KW:
    1813         5258 :         return 5;
    1814            0 :       default:
    1815            0 :         rust_unreachable ();
    1816              :       };
    1817              :   };
    1818              : 
    1819       136252 :   size_t last_priority = 0;
    1820       186493 :   for (auto token_id : found_order)
    1821              :     {
    1822        50243 :       const size_t priority = token_priority (token_id);
    1823        50243 :       if (priority <= last_priority)
    1824              :         {
    1825            2 :           emit_function_qualifier_order_error_msg (locus, found_order);
    1826            2 :           return false;
    1827              :         }
    1828              : 
    1829        50241 :       last_priority = priority;
    1830              :     }
    1831              : 
    1832              :   return true;
    1833              : }
    1834              : 
    1835              : template <typename ManagedTokenSource>
    1836              : void
    1837            2 : Parser<ManagedTokenSource>::emit_function_qualifier_order_error_msg (
    1838              :   location_t locus, const std::vector<TokenId> &found_order)
    1839              : {
    1840            2 :   std::vector<TokenId> expected_order
    1841              :     = {IDENTIFIER, CONST, ASYNC, UNSAFE, EXTERN_KW};
    1842              : 
    1843              :   // we only keep the qualifiers actually used in the offending code
    1844            2 :   std::vector<TokenId>::iterator token_id = expected_order.begin ();
    1845           12 :   while (token_id != expected_order.end ())
    1846              :     {
    1847           20 :       if (std::find (found_order.cbegin (), found_order.cend (), *token_id)
    1848           20 :           == found_order.cend ())
    1849              :         {
    1850            3 :           token_id = expected_order.erase (token_id);
    1851              :         }
    1852              :       else
    1853              :         {
    1854            7 :           ++token_id;
    1855              :         }
    1856              :     }
    1857              : 
    1858            4 :   auto qualifiers_to_str = [] (const std::vector<TokenId> &token_ids) {
    1859            4 :     std::ostringstream ss;
    1860              : 
    1861           18 :     for (auto id : token_ids)
    1862              :       {
    1863           14 :         if (ss.tellp () != 0)
    1864           10 :           ss << ' ';
    1865              : 
    1866           14 :         if (id == IDENTIFIER)
    1867            4 :           ss << Values::WeakKeywords::DEFAULT;
    1868              :         else
    1869           10 :           ss << token_id_keyword_string (id);
    1870              :       }
    1871              : 
    1872            8 :     return ss.str ();
    1873            4 :   };
    1874              : 
    1875            2 :   const std::string found_qualifiers = qualifiers_to_str (found_order);
    1876            2 :   const std::string expected_qualifiers = qualifiers_to_str (expected_order);
    1877              : 
    1878              :   location_t error_locus
    1879            2 :     = make_location (locus, locus, lexer.peek_token ()->get_locus ());
    1880            2 :   Error error (error_locus,
    1881              :                "invalid order of function qualifiers; found %qs, expected %qs",
    1882              :                found_qualifiers.c_str (), expected_qualifiers.c_str ());
    1883            2 :   add_error (std::move (error));
    1884            2 : }
    1885              : 
    1886              : // Parses generic (lifetime or type) params inside angle brackets (optional).
    1887              : template <typename ManagedTokenSource>
    1888              : std::vector<std::unique_ptr<AST::GenericParam>>
    1889       227167 : Parser<ManagedTokenSource>::parse_generic_params_in_angles ()
    1890              : {
    1891       454334 :   if (lexer.peek_token ()->get_id () != LEFT_ANGLE)
    1892              :     {
    1893              :       // seems to be no generic params, so exit with empty vector
    1894       192403 :       return std::vector<std::unique_ptr<AST::GenericParam>> ();
    1895              :     }
    1896        34764 :   lexer.skip_token ();
    1897              : 
    1898              :   // DEBUG:
    1899        34764 :   rust_debug ("skipped left angle in generic param");
    1900              : 
    1901        34764 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    1902              :     = parse_generic_params (Parse::Utils::is_right_angle_tok);
    1903              : 
    1904              :   // DEBUG:
    1905        34764 :   rust_debug ("finished parsing actual generic params (i.e. inside angles)");
    1906              : 
    1907        34764 :   if (!skip_generics_right_angle ())
    1908              :     {
    1909              :       // DEBUG
    1910            1 :       rust_debug ("failed to skip generics right angle - returning empty "
    1911              :                   "generic params");
    1912              : 
    1913            1 :       return std::vector<std::unique_ptr<AST::GenericParam>> ();
    1914              :     }
    1915              : 
    1916        34763 :   return generic_params;
    1917        34764 : }
    1918              : 
    1919              : template <typename ManagedTokenSource>
    1920              : template <typename EndTokenPred>
    1921              : std::unique_ptr<AST::GenericParam>
    1922        74793 : Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
    1923              : {
    1924        74793 :   auto outer_attrs = parse_outer_attributes ();
    1925        74793 :   std::unique_ptr<AST::GenericParam> param;
    1926        74793 :   auto token = lexer.peek_token ();
    1927              : 
    1928        74793 :   switch (token->get_id ())
    1929              :     {
    1930         9465 :     case LIFETIME:
    1931              :       {
    1932         9465 :         auto lifetime = parse_lifetime (false);
    1933         9465 :         if (!lifetime)
    1934              :           {
    1935            0 :             Error error (token->get_locus (),
    1936              :                          "failed to parse lifetime in generic parameter list");
    1937            0 :             add_error (std::move (error));
    1938              : 
    1939            0 :             return nullptr;
    1940            0 :           }
    1941              : 
    1942         9465 :         std::vector<AST::Lifetime> lifetime_bounds;
    1943        18930 :         if (lexer.peek_token ()->get_id () == COLON)
    1944              :           {
    1945          151 :             lexer.skip_token ();
    1946              :             // parse required bounds
    1947              :             lifetime_bounds
    1948          151 :               = parse_lifetime_bounds ([is_end_token] (TokenId id) {
    1949          159 :                   return is_end_token (id) || id == COMMA;
    1950              :                 });
    1951              :           }
    1952              : 
    1953        18930 :         param = std::unique_ptr<AST::LifetimeParam> (new AST::LifetimeParam (
    1954         9465 :           std::move (lifetime.value ()), std::move (lifetime_bounds),
    1955         9465 :           std::move (outer_attrs), token->get_locus ()));
    1956              :         break;
    1957        18930 :       }
    1958        64623 :     case IDENTIFIER:
    1959              :       {
    1960        64623 :         auto type_ident = token->get_str ();
    1961        64623 :         lexer.skip_token ();
    1962              : 
    1963        64623 :         std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    1964       129246 :         if (lexer.peek_token ()->get_id () == COLON)
    1965              :           {
    1966        15226 :             lexer.skip_token ();
    1967              : 
    1968              :             // parse optional type param bounds
    1969        15226 :             type_param_bounds = parse_type_param_bounds ();
    1970              :           }
    1971              : 
    1972        64623 :         std::unique_ptr<AST::Type> type = nullptr;
    1973       129246 :         if (lexer.peek_token ()->get_id () == EQUAL)
    1974              :           {
    1975          642 :             lexer.skip_token ();
    1976              : 
    1977              :             // parse required type
    1978          642 :             type = parse_type ();
    1979          642 :             if (!type)
    1980              :               {
    1981            0 :                 Error error (
    1982            0 :                   lexer.peek_token ()->get_locus (),
    1983              :                   "failed to parse type in type param in generic params");
    1984            0 :                 add_error (std::move (error));
    1985              : 
    1986            0 :                 return nullptr;
    1987            0 :               }
    1988              :           }
    1989              : 
    1990        64623 :         param = std::unique_ptr<AST::TypeParam> (
    1991       193869 :           new AST::TypeParam (std::move (type_ident), token->get_locus (),
    1992              :                               std::move (type_param_bounds), std::move (type),
    1993        64623 :                               std::move (outer_attrs)));
    1994              :         break;
    1995        64623 :       }
    1996          704 :     case CONST:
    1997              :       {
    1998          704 :         lexer.skip_token ();
    1999              : 
    2000          704 :         auto name_token = expect_token (IDENTIFIER);
    2001              : 
    2002         1408 :         if (!name_token || !expect_token (COLON))
    2003            1 :           return nullptr;
    2004              : 
    2005          703 :         auto type = parse_type ();
    2006          703 :         if (!type)
    2007            1 :           return nullptr;
    2008              : 
    2009              :         // optional default value
    2010          702 :         tl::optional<AST::GenericArg> default_expr = tl::nullopt;
    2011         1404 :         if (lexer.peek_token ()->get_id () == EQUAL)
    2012              :           {
    2013           24 :             lexer.skip_token ();
    2014           24 :             auto tok = lexer.peek_token ();
    2015           47 :             default_expr = parse_generic_arg ();
    2016              : 
    2017           24 :             if (!default_expr)
    2018              :               {
    2019            1 :                 Error error (tok->get_locus (),
    2020              :                              "invalid token for start of default value for "
    2021              :                              "const generic parameter: expected %<block%>, "
    2022              :                              "%<identifier%> or %<literal%>, got %qs",
    2023              :                              token_id_to_str (tok->get_id ()));
    2024              : 
    2025            1 :                 add_error (std::move (error));
    2026            1 :                 return nullptr;
    2027            1 :               }
    2028              : 
    2029              :             // At this point, we *know* that we are parsing a const
    2030              :             // expression
    2031           23 :             if (default_expr.value ().get_kind ()
    2032              :                 == AST::GenericArg::Kind::Either)
    2033            1 :               default_expr = default_expr.value ().disambiguate_to_const ();
    2034           22 :             else if (default_expr.value ().get_kind ()
    2035              :                      != AST::GenericArg::Kind::Const)
    2036              :               {
    2037            1 :                 Error error (
    2038            1 :                   default_expr.value ().get_locus (),
    2039              :                   "expressions must be enclosed in braces to be used as const "
    2040              :                   "generic arguments");
    2041            1 :                 add_error (std::move (error));
    2042            1 :                 default_expr = tl::nullopt;
    2043            1 :               }
    2044           24 :           }
    2045              : 
    2046          701 :         param = std::unique_ptr<AST::ConstGenericParam> (
    2047         2826 :           new AST::ConstGenericParam (name_token->get_str (), std::move (type),
    2048              :                                       default_expr, std::move (outer_attrs),
    2049          701 :                                       token->get_locus ()));
    2050              : 
    2051              :         break;
    2052         1407 :       }
    2053            1 :     default:
    2054              :       // FIXME: Can we clean this last call with a method call?
    2055            1 :       Error error (token->get_locus (),
    2056              :                    "unexpected token when parsing generic parameters: %qs",
    2057            1 :                    token->as_string ().c_str ());
    2058            1 :       add_error (std::move (error));
    2059              : 
    2060            1 :       return nullptr;
    2061            1 :     }
    2062              : 
    2063        74789 :   return param;
    2064        74793 : }
    2065              : 
    2066              : /* Parse generic (lifetime or type) params NOT INSIDE ANGLE BRACKETS!!! Almost
    2067              :  * always parse_generic_params_in_angles is what is wanted. */
    2068              : template <typename ManagedTokenSource>
    2069              : template <typename EndTokenPred>
    2070              : std::vector<std::unique_ptr<AST::GenericParam>>
    2071        34764 : Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
    2072              : {
    2073        34764 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
    2074              : 
    2075              :   /* can't parse lifetime and type params separately due to lookahead issues
    2076              :    * thus, parse them all here */
    2077              : 
    2078              :   /* HACK: used to retain attribute data if a lifetime param is tentatively
    2079              :    * parsed but it turns out to be type param */
    2080        34764 :   AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
    2081              : 
    2082              :   // Did we parse a generic type param yet
    2083        34764 :   auto type_seen = false;
    2084              :   // Did we parse a const param with a default value yet
    2085        34764 :   auto const_with_default_seen = false;
    2086              :   // Did the user write a lifetime parameter after a type one
    2087        34764 :   auto order_error = false;
    2088              :   // Did the user write a const param with a default value after a type one
    2089        34764 :   auto const_with_default_order_error = false;
    2090              : 
    2091              :   // parse lifetime params
    2092       293899 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    2093              :     {
    2094        74793 :       auto param = parse_generic_param (is_end_token);
    2095        74793 :       if (param)
    2096              :         {
    2097        74789 :           if (param->get_kind () == AST::GenericParam::Kind::Type)
    2098              :             {
    2099        64623 :               type_seen = true;
    2100        64623 :               if (const_with_default_seen)
    2101        74789 :                 const_with_default_order_error = true;
    2102              :             }
    2103        10166 :           else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
    2104        10166 :                    && type_seen)
    2105              :             {
    2106            2 :               order_error = true;
    2107            2 :               if (const_with_default_seen)
    2108            0 :                 const_with_default_order_error = true;
    2109              :             }
    2110        10164 :           else if (param->get_kind () == AST::GenericParam::Kind::Const)
    2111              :             {
    2112          701 :               type_seen = true;
    2113              :               AST::ConstGenericParam *const_param
    2114          701 :                 = static_cast<AST::ConstGenericParam *> (param.get ());
    2115          701 :               if (const_param->has_default_value ())
    2116              :                 const_with_default_seen = true;
    2117          679 :               else if (const_with_default_seen)
    2118        74789 :                 const_with_default_order_error = true;
    2119              :             }
    2120              : 
    2121        74789 :           generic_params.emplace_back (std::move (param));
    2122        74789 :           maybe_skip_token (COMMA);
    2123              :         }
    2124              :       else
    2125              :         break;
    2126              :     }
    2127              : 
    2128              :   // FIXME: Add reordering hint
    2129        34764 :   if (order_error)
    2130              :     {
    2131            2 :       Error error (generic_params.front ()->get_locus (),
    2132              :                    "invalid order for generic parameters: lifetime parameters "
    2133              :                    "must be declared prior to type and const parameters");
    2134            2 :       add_error (std::move (error));
    2135            2 :     }
    2136        34764 :   if (const_with_default_order_error)
    2137              :     {
    2138            2 :       Error error (generic_params.front ()->get_locus (),
    2139              :                    "invalid order for generic parameters: generic parameters "
    2140              :                    "with a default must be trailing");
    2141            2 :       add_error (std::move (error));
    2142            2 :     }
    2143              : 
    2144        34764 :   generic_params.shrink_to_fit ();
    2145        34764 :   return generic_params;
    2146        34764 : }
    2147              : 
    2148              : /* Parses lifetime generic parameters (pointers). Will also consume any
    2149              :  * trailing comma. No extra checks for end token. */
    2150              : template <typename ManagedTokenSource>
    2151              : std::vector<std::unique_ptr<AST::LifetimeParam>>
    2152          108 : Parser<ManagedTokenSource>::parse_lifetime_params ()
    2153              : {
    2154          108 :   std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
    2155              : 
    2156          356 :   while (lexer.peek_token ()->get_id () != END_OF_FILE)
    2157              :     {
    2158          124 :       auto lifetime_param = parse_lifetime_param ();
    2159              : 
    2160          124 :       if (!lifetime_param)
    2161              :         {
    2162              :           // can't treat as error as only way to get out with trailing comma
    2163              :           break;
    2164              :         }
    2165              : 
    2166           50 :       lifetime_params.emplace_back (
    2167           50 :         new AST::LifetimeParam (std::move (lifetime_param.value ())));
    2168              : 
    2169          100 :       if (lexer.peek_token ()->get_id () != COMMA)
    2170              :         break;
    2171              : 
    2172              :       // skip commas, including trailing commas
    2173           16 :       lexer.skip_token ();
    2174              :     }
    2175              : 
    2176          108 :   lifetime_params.shrink_to_fit ();
    2177              : 
    2178          108 :   return lifetime_params;
    2179              : }
    2180              : 
    2181              : /* Parses lifetime generic parameters (pointers). Will also consume any
    2182              :  * trailing comma. Has extra is_end_token predicate checking. */
    2183              : template <typename ManagedTokenSource>
    2184              : template <typename EndTokenPred>
    2185              : std::vector<std::unique_ptr<AST::LifetimeParam>>
    2186              : Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token)
    2187              : {
    2188              :   std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
    2189              : 
    2190              :   // if end_token is not specified, it defaults to EOF, so should work fine
    2191              :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    2192              :     {
    2193              :       auto lifetime_param = parse_lifetime_param ();
    2194              : 
    2195              :       if (!lifetime_param)
    2196              :         {
    2197              :           /* TODO: is it worth throwing away all lifetime params just because
    2198              :            * one failed? */
    2199              :           Error error (lexer.peek_token ()->get_locus (),
    2200              :                        "failed to parse lifetime param in lifetime params");
    2201              :           add_error (std::move (error));
    2202              : 
    2203              :           return {};
    2204              :         }
    2205              : 
    2206              :       lifetime_params.emplace_back (
    2207              :         new AST::LifetimeParam (std::move (lifetime_param)));
    2208              : 
    2209              :       if (lexer.peek_token ()->get_id () != COMMA)
    2210              :         break;
    2211              : 
    2212              :       // skip commas, including trailing commas
    2213              :       lexer.skip_token ();
    2214              :     }
    2215              : 
    2216              :   lifetime_params.shrink_to_fit ();
    2217              : 
    2218              :   return lifetime_params;
    2219              : }
    2220              : 
    2221              : /* Parses lifetime generic parameters (objects). Will also consume any
    2222              :  * trailing comma. No extra checks for end token.
    2223              :  * TODO: is this best solution? implements most of the same algorithm.
    2224              :  * TODO: seems to be unused, remove? */
    2225              : template <typename ManagedTokenSource>
    2226              : std::vector<AST::LifetimeParam>
    2227            0 : Parser<ManagedTokenSource>::parse_lifetime_params_objs ()
    2228              : {
    2229            0 :   std::vector<AST::LifetimeParam> lifetime_params;
    2230              : 
    2231              :   // bad control structure as end token cannot be guaranteed
    2232            0 :   while (true)
    2233              :     {
    2234            0 :       auto lifetime_param = parse_lifetime_param ();
    2235              : 
    2236            0 :       if (!lifetime_param)
    2237              :         {
    2238              :           // not an error as only way to exit if trailing comma
    2239              :           break;
    2240              :         }
    2241              : 
    2242            0 :       lifetime_params.push_back (std::move (lifetime_param.value ()));
    2243              : 
    2244            0 :       if (lexer.peek_token ()->get_id () != COMMA)
    2245              :         break;
    2246              : 
    2247              :       // skip commas, including trailing commas
    2248            0 :       lexer.skip_token ();
    2249              :     }
    2250              : 
    2251            0 :   lifetime_params.shrink_to_fit ();
    2252              : 
    2253            0 :   return lifetime_params;
    2254              : }
    2255              : 
    2256              : /* Parses lifetime generic parameters (objects). Will also consume any
    2257              :  * trailing comma. Has extra is_end_token predicate checking.
    2258              :  * TODO: is this best solution? implements most of the same algorithm. */
    2259              : template <typename ManagedTokenSource>
    2260              : template <typename EndTokenPred>
    2261              : std::vector<AST::LifetimeParam>
    2262           36 : Parser<ManagedTokenSource>::parse_lifetime_params_objs (
    2263              :   EndTokenPred is_end_token)
    2264              : {
    2265           36 :   std::vector<AST::LifetimeParam> lifetime_params;
    2266              : 
    2267          108 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    2268              :     {
    2269           36 :       auto lifetime_param = parse_lifetime_param ();
    2270              : 
    2271           36 :       if (!lifetime_param)
    2272              :         {
    2273              :           /* TODO: is it worth throwing away all lifetime params just because
    2274              :            * one failed? */
    2275            0 :           Error error (lexer.peek_token ()->get_locus (),
    2276              :                        "failed to parse lifetime param in lifetime params");
    2277            0 :           add_error (std::move (error));
    2278              : 
    2279            0 :           return {};
    2280            0 :         }
    2281              : 
    2282           36 :       lifetime_params.push_back (std::move (lifetime_param.value ()));
    2283              : 
    2284           72 :       if (lexer.peek_token ()->get_id () != COMMA)
    2285              :         break;
    2286              : 
    2287              :       // skip commas, including trailing commas
    2288            0 :       lexer.skip_token ();
    2289              :     }
    2290              : 
    2291           36 :   lifetime_params.shrink_to_fit ();
    2292              : 
    2293           36 :   return lifetime_params;
    2294           36 : }
    2295              : 
    2296              : /* Parses a sequence of a certain grammar rule in object form (not pointer or
    2297              :  * smart pointer), delimited by commas and ending when 'is_end_token' is
    2298              :  * satisfied (templated). Will also consume any trailing comma.
    2299              :  * FIXME: this cannot be used due to member function pointer problems (i.e.
    2300              :  * parsing_function cannot be specified properly) */
    2301              : template <typename ManagedTokenSource>
    2302              : template <typename ParseFunction, typename EndTokenPred>
    2303              : auto
    2304              : Parser<ManagedTokenSource>::parse_non_ptr_sequence (
    2305              :   ParseFunction parsing_function, EndTokenPred is_end_token,
    2306              :   std::string error_msg) -> std::vector<decltype (parsing_function ())>
    2307              : {
    2308              :   std::vector<decltype (parsing_function ())> params;
    2309              : 
    2310              :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    2311              :     {
    2312              :       auto param = parsing_function ();
    2313              : 
    2314              :       if (param.is_error ())
    2315              :         {
    2316              :           // TODO: is it worth throwing away all params just because one
    2317              :           // failed?
    2318              :           Error error (lexer.peek_token ()->get_locus (),
    2319              :                        std::move (error_msg));
    2320              :           add_error (std::move (error));
    2321              : 
    2322              :           return {};
    2323              :         }
    2324              : 
    2325              :       params.push_back (std::move (param));
    2326              : 
    2327              :       if (lexer.peek_token ()->get_id () != COMMA)
    2328              :         break;
    2329              : 
    2330              :       // skip commas, including trailing commas
    2331              :       lexer.skip_token ();
    2332              :     }
    2333              : 
    2334              :   params.shrink_to_fit ();
    2335              : 
    2336              :   return params;
    2337              : }
    2338              : 
    2339              : /* Parses a single lifetime generic parameter (not including comma). */
    2340              : template <typename ManagedTokenSource>
    2341              : tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
    2342          160 : Parser<ManagedTokenSource>::parse_lifetime_param ()
    2343              : {
    2344              :   // parse outer attributes, which are optional and may not exist
    2345          160 :   auto outer_attrs = parse_outer_attributes ();
    2346              : 
    2347              :   // save lifetime token - required
    2348          160 :   const_TokenPtr lifetime_tok = lexer.peek_token ();
    2349          160 :   if (lifetime_tok->get_id () != LIFETIME)
    2350              :     {
    2351              :       // if lifetime is missing, must not be a lifetime param, so return error
    2352           74 :       return Parse::Error::LifetimeParam::make_not_a_lifetime_param ();
    2353              :     }
    2354           86 :   lexer.skip_token ();
    2355          172 :   AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
    2356              :                           lifetime_tok->get_locus ());
    2357              : 
    2358              :   // parse lifetime bounds, if it exists
    2359           86 :   std::vector<AST::Lifetime> lifetime_bounds;
    2360          172 :   if (lexer.peek_token ()->get_id () == COLON)
    2361              :     {
    2362              :       // parse lifetime bounds
    2363            0 :       lifetime_bounds = parse_lifetime_bounds ();
    2364              :       // TODO: have end token passed in?
    2365              :     }
    2366              : 
    2367          172 :   return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
    2368              :                              std::move (outer_attrs),
    2369           86 :                              lifetime_tok->get_locus ());
    2370          246 : }
    2371              : 
    2372              : // Parses type generic parameters. Will also consume any trailing comma.
    2373              : template <typename ManagedTokenSource>
    2374              : std::vector<std::unique_ptr<AST::TypeParam>>
    2375            0 : Parser<ManagedTokenSource>::parse_type_params ()
    2376              : {
    2377            0 :   std::vector<std::unique_ptr<AST::TypeParam>> type_params;
    2378              : 
    2379              :   // infinite loop with break on failure as no info on ending token
    2380            0 :   while (true)
    2381              :     {
    2382            0 :       std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
    2383              : 
    2384            0 :       if (type_param == nullptr)
    2385              :         {
    2386              :           // break if fails to parse
    2387              :           break;
    2388              :         }
    2389              : 
    2390            0 :       type_params.push_back (std::move (type_param));
    2391              : 
    2392            0 :       if (lexer.peek_token ()->get_id () != COMMA)
    2393              :         break;
    2394              : 
    2395              :       // skip commas, including trailing commas
    2396            0 :       lexer.skip_token ();
    2397              :     }
    2398              : 
    2399            0 :   type_params.shrink_to_fit ();
    2400            0 :   return type_params;
    2401              : }
    2402              : 
    2403              : // Parses type generic parameters. Will also consume any trailing comma.
    2404              : template <typename ManagedTokenSource>
    2405              : template <typename EndTokenPred>
    2406              : std::vector<std::unique_ptr<AST::TypeParam>>
    2407              : Parser<ManagedTokenSource>::parse_type_params (EndTokenPred is_end_token)
    2408              : {
    2409              :   std::vector<std::unique_ptr<AST::TypeParam>> type_params;
    2410              : 
    2411              :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    2412              :     {
    2413              :       std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
    2414              : 
    2415              :       if (type_param == nullptr)
    2416              :         {
    2417              :           Error error (lexer.peek_token ()->get_locus (),
    2418              :                        "failed to parse type param in type params");
    2419              :           add_error (std::move (error));
    2420              : 
    2421              :           return {};
    2422              :         }
    2423              : 
    2424              :       type_params.push_back (std::move (type_param));
    2425              : 
    2426              :       if (lexer.peek_token ()->get_id () != COMMA)
    2427              :         break;
    2428              : 
    2429              :       // skip commas, including trailing commas
    2430              :       lexer.skip_token ();
    2431              :     }
    2432              : 
    2433              :   type_params.shrink_to_fit ();
    2434              :   return type_params;
    2435              :   /* TODO: this shares most code with parse_lifetime_params - good place to
    2436              :    * use template (i.e. parse_non_ptr_sequence if doable) */
    2437              : }
    2438              : 
    2439              : /* Parses a single type (generic) parameter, not including commas. May change
    2440              :  * to return value. */
    2441              : template <typename ManagedTokenSource>
    2442              : std::unique_ptr<AST::TypeParam>
    2443            0 : Parser<ManagedTokenSource>::parse_type_param ()
    2444              : {
    2445              :   // parse outer attributes, which are optional and may not exist
    2446            0 :   auto outer_attrs = parse_outer_attributes ();
    2447              : 
    2448            0 :   const_TokenPtr identifier_tok = lexer.peek_token ();
    2449            0 :   if (identifier_tok->get_id () != IDENTIFIER)
    2450              :     {
    2451              :       // return null as type param can't exist without this required
    2452              :       // identifier
    2453            0 :       return nullptr;
    2454              :     }
    2455            0 :   Identifier ident{identifier_tok};
    2456            0 :   lexer.skip_token ();
    2457              : 
    2458              :   // parse type param bounds (if they exist)
    2459            0 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    2460            0 :   if (lexer.peek_token ()->get_id () == COLON)
    2461              :     {
    2462            0 :       lexer.skip_token ();
    2463              : 
    2464              :       // parse type param bounds, which may or may not exist
    2465            0 :       type_param_bounds = parse_type_param_bounds ();
    2466              :     }
    2467              : 
    2468              :   // parse type (if it exists)
    2469            0 :   std::unique_ptr<AST::Type> type = nullptr;
    2470            0 :   if (lexer.peek_token ()->get_id () == EQUAL)
    2471              :     {
    2472            0 :       lexer.skip_token ();
    2473              : 
    2474              :       // parse type (now required)
    2475            0 :       type = parse_type ();
    2476            0 :       if (type == nullptr)
    2477              :         {
    2478            0 :           Error error (lexer.peek_token ()->get_locus (),
    2479              :                        "failed to parse type in type param");
    2480            0 :           add_error (std::move (error));
    2481              : 
    2482            0 :           return nullptr;
    2483            0 :         }
    2484              :     }
    2485              : 
    2486              :   return std::unique_ptr<AST::TypeParam> (
    2487            0 :     new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
    2488              :                         std::move (type_param_bounds), std::move (type),
    2489            0 :                         std::move (outer_attrs)));
    2490            0 : }
    2491              : 
    2492              : /* Parses regular (i.e. non-generic) parameters in functions or methods. Also
    2493              :  * has end token handling. */
    2494              : template <typename ManagedTokenSource>
    2495              : template <typename EndTokenPred>
    2496              : std::vector<std::unique_ptr<AST::Param>>
    2497        97931 : Parser<ManagedTokenSource>::parse_function_params (EndTokenPred is_end_token)
    2498              : {
    2499        97931 :   std::vector<std::unique_ptr<AST::Param>> params;
    2500              : 
    2501       195862 :   if (is_end_token (lexer.peek_token ()->get_id ()))
    2502         8209 :     return params;
    2503              : 
    2504        89722 :   auto initial_param = parse_function_param ();
    2505              : 
    2506              :   // Return empty parameter list if no parameter there
    2507        89722 :   if (initial_param == nullptr)
    2508              :     {
    2509              :       // TODO: is this an error?
    2510            1 :       return params;
    2511              :     }
    2512              : 
    2513        89721 :   params.push_back (std::move (initial_param));
    2514              : 
    2515              :   // maybe think of a better control structure here - do-while with an initial
    2516              :   // error state? basically, loop through parameter list until can't find any
    2517              :   // more params
    2518        89721 :   const_TokenPtr t = lexer.peek_token ();
    2519       130795 :   while (t->get_id () == COMMA)
    2520              :     {
    2521              :       // skip comma if applies
    2522        43406 :       lexer.skip_token ();
    2523              : 
    2524              :       // TODO: strictly speaking, shouldn't there be no trailing comma?
    2525        86812 :       if (is_end_token (lexer.peek_token ()->get_id ()))
    2526              :         break;
    2527              : 
    2528              :       // now, as right paren would break, function param is required
    2529        41074 :       auto param = parse_function_param ();
    2530        41074 :       if (param == nullptr)
    2531              :         {
    2532            0 :           Error error (lexer.peek_token ()->get_locus (),
    2533              :                        "failed to parse function param (in function params)");
    2534            0 :           add_error (std::move (error));
    2535              : 
    2536              :           // skip somewhere?
    2537            0 :           return std::vector<std::unique_ptr<AST::Param>> ();
    2538            0 :         }
    2539              : 
    2540        41074 :       params.push_back (std::move (param));
    2541              : 
    2542        41074 :       t = lexer.peek_token ();
    2543              :     }
    2544              : 
    2545        89721 :   params.shrink_to_fit ();
    2546        89721 :   return params;
    2547        97931 : }
    2548              : 
    2549              : /* Parses a single regular (i.e. non-generic) parameter in a function or
    2550              :  * method, i.e. the "name: type" bit. Also handles it not existing. */
    2551              : template <typename ManagedTokenSource>
    2552              : std::unique_ptr<AST::Param>
    2553       130796 : Parser<ManagedTokenSource>::parse_function_param ()
    2554              : {
    2555              :   // parse outer attributes if they exist
    2556       130796 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    2557              : 
    2558              :   // TODO: should saved location be at start of outer attributes or pattern?
    2559       130796 :   location_t locus = lexer.peek_token ()->get_locus ();
    2560              : 
    2561       261592 :   if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic
    2562              :     {
    2563          894 :       lexer.skip_token (); // Skip ellipsis
    2564          894 :       return std::make_unique<AST::VariadicParam> (
    2565         1788 :         AST::VariadicParam (std::move (outer_attrs), locus));
    2566              :     }
    2567              : 
    2568       129902 :   std::unique_ptr<AST::Pattern> param_pattern = parse_pattern ();
    2569              : 
    2570              :   // create error function param if it doesn't exist
    2571       129902 :   if (param_pattern == nullptr)
    2572              :     {
    2573              :       // skip after something
    2574            1 :       return nullptr;
    2575              :     }
    2576              : 
    2577       129901 :   if (!skip_token (COLON))
    2578              :     {
    2579              :       // skip after something
    2580            0 :       return nullptr;
    2581              :     }
    2582              : 
    2583       259802 :   if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic
    2584              :     {
    2585           11 :       lexer.skip_token (); // Skip ellipsis
    2586           11 :       return std::make_unique<AST::VariadicParam> (
    2587           22 :         AST::VariadicParam (std::move (param_pattern), std::move (outer_attrs),
    2588           11 :                             locus));
    2589              :     }
    2590              :   else
    2591              :     {
    2592       129890 :       std::unique_ptr<AST::Type> param_type = parse_type ();
    2593       129890 :       if (param_type == nullptr)
    2594              :         {
    2595            0 :           return nullptr;
    2596              :         }
    2597       129890 :       return std::make_unique<AST::FunctionParam> (
    2598       259780 :         AST::FunctionParam (std::move (param_pattern), std::move (param_type),
    2599       129890 :                             std::move (outer_attrs), locus));
    2600       129890 :     }
    2601       130796 : }
    2602              : 
    2603              : /* Parses a function or method return type syntactical construction. Also
    2604              :  * handles a function return type not existing. */
    2605              : template <typename ManagedTokenSource>
    2606              : std::unique_ptr<AST::Type>
    2607       135339 : Parser<ManagedTokenSource>::parse_function_return_type ()
    2608              : {
    2609       270678 :   if (lexer.peek_token ()->get_id () != RETURN_TYPE)
    2610        32381 :     return nullptr;
    2611              : 
    2612              :   // skip return type, as it now obviously exists
    2613       102958 :   lexer.skip_token ();
    2614              : 
    2615       102958 :   std::unique_ptr<AST::Type> type = parse_type ();
    2616              : 
    2617       102958 :   return type;
    2618       102958 : }
    2619              : 
    2620              : /* Parses a "where clause" (in a function, struct, method, etc.). Also handles
    2621              :  * a where clause not existing, in which it will return
    2622              :  * WhereClause::create_empty(), which can be checked via
    2623              :  * WhereClause::is_empty(). */
    2624              : template <typename ManagedTokenSource>
    2625              : AST::WhereClause
    2626       227144 : Parser<ManagedTokenSource>::parse_where_clause ()
    2627              : {
    2628       227144 :   const_TokenPtr where_tok = lexer.peek_token ();
    2629       227144 :   if (where_tok->get_id () != WHERE)
    2630              :     {
    2631              :       // where clause doesn't exist, so create empty one
    2632       220192 :       return AST::WhereClause::create_empty ();
    2633              :     }
    2634              : 
    2635         6952 :   lexer.skip_token ();
    2636              : 
    2637              :   /* parse where clause items - this is not a separate rule in the reference
    2638              :    * so won't be here */
    2639         6952 :   std::vector<std::unique_ptr<AST::WhereClauseItem>> where_clause_items;
    2640              : 
    2641         6952 :   std::vector<AST::LifetimeParam> for_lifetimes;
    2642        13904 :   if (lexer.peek_token ()->get_id () == FOR)
    2643            1 :     for_lifetimes = parse_for_lifetimes ();
    2644              : 
    2645              :   /* HACK: where clauses end with a right curly or semicolon or equals in all
    2646              :    * uses currently */
    2647         6952 :   const_TokenPtr t = lexer.peek_token ();
    2648        16229 :   while (t->get_id () != LEFT_CURLY && t->get_id () != SEMICOLON
    2649        14851 :          && t->get_id () != EQUAL)
    2650              :     {
    2651         9277 :       std::unique_ptr<AST::WhereClauseItem> where_clause_item
    2652              :         = parse_where_clause_item (for_lifetimes);
    2653              : 
    2654         9277 :       if (where_clause_item == nullptr)
    2655              :         {
    2656            0 :           Error error (t->get_locus (), "failed to parse where clause item");
    2657            0 :           add_error (std::move (error));
    2658              : 
    2659            0 :           return AST::WhereClause::create_empty ();
    2660            0 :         }
    2661              : 
    2662         9277 :       where_clause_items.push_back (std::move (where_clause_item));
    2663              : 
    2664              :       // also skip comma if it exists
    2665        18554 :       if (lexer.peek_token ()->get_id () != COMMA)
    2666              :         break;
    2667              : 
    2668         7899 :       lexer.skip_token ();
    2669         7899 :       t = lexer.peek_token ();
    2670              :     }
    2671              : 
    2672         6952 :   where_clause_items.shrink_to_fit ();
    2673         6952 :   return AST::WhereClause (std::move (where_clause_items));
    2674         6952 : }
    2675              : 
    2676              : /* Parses a where clause item (lifetime or type bound). Does not parse any
    2677              :  * commas. */
    2678              : template <typename ManagedTokenSource>
    2679              : std::unique_ptr<AST::WhereClauseItem>
    2680         9277 : Parser<ManagedTokenSource>::parse_where_clause_item (
    2681              :   const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
    2682              : {
    2683              :   // shitty cheat way of determining lifetime or type bound - test for
    2684              :   // lifetime
    2685         9277 :   const_TokenPtr t = lexer.peek_token ();
    2686              : 
    2687         9277 :   if (t->get_id () == LIFETIME)
    2688           10 :     return parse_lifetime_where_clause_item ();
    2689              :   else
    2690         9267 :     return parse_type_bound_where_clause_item (outer_for_lifetimes);
    2691         9277 : }
    2692              : 
    2693              : // Parses a lifetime where clause item.
    2694              : template <typename ManagedTokenSource>
    2695              : std::unique_ptr<AST::LifetimeWhereClauseItem>
    2696           10 : Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
    2697              : {
    2698           10 :   auto parsed_lifetime = parse_lifetime (false);
    2699           10 :   if (!parsed_lifetime)
    2700              :     {
    2701              :       // TODO: error here?
    2702            0 :       return nullptr;
    2703              :     }
    2704           10 :   auto lifetime = parsed_lifetime.value ();
    2705              : 
    2706           10 :   if (!skip_token (COLON))
    2707              :     {
    2708              :       // TODO: skip after somewhere
    2709            0 :       return nullptr;
    2710              :     }
    2711              : 
    2712           10 :   std::vector<AST::Lifetime> lifetime_bounds = parse_lifetime_bounds ();
    2713              :   // TODO: have end token passed in?
    2714              : 
    2715           10 :   location_t locus = lifetime.get_locus ();
    2716              : 
    2717              :   return std::unique_ptr<AST::LifetimeWhereClauseItem> (
    2718           10 :     new AST::LifetimeWhereClauseItem (std::move (lifetime),
    2719           10 :                                       std::move (lifetime_bounds), locus));
    2720           20 : }
    2721              : 
    2722              : // Parses a type bound where clause item.
    2723              : template <typename ManagedTokenSource>
    2724              : std::unique_ptr<AST::TypeBoundWhereClauseItem>
    2725         9267 : Parser<ManagedTokenSource>::parse_type_bound_where_clause_item (
    2726              :   const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
    2727              : {
    2728         9267 :   std::vector<AST::LifetimeParam> for_lifetimes = outer_for_lifetimes;
    2729              : 
    2730         9267 :   std::unique_ptr<AST::Type> type = parse_type ();
    2731         9267 :   if (type == nullptr)
    2732              :     {
    2733            0 :       return nullptr;
    2734              :     }
    2735              : 
    2736         9267 :   if (!skip_token (COLON))
    2737              :     {
    2738              :       // TODO: skip after somewhere
    2739            0 :       return nullptr;
    2740              :     }
    2741              : 
    2742        18534 :   if (lexer.peek_token ()->get_id () == FOR)
    2743              :     {
    2744           16 :       auto for_lifetimes_inner = parse_for_lifetimes ();
    2745           16 :       for_lifetimes.insert (for_lifetimes.end (), for_lifetimes_inner.begin (),
    2746              :                             for_lifetimes_inner.end ());
    2747           16 :     }
    2748              : 
    2749              :   // parse type param bounds if they exist
    2750         9267 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds
    2751              :     = parse_type_param_bounds ();
    2752              : 
    2753         9267 :   location_t locus = lexer.peek_token ()->get_locus ();
    2754              : 
    2755              :   return std::unique_ptr<AST::TypeBoundWhereClauseItem> (
    2756         9267 :     new AST::TypeBoundWhereClauseItem (std::move (for_lifetimes),
    2757              :                                        std::move (type),
    2758         9267 :                                        std::move (type_param_bounds), locus));
    2759         9267 : }
    2760              : 
    2761              : // Parses a for lifetimes clause, including the for keyword and angle
    2762              : // brackets.
    2763              : template <typename ManagedTokenSource>
    2764              : std::vector<AST::LifetimeParam>
    2765           36 : Parser<ManagedTokenSource>::parse_for_lifetimes ()
    2766              : {
    2767           36 :   std::vector<AST::LifetimeParam> params;
    2768              : 
    2769           36 :   if (!skip_token (FOR))
    2770              :     {
    2771              :       // skip after somewhere?
    2772              :       return params;
    2773              :     }
    2774              : 
    2775           36 :   if (!skip_token (LEFT_ANGLE))
    2776              :     {
    2777              :       // skip after somewhere?
    2778              :       return params;
    2779              :     }
    2780              : 
    2781              :   /* cannot specify end token due to parsing problems with '>' tokens being
    2782              :    * nested */
    2783           36 :   params = parse_lifetime_params_objs (Parse::Utils::is_right_angle_tok);
    2784              : 
    2785           36 :   if (!skip_generics_right_angle ())
    2786              :     {
    2787              :       // DEBUG
    2788            0 :       rust_debug ("failed to skip generics right angle after (supposedly) "
    2789              :                   "finished parsing where clause items");
    2790              :       // ok, well this gets called.
    2791              : 
    2792              :       // skip after somewhere?
    2793              :       return params;
    2794              :     }
    2795              : 
    2796              :   return params;
    2797              : }
    2798              : 
    2799              : // Parses type parameter bounds in where clause or generic arguments.
    2800              : template <typename ManagedTokenSource>
    2801              : std::vector<std::unique_ptr<AST::TypeParamBound>>
    2802        25134 : Parser<ManagedTokenSource>::parse_type_param_bounds ()
    2803              : {
    2804        25134 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    2805              : 
    2806        25134 :   std::unique_ptr<AST::TypeParamBound> initial_bound
    2807              :     = parse_type_param_bound ();
    2808              : 
    2809              :   // quick exit if null
    2810        25134 :   if (initial_bound == nullptr)
    2811              :     {
    2812              :       /* error? type param bounds must have at least one term, but are bounds
    2813              :        * optional? */
    2814              :       return type_param_bounds;
    2815              :     }
    2816        25134 :   type_param_bounds.push_back (std::move (initial_bound));
    2817              : 
    2818        53888 :   while (lexer.peek_token ()->get_id () == PLUS)
    2819              :     {
    2820         1810 :       lexer.skip_token ();
    2821              : 
    2822         1810 :       std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
    2823         1810 :       if (bound == nullptr)
    2824              :         {
    2825              :           /* not an error: bound is allowed to be null as trailing plus is
    2826              :            * allowed */
    2827              :           return type_param_bounds;
    2828              :         }
    2829              : 
    2830         1810 :       type_param_bounds.push_back (std::move (bound));
    2831              :     }
    2832              : 
    2833        25134 :   type_param_bounds.shrink_to_fit ();
    2834              :   return type_param_bounds;
    2835        25134 : }
    2836              : 
    2837              : /* Parses type parameter bounds in where clause or generic arguments, with end
    2838              :  * token handling. */
    2839              : template <typename ManagedTokenSource>
    2840              : template <typename EndTokenPred>
    2841              : std::vector<std::unique_ptr<AST::TypeParamBound>>
    2842         1299 : Parser<ManagedTokenSource>::parse_type_param_bounds (EndTokenPred is_end_token)
    2843              : {
    2844         1299 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    2845              : 
    2846         1299 :   std::unique_ptr<AST::TypeParamBound> initial_bound
    2847              :     = parse_type_param_bound ();
    2848              : 
    2849              :   // quick exit if null
    2850         1299 :   if (initial_bound == nullptr)
    2851              :     {
    2852              :       /* error? type param bounds must have at least one term, but are bounds
    2853              :        * optional? */
    2854            0 :       return type_param_bounds;
    2855              :     }
    2856         1299 :   type_param_bounds.push_back (std::move (initial_bound));
    2857              : 
    2858         3200 :   while (lexer.peek_token ()->get_id () == PLUS)
    2859              :     {
    2860          301 :       lexer.skip_token ();
    2861              : 
    2862              :       // break if end token character
    2863          602 :       if (is_end_token (lexer.peek_token ()->get_id ()))
    2864              :         break;
    2865              : 
    2866          301 :       std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
    2867          301 :       if (bound == nullptr)
    2868              :         {
    2869              :           // TODO how wise is it to ditch all bounds if only one failed?
    2870            0 :           Error error (lexer.peek_token ()->get_locus (),
    2871              :                        "failed to parse type param bound in type param bounds");
    2872            0 :           add_error (std::move (error));
    2873              : 
    2874            0 :           return {};
    2875            0 :         }
    2876              : 
    2877          301 :       type_param_bounds.push_back (std::move (bound));
    2878              :     }
    2879              : 
    2880         1299 :   type_param_bounds.shrink_to_fit ();
    2881         1299 :   return type_param_bounds;
    2882         1299 : }
    2883              : 
    2884              : /* Parses a single type parameter bound in a where clause or generic argument.
    2885              :  * Does not parse the '+' between arguments. */
    2886              : template <typename ManagedTokenSource>
    2887              : std::unique_ptr<AST::TypeParamBound>
    2888        28940 : Parser<ManagedTokenSource>::parse_type_param_bound ()
    2889              : {
    2890              :   // shitty cheat way of determining lifetime or trait bound - test for
    2891              :   // lifetime
    2892        28940 :   const_TokenPtr t = lexer.peek_token ();
    2893        28940 :   switch (t->get_id ())
    2894              :     {
    2895         1000 :     case LIFETIME:
    2896         1000 :       return std::unique_ptr<AST::Lifetime> (
    2897         2000 :         new AST::Lifetime (parse_lifetime (false).value ()));
    2898        27940 :     case LEFT_PAREN:
    2899              :     case QUESTION_MARK:
    2900              :     case FOR:
    2901              :     case IDENTIFIER:
    2902              :     case SUPER:
    2903              :     case SELF:
    2904              :     case SELF_ALIAS:
    2905              :     case CRATE:
    2906              :     case DOLLAR_SIGN:
    2907              :     case SCOPE_RESOLUTION:
    2908        27940 :       return parse_trait_bound ();
    2909            0 :     default:
    2910              :       // don't error - assume this is fine TODO
    2911            0 :       return nullptr;
    2912              :     }
    2913        28940 : }
    2914              : 
    2915              : // Parses a trait bound type param bound.
    2916              : template <typename ManagedTokenSource>
    2917              : std::unique_ptr<AST::TraitBound>
    2918        29589 : Parser<ManagedTokenSource>::parse_trait_bound ()
    2919              : {
    2920        29589 :   bool has_parens = false;
    2921        29589 :   bool has_question_mark = false;
    2922              : 
    2923        29589 :   location_t locus = lexer.peek_token ()->get_locus ();
    2924              : 
    2925              :   /* parse optional `for lifetimes`. */
    2926        29589 :   std::vector<AST::LifetimeParam> for_lifetimes;
    2927        59178 :   if (lexer.peek_token ()->get_id () == FOR)
    2928            7 :     for_lifetimes = parse_for_lifetimes ();
    2929              : 
    2930              :   // handle trait bound being in parentheses
    2931        59178 :   if (lexer.peek_token ()->get_id () == LEFT_PAREN)
    2932              :     {
    2933            0 :       has_parens = true;
    2934            0 :       lexer.skip_token ();
    2935              :     }
    2936              : 
    2937              :   // handle having question mark (optional)
    2938        59178 :   if (lexer.peek_token ()->get_id () == QUESTION_MARK)
    2939              :     {
    2940         3387 :       has_question_mark = true;
    2941         3387 :       lexer.skip_token ();
    2942              :     }
    2943              : 
    2944              :   // handle TypePath
    2945        29589 :   AST::TypePath type_path = parse_type_path ();
    2946        29589 :   if (type_path.is_error ())
    2947            2 :     return nullptr;
    2948              : 
    2949              :   // handle closing parentheses
    2950        29587 :   if (has_parens)
    2951              :     {
    2952            0 :       if (!skip_token (RIGHT_PAREN))
    2953              :         {
    2954            0 :           return nullptr;
    2955              :         }
    2956              :     }
    2957              : 
    2958              :   return std::unique_ptr<AST::TraitBound> (
    2959        29587 :     new AST::TraitBound (std::move (type_path), locus, has_parens,
    2960        29587 :                          has_question_mark, std::move (for_lifetimes)));
    2961        29589 : }
    2962              : 
    2963              : // Parses lifetime bounds.
    2964              : template <typename ManagedTokenSource>
    2965              : std::vector<AST::Lifetime>
    2966           10 : Parser<ManagedTokenSource>::parse_lifetime_bounds ()
    2967              : {
    2968           10 :   std::vector<AST::Lifetime> lifetime_bounds;
    2969              : 
    2970           10 :   while (true)
    2971              :     {
    2972           10 :       auto lifetime = parse_lifetime (false);
    2973              : 
    2974              :       // quick exit for parsing failure
    2975           10 :       if (!lifetime)
    2976              :         break;
    2977              : 
    2978           10 :       lifetime_bounds.push_back (std::move (lifetime.value ()));
    2979              : 
    2980              :       /* plus is maybe not allowed at end - spec defines it weirdly, so
    2981              :        * assuming allowed at end */
    2982           20 :       if (lexer.peek_token ()->get_id () != PLUS)
    2983              :         break;
    2984              : 
    2985            0 :       lexer.skip_token ();
    2986              :     }
    2987              : 
    2988           10 :   lifetime_bounds.shrink_to_fit ();
    2989           10 :   return lifetime_bounds;
    2990              : }
    2991              : 
    2992              : // Parses lifetime bounds, with added check for ending token.
    2993              : template <typename ManagedTokenSource>
    2994              : template <typename EndTokenPred>
    2995              : std::vector<AST::Lifetime>
    2996          151 : Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
    2997              : {
    2998          151 :   std::vector<AST::Lifetime> lifetime_bounds;
    2999              : 
    3000          628 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3001              :     {
    3002          159 :       auto lifetime = parse_lifetime (false);
    3003              : 
    3004          159 :       if (!lifetime)
    3005              :         {
    3006              :           /* TODO: is it worth throwing away all lifetime bound info just
    3007              :            * because one failed? */
    3008            0 :           Error error (lexer.peek_token ()->get_locus (),
    3009              :                        "failed to parse lifetime in lifetime bounds");
    3010            0 :           add_error (std::move (error));
    3011              : 
    3012            0 :           return {};
    3013            0 :         }
    3014              : 
    3015          159 :       lifetime_bounds.push_back (std::move (lifetime.value ()));
    3016              : 
    3017              :       /* plus is maybe not allowed at end - spec defines it weirdly, so
    3018              :        * assuming allowed at end */
    3019          318 :       if (lexer.peek_token ()->get_id () != PLUS)
    3020              :         break;
    3021              : 
    3022            8 :       lexer.skip_token ();
    3023              :     }
    3024              : 
    3025          151 :   lifetime_bounds.shrink_to_fit ();
    3026          151 :   return lifetime_bounds;
    3027          151 : }
    3028              : 
    3029              : /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
    3030              :  * existing. */
    3031              : template <typename ManagedTokenSource>
    3032              : tl::expected<AST::Lifetime, Parse::Error::Lifetime>
    3033       158230 : Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
    3034              : {
    3035       158230 :   const_TokenPtr lifetime_tok = lexer.peek_token ();
    3036       158230 :   if (lifetime_tok->get_id () != LIFETIME)
    3037              :     {
    3038       124639 :       if (allow_elided)
    3039              :         {
    3040            0 :           return AST::Lifetime::elided ();
    3041              :         }
    3042              :       else
    3043              :         {
    3044       124639 :           return tl::make_unexpected<Parse::Error::Lifetime> ({});
    3045              :         }
    3046              :     }
    3047        33591 :   lexer.skip_token ();
    3048              : 
    3049        67182 :   return lifetime_from_token (lifetime_tok);
    3050       158230 : }
    3051              : 
    3052              : template <typename ManagedTokenSource>
    3053              : AST::Lifetime
    3054        33666 : Parser<ManagedTokenSource>::lifetime_from_token (const_TokenPtr tok)
    3055              : {
    3056        33666 :   location_t locus = tok->get_locus ();
    3057        33666 :   std::string lifetime_ident = tok->get_str ();
    3058              : 
    3059        33666 :   if (lifetime_ident == "static")
    3060              :     {
    3061          413 :       return AST::Lifetime (AST::Lifetime::STATIC, "", locus);
    3062              :     }
    3063        33253 :   else if (lifetime_ident == "_")
    3064              :     {
    3065              :       // Explicitly and implicitly elided lifetimes follow the same rules.
    3066         6225 :       return AST::Lifetime (AST::Lifetime::WILDCARD, "", locus);
    3067              :     }
    3068              :   else
    3069              :     {
    3070        54056 :       return AST::Lifetime (AST::Lifetime::NAMED, std::move (lifetime_ident),
    3071        27028 :                             locus);
    3072              :     }
    3073        33666 : }
    3074              : 
    3075              : template <typename ManagedTokenSource>
    3076              : std::unique_ptr<AST::ExternalTypeItem>
    3077           15 : Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
    3078              :                                                       AST::AttrVec outer_attrs)
    3079              : {
    3080           15 :   location_t locus = lexer.peek_token ()->get_locus ();
    3081           15 :   skip_token (TYPE);
    3082              : 
    3083           15 :   const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
    3084           15 :   if (alias_name_tok == nullptr)
    3085              :     {
    3086            0 :       Error error (lexer.peek_token ()->get_locus (),
    3087              :                    "could not parse identifier in external opaque type");
    3088            0 :       add_error (std::move (error));
    3089              : 
    3090            0 :       skip_after_semicolon ();
    3091            0 :       return nullptr;
    3092            0 :     }
    3093              : 
    3094           15 :   if (!skip_token (SEMICOLON))
    3095            1 :     return nullptr;
    3096              : 
    3097              :   return std::unique_ptr<AST::ExternalTypeItem> (
    3098           42 :     new AST::ExternalTypeItem (alias_name_tok->get_str (), std::move (vis),
    3099           14 :                                std::move (outer_attrs), std::move (locus)));
    3100           15 : }
    3101              : 
    3102              : // Parses a "type alias" (typedef) item.
    3103              : template <typename ManagedTokenSource>
    3104              : std::unique_ptr<AST::TypeAlias>
    3105        22956 : Parser<ManagedTokenSource>::parse_type_alias (AST::Visibility vis,
    3106              :                                               AST::AttrVec outer_attrs)
    3107              : {
    3108        22956 :   location_t locus = lexer.peek_token ()->get_locus ();
    3109        22956 :   skip_token (TYPE);
    3110              : 
    3111              :   // TODO: use this token for identifier when finished that
    3112        22956 :   const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
    3113        22956 :   if (alias_name_tok == nullptr)
    3114              :     {
    3115            0 :       Error error (lexer.peek_token ()->get_locus (),
    3116              :                    "could not parse identifier in type alias");
    3117            0 :       add_error (std::move (error));
    3118              : 
    3119            0 :       skip_after_semicolon ();
    3120            0 :       return nullptr;
    3121            0 :     }
    3122        45912 :   Identifier alias_name{alias_name_tok};
    3123              : 
    3124              :   // parse generic params, which may not exist
    3125        22956 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3126              :     = parse_generic_params_in_angles ();
    3127              : 
    3128              :   // parse where clause, which may not exist
    3129        22956 :   AST::WhereClause where_clause = parse_where_clause ();
    3130              : 
    3131        22956 :   if (!skip_token (EQUAL))
    3132              :     {
    3133            0 :       skip_after_semicolon ();
    3134            0 :       return nullptr;
    3135              :     }
    3136              : 
    3137        22956 :   std::unique_ptr<AST::Type> type_to_alias = parse_type ();
    3138              : 
    3139        22956 :   if (!skip_token (SEMICOLON))
    3140              :     {
    3141              :       // should be skipping past this, not the next line
    3142            0 :       return nullptr;
    3143              :     }
    3144              : 
    3145              :   return std::unique_ptr<AST::TypeAlias> (
    3146        22956 :     new AST::TypeAlias (std::move (alias_name), std::move (generic_params),
    3147              :                         std::move (where_clause), std::move (type_to_alias),
    3148        22956 :                         std::move (vis), std::move (outer_attrs), locus));
    3149        45912 : }
    3150              : 
    3151              : // Parse a struct item AST node.
    3152              : template <typename ManagedTokenSource>
    3153              : std::unique_ptr<AST::Struct>
    3154         8469 : Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
    3155              :                                           AST::AttrVec outer_attrs)
    3156              : {
    3157              :   /* TODO: determine best way to parse the proper struct vs tuple struct -
    3158              :    * share most of initial constructs so lookahead might be impossible, and if
    3159              :    * not probably too expensive. Best way is probably unified parsing for the
    3160              :    * initial parts and then pass them in as params to more derived functions.
    3161              :    * Alternatively, just parse everything in this one function - do this if
    3162              :    * function not too long. */
    3163              : 
    3164              :   /* Proper struct <- 'struct' IDENTIFIER generic_params? where_clause? ( '{'
    3165              :    * struct_fields? '}' | ';' ) */
    3166              :   /* Tuple struct <- 'struct' IDENTIFIER generic_params? '(' tuple_fields? ')'
    3167              :    * where_clause? ';' */
    3168         8469 :   location_t locus = lexer.peek_token ()->get_locus ();
    3169         8469 :   skip_token (STRUCT_KW);
    3170              : 
    3171              :   // parse struct name
    3172         8469 :   const_TokenPtr name_tok = expect_token (IDENTIFIER);
    3173         8469 :   if (name_tok == nullptr)
    3174              :     {
    3175              :       // skip after somewhere?
    3176            1 :       return nullptr;
    3177              :     }
    3178        16936 :   Identifier struct_name{name_tok};
    3179              : 
    3180              :   // parse generic params, which may or may not exist
    3181         8468 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3182              :     = parse_generic_params_in_angles ();
    3183              : 
    3184              :   // branch on next token - determines whether proper struct or tuple struct
    3185        16936 :   if (lexer.peek_token ()->get_id () == LEFT_PAREN)
    3186              :     {
    3187              :       // tuple struct
    3188              : 
    3189              :       // skip left parenthesis
    3190         1948 :       lexer.skip_token ();
    3191              : 
    3192              :       // parse tuple fields
    3193         1948 :       std::vector<AST::TupleField> tuple_fields;
    3194              :       // Might be empty tuple for unit tuple struct.
    3195         3896 :       if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    3196           24 :         tuple_fields = std::vector<AST::TupleField> ();
    3197              :       else
    3198         1924 :         tuple_fields = parse_tuple_fields ();
    3199              : 
    3200              :       // tuple parameters must have closing parenthesis
    3201         1948 :       if (!skip_token (RIGHT_PAREN))
    3202              :         {
    3203            1 :           skip_after_semicolon ();
    3204            1 :           return nullptr;
    3205              :         }
    3206              : 
    3207              :       // parse where clause, which is optional
    3208         1947 :       AST::WhereClause where_clause = parse_where_clause ();
    3209              : 
    3210         1947 :       if (!skip_token (SEMICOLON))
    3211              :         {
    3212              :           // can't skip after semicolon because it's meant to be here
    3213            0 :           return nullptr;
    3214              :         }
    3215              : 
    3216         1947 :       return std::unique_ptr<AST::TupleStruct> (
    3217         1947 :         new AST::TupleStruct (std::move (tuple_fields), std::move (struct_name),
    3218              :                               std::move (generic_params),
    3219              :                               std::move (where_clause), std::move (vis),
    3220         1947 :                               std::move (outer_attrs), locus));
    3221         1948 :     }
    3222              : 
    3223              :   // assume it is a proper struct being parsed and continue outside of switch
    3224              :   // - label only here to suppress warning
    3225              : 
    3226              :   // parse where clause, which is optional
    3227         6520 :   AST::WhereClause where_clause = parse_where_clause ();
    3228              : 
    3229              :   // branch on next token - determines whether struct is a unit struct
    3230         6520 :   const_TokenPtr t = lexer.peek_token ();
    3231         6520 :   switch (t->get_id ())
    3232              :     {
    3233         2675 :     case LEFT_CURLY:
    3234              :       {
    3235              :         // struct with body
    3236              : 
    3237              :         // skip curly bracket
    3238         2675 :         lexer.skip_token ();
    3239              : 
    3240              :         // parse struct fields, if any
    3241         2675 :         std::vector<AST::StructField> struct_fields
    3242              :           = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    3243              : 
    3244         2675 :         if (!skip_token (RIGHT_CURLY))
    3245              :           {
    3246              :             // skip somewhere?
    3247            0 :             return nullptr;
    3248              :           }
    3249              : 
    3250         2675 :         return std::unique_ptr<AST::StructStruct> (new AST::StructStruct (
    3251              :           std::move (struct_fields), std::move (struct_name),
    3252              :           std::move (generic_params), std::move (where_clause), false,
    3253         2675 :           std::move (vis), std::move (outer_attrs), locus));
    3254         2675 :       }
    3255         3844 :     case SEMICOLON:
    3256              :       // unit struct declaration
    3257              : 
    3258         3844 :       lexer.skip_token ();
    3259              : 
    3260         3844 :       return std::unique_ptr<AST::StructStruct> (
    3261         7688 :         new AST::StructStruct (std::move (struct_name),
    3262              :                                std::move (generic_params),
    3263              :                                std::move (where_clause), std::move (vis),
    3264         3844 :                                std::move (outer_attrs), locus));
    3265            1 :     default:
    3266            1 :       add_error (Error (t->get_locus (),
    3267              :                         "unexpected token %qs in struct declaration",
    3268              :                         t->get_token_description ()));
    3269              : 
    3270              :       // skip somewhere?
    3271            1 :       return nullptr;
    3272              :     }
    3273         8468 : }
    3274              : 
    3275              : // Parses struct fields in struct declarations.
    3276              : template <typename ManagedTokenSource>
    3277              : std::vector<AST::StructField>
    3278            0 : Parser<ManagedTokenSource>::parse_struct_fields ()
    3279              : {
    3280            0 :   std::vector<AST::StructField> fields;
    3281              : 
    3282            0 :   AST::StructField initial_field = parse_struct_field ();
    3283              : 
    3284              :   // Return empty field list if no field there
    3285            0 :   if (initial_field.is_error ())
    3286              :     return fields;
    3287              : 
    3288            0 :   fields.push_back (std::move (initial_field));
    3289              : 
    3290            0 :   while (lexer.peek_token ()->get_id () == COMMA)
    3291              :     {
    3292            0 :       lexer.skip_token ();
    3293              : 
    3294            0 :       AST::StructField field = parse_struct_field ();
    3295              : 
    3296            0 :       if (field.is_error ())
    3297              :         {
    3298              :           // would occur with trailing comma, so allowed
    3299              :           break;
    3300              :         }
    3301              : 
    3302            0 :       fields.push_back (std::move (field));
    3303              :     }
    3304              : 
    3305            0 :   fields.shrink_to_fit ();
    3306              :   return fields;
    3307              :   // TODO: template if possible (parse_non_ptr_seq)
    3308            0 : }
    3309              : 
    3310              : // Parses struct fields in struct declarations.
    3311              : template <typename ManagedTokenSource>
    3312              : template <typename EndTokenPred>
    3313              : std::vector<AST::StructField>
    3314         2909 : Parser<ManagedTokenSource>::parse_struct_fields (EndTokenPred is_end_tok)
    3315              : {
    3316         2909 :   std::vector<AST::StructField> fields;
    3317              : 
    3318         2909 :   AST::StructField initial_field = parse_struct_field ();
    3319              : 
    3320              :   // Return empty field list if no field there
    3321         2909 :   if (initial_field.is_error ())
    3322           60 :     return fields;
    3323              : 
    3324         2849 :   fields.push_back (std::move (initial_field));
    3325              : 
    3326        11420 :   while (lexer.peek_token ()->get_id () == COMMA)
    3327              :     {
    3328         5498 :       lexer.skip_token ();
    3329              : 
    3330        10996 :       if (is_end_tok (lexer.peek_token ()->get_id ()))
    3331              :         break;
    3332              : 
    3333         2861 :       AST::StructField field = parse_struct_field ();
    3334         2861 :       if (field.is_error ())
    3335              :         {
    3336              :           /* TODO: should every field be ditched just because one couldn't be
    3337              :            * parsed? */
    3338            0 :           Error error (lexer.peek_token ()->get_locus (),
    3339              :                        "failed to parse struct field in struct fields");
    3340            0 :           add_error (std::move (error));
    3341              : 
    3342            0 :           return {};
    3343            0 :         }
    3344              : 
    3345         2861 :       fields.push_back (std::move (field));
    3346              :     }
    3347              : 
    3348         2849 :   fields.shrink_to_fit ();
    3349         2849 :   return fields;
    3350              :   // TODO: template if possible (parse_non_ptr_seq)
    3351         2909 : }
    3352              : 
    3353              : // Parses a single struct field (in a struct definition). Does not parse
    3354              : // commas.
    3355              : template <typename ManagedTokenSource>
    3356              : AST::StructField
    3357         5770 : Parser<ManagedTokenSource>::parse_struct_field ()
    3358              : {
    3359              :   // parse outer attributes, if they exist
    3360         5770 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    3361              : 
    3362              :   // parse visibility, if it exists
    3363         5770 :   auto vis = parse_visibility ();
    3364         5770 :   if (!vis)
    3365            0 :     return AST::StructField::create_error ();
    3366              : 
    3367         5770 :   location_t locus = lexer.peek_token ()->get_locus ();
    3368              : 
    3369              :   // parse field name
    3370         5770 :   const_TokenPtr field_name_tok = lexer.peek_token ();
    3371         5770 :   if (field_name_tok->get_id () != IDENTIFIER)
    3372              :     {
    3373              :       // if not identifier, assumes there is no struct field and exits - not
    3374              :       // necessarily error
    3375           60 :       return AST::StructField::create_error ();
    3376              :     }
    3377        11420 :   Identifier field_name{field_name_tok};
    3378         5710 :   lexer.skip_token ();
    3379              : 
    3380         5710 :   if (!skip_token (COLON))
    3381              :     {
    3382              :       // skip after somewhere?
    3383            0 :       return AST::StructField::create_error ();
    3384              :     }
    3385              : 
    3386              :   // parse field type - this is required
    3387         5710 :   std::unique_ptr<AST::Type> field_type = parse_type ();
    3388         5710 :   if (field_type == nullptr)
    3389              :     {
    3390            0 :       Error error (lexer.peek_token ()->get_locus (),
    3391              :                    "could not parse type in struct field definition");
    3392            0 :       add_error (std::move (error));
    3393              : 
    3394              :       // skip after somewhere
    3395            0 :       return AST::StructField::create_error ();
    3396            0 :     }
    3397              : 
    3398        11420 :   return AST::StructField (std::move (field_name), std::move (field_type),
    3399         5710 :                            std::move (vis.value ()), locus,
    3400         5710 :                            std::move (outer_attrs));
    3401        22960 : }
    3402              : 
    3403              : // Parses tuple fields in tuple/tuple struct declarations.
    3404              : template <typename ManagedTokenSource>
    3405              : std::vector<AST::TupleField>
    3406         2670 : Parser<ManagedTokenSource>::parse_tuple_fields ()
    3407              : {
    3408         2670 :   std::vector<AST::TupleField> fields;
    3409              : 
    3410         2670 :   AST::TupleField initial_field = parse_tuple_field ();
    3411              : 
    3412              :   // Return empty field list if no field there
    3413         2670 :   if (initial_field.is_error ())
    3414              :     {
    3415            1 :       return fields;
    3416              :     }
    3417              : 
    3418         2669 :   fields.push_back (std::move (initial_field));
    3419              : 
    3420              :   // maybe think of a better control structure here - do-while with an initial
    3421              :   // error state? basically, loop through field list until can't find any more
    3422              :   // params HACK: all current syntax uses of tuple fields have them ending
    3423              :   // with a right paren token
    3424         2669 :   const_TokenPtr t = lexer.peek_token ();
    3425         6463 :   while (t->get_id () == COMMA)
    3426              :     {
    3427              :       // skip comma if applies - e.g. trailing comma
    3428         3804 :       lexer.skip_token ();
    3429              : 
    3430              :       // break out due to right paren if it exists
    3431         7608 :       if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    3432              :         {
    3433              :           break;
    3434              :         }
    3435              : 
    3436         3794 :       AST::TupleField field = parse_tuple_field ();
    3437         3794 :       if (field.is_error ())
    3438              :         {
    3439            0 :           Error error (lexer.peek_token ()->get_locus (),
    3440              :                        "failed to parse tuple field in tuple fields");
    3441            0 :           add_error (std::move (error));
    3442              : 
    3443            0 :           return std::vector<AST::TupleField> ();
    3444            0 :         }
    3445              : 
    3446         3794 :       fields.push_back (std::move (field));
    3447              : 
    3448         3794 :       t = lexer.peek_token ();
    3449              :     }
    3450              : 
    3451         2669 :   fields.shrink_to_fit ();
    3452         2669 :   return fields;
    3453              : 
    3454              :   // TODO: this shares basically all code with function params and struct
    3455              :   // fields
    3456              :   // - templates?
    3457         2670 : }
    3458              : 
    3459              : /* Parses a single tuple struct field in a tuple struct definition. Does not
    3460              :  * parse commas. */
    3461              : template <typename ManagedTokenSource>
    3462              : AST::TupleField
    3463         6464 : Parser<ManagedTokenSource>::parse_tuple_field ()
    3464              : {
    3465              :   // parse outer attributes if they exist
    3466         6464 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    3467              : 
    3468              :   // parse visibility if it exists
    3469         6464 :   auto visibility = parse_visibility ();
    3470         6464 :   if (!visibility)
    3471            0 :     return AST::TupleField::create_error ();
    3472              : 
    3473         6464 :   location_t locus = lexer.peek_token ()->get_locus ();
    3474              : 
    3475              :   // parse type, which is required
    3476         6464 :   std::unique_ptr<AST::Type> field_type = parse_type ();
    3477         6464 :   if (field_type == nullptr)
    3478              :     {
    3479              :       // error if null
    3480            1 :       Error error (lexer.peek_token ()->get_locus (),
    3481              :                    "could not parse type in tuple struct field");
    3482            1 :       add_error (std::move (error));
    3483              : 
    3484              :       // skip after something
    3485            1 :       return AST::TupleField::create_error ();
    3486            1 :     }
    3487              : 
    3488         6463 :   return AST::TupleField (std::move (field_type),
    3489         6463 :                           std::move (visibility.value ()), locus,
    3490         6463 :                           std::move (outer_attrs));
    3491        12928 : }
    3492              : 
    3493              : // Parses a Rust "enum" tagged union item definition.
    3494              : template <typename ManagedTokenSource>
    3495              : std::unique_ptr<AST::Enum>
    3496          926 : Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis,
    3497              :                                         AST::AttrVec outer_attrs)
    3498              : {
    3499          926 :   location_t locus = lexer.peek_token ()->get_locus ();
    3500          926 :   skip_token (ENUM_KW);
    3501              : 
    3502              :   // parse enum name
    3503          926 :   const_TokenPtr enum_name_tok = expect_token (IDENTIFIER);
    3504          926 :   if (enum_name_tok == nullptr)
    3505            1 :     return nullptr;
    3506              : 
    3507         1850 :   Identifier enum_name = {enum_name_tok};
    3508              : 
    3509              :   // parse generic params (of enum container, not enum variants) if they exist
    3510          925 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3511              :     = parse_generic_params_in_angles ();
    3512              : 
    3513              :   // parse where clause if it exists
    3514          925 :   AST::WhereClause where_clause = parse_where_clause ();
    3515              : 
    3516          925 :   if (!skip_token (LEFT_CURLY))
    3517              :     {
    3518            0 :       skip_after_end_block ();
    3519            0 :       return nullptr;
    3520              :     }
    3521              : 
    3522              :   // parse actual enum variant definitions
    3523          925 :   std::vector<std::unique_ptr<AST::EnumItem>> enum_items
    3524              :     = parse_enum_items ([] (TokenId id) { return id == RIGHT_CURLY; });
    3525              : 
    3526          925 :   if (!skip_token (RIGHT_CURLY))
    3527              :     {
    3528            1 :       skip_after_end_block ();
    3529            1 :       return nullptr;
    3530              :     }
    3531              : 
    3532              :   return std::unique_ptr<AST::Enum> (
    3533          924 :     new AST::Enum (std::move (enum_name), std::move (vis),
    3534              :                    std::move (generic_params), std::move (where_clause),
    3535          924 :                    std::move (enum_items), std::move (outer_attrs), locus));
    3536         1850 : }
    3537              : 
    3538              : // Parses the enum variants inside an enum definiton.
    3539              : template <typename ManagedTokenSource>
    3540              : std::vector<std::unique_ptr<AST::EnumItem>>
    3541            0 : Parser<ManagedTokenSource>::parse_enum_items ()
    3542              : {
    3543            0 :   std::vector<std::unique_ptr<AST::EnumItem>> items;
    3544              : 
    3545            0 :   auto initial_item = parse_enum_item ();
    3546              : 
    3547              :   // Return empty item list if no field there
    3548            0 :   if (!initial_item)
    3549              :     return items;
    3550              : 
    3551            0 :   items.push_back (std::move (initial_item.value ()));
    3552              : 
    3553            0 :   while (lexer.peek_token ()->get_id () == COMMA)
    3554              :     {
    3555            0 :       lexer.skip_token ();
    3556              : 
    3557            0 :       auto item = parse_enum_item ();
    3558            0 :       if (!item)
    3559              :         {
    3560              :           // this would occur with a trailing comma, which is allowed
    3561              :           break;
    3562              :         }
    3563              : 
    3564            0 :       items.push_back (std::move (item.value ()));
    3565              :     }
    3566              : 
    3567            0 :   items.shrink_to_fit ();
    3568              :   return items;
    3569              : 
    3570              :   /* TODO: use template if doable (parse_non_ptr_sequence) */
    3571            0 : }
    3572              : 
    3573              : // Parses the enum variants inside an enum definiton.
    3574              : template <typename ManagedTokenSource>
    3575              : template <typename EndTokenPred>
    3576              : std::vector<std::unique_ptr<AST::EnumItem>>
    3577          925 : Parser<ManagedTokenSource>::parse_enum_items (EndTokenPred is_end_tok)
    3578              : {
    3579          925 :   std::vector<std::unique_ptr<AST::EnumItem>> items;
    3580              : 
    3581          925 :   auto initial_item = parse_enum_item ();
    3582              : 
    3583              :   // Return empty item list if no field there
    3584          925 :   if (!initial_item)
    3585           70 :     return items;
    3586              : 
    3587          855 :   items.push_back (std::move (initial_item.value ()));
    3588              : 
    3589         4430 :   while (lexer.peek_token ()->get_id () == COMMA)
    3590              :     {
    3591         2185 :       lexer.skip_token ();
    3592              : 
    3593         4370 :       if (is_end_tok (lexer.peek_token ()->get_id ()))
    3594              :         break;
    3595              : 
    3596         1360 :       auto item = parse_enum_item ();
    3597         1360 :       if (!item)
    3598              :         {
    3599              :           /* TODO should this ignore all successfully parsed enum items just
    3600              :            * because one failed? */
    3601            0 :           Error error (lexer.peek_token ()->get_locus (),
    3602              :                        "failed to parse enum item in enum items");
    3603            0 :           add_error (std::move (error));
    3604              : 
    3605            0 :           return {};
    3606            0 :         }
    3607              : 
    3608         1360 :       items.push_back (std::move (item.value ()));
    3609              :     }
    3610              : 
    3611          855 :   items.shrink_to_fit ();
    3612          855 :   return items;
    3613              : 
    3614              :   /* TODO: use template if doable (parse_non_ptr_sequence) */
    3615          925 : }
    3616              : 
    3617              : /* Parses a single enum variant item in an enum definition. Does not parse
    3618              :  * commas. */
    3619              : template <typename ManagedTokenSource>
    3620              : tl::expected<std::unique_ptr<AST::EnumItem>, Parse::Error::EnumVariant>
    3621         2285 : Parser<ManagedTokenSource>::parse_enum_item ()
    3622              : {
    3623              :   // parse outer attributes if they exist
    3624         2285 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    3625              : 
    3626              :   // parse visibility, which may or may not exist
    3627         2285 :   auto vis_res = parse_visibility ();
    3628         2285 :   if (!vis_res)
    3629            0 :     return Parse::Error::EnumVariant::make_child_error ();
    3630         2285 :   auto vis = vis_res.value ();
    3631              : 
    3632              :   // parse name for enum item, which is required
    3633         2285 :   const_TokenPtr item_name_tok = lexer.peek_token ();
    3634         2285 :   if (item_name_tok->get_id () != IDENTIFIER)
    3635              :     {
    3636              :       // this may not be an error but it means there is no enum item here
    3637          140 :       return Parse::Error::EnumVariant::make_not_identifier (item_name_tok);
    3638              :     }
    3639         2215 :   lexer.skip_token ();
    3640         4430 :   Identifier item_name{item_name_tok};
    3641              : 
    3642              :   // branch based on next token
    3643         2215 :   const_TokenPtr t = lexer.peek_token ();
    3644         2215 :   switch (t->get_id ())
    3645              :     {
    3646          769 :     case LEFT_PAREN:
    3647              :       {
    3648              :         // tuple enum item
    3649          769 :         lexer.skip_token ();
    3650              : 
    3651          769 :         std::vector<AST::TupleField> tuple_fields;
    3652              :         // Might be empty tuple for unit tuple enum variant.
    3653         1538 :         if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    3654           23 :           tuple_fields = std::vector<AST::TupleField> ();
    3655              :         else
    3656          746 :           tuple_fields = parse_tuple_fields ();
    3657              : 
    3658          769 :         if (!skip_token (RIGHT_PAREN))
    3659              :           {
    3660              :             // skip after somewhere
    3661            0 :             return Parse::Error::EnumVariant::make_unfinished_tuple_variant ();
    3662              :           }
    3663              : 
    3664          769 :         return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
    3665              :           std::move (item_name), std::move (vis), std::move (tuple_fields),
    3666          769 :           std::move (outer_attrs), item_name_tok->get_locus ()));
    3667          769 :       }
    3668          103 :     case LEFT_CURLY:
    3669              :       {
    3670              :         // struct enum item
    3671          103 :         lexer.skip_token ();
    3672              : 
    3673          103 :         std::vector<AST::StructField> struct_fields
    3674              :           = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    3675              : 
    3676          103 :         if (!skip_token (RIGHT_CURLY))
    3677              :           {
    3678              :             // skip after somewhere
    3679            0 :             return Parse::Error::EnumVariant::make_unfinished_tuple_variant ();
    3680              :           }
    3681              : 
    3682          103 :         return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
    3683              :           std::move (item_name), std::move (vis), std::move (struct_fields),
    3684          103 :           std::move (outer_attrs), item_name_tok->get_locus ()));
    3685          103 :       }
    3686          320 :     case EQUAL:
    3687              :       {
    3688              :         // discriminant enum item
    3689          320 :         lexer.skip_token ();
    3690              : 
    3691          320 :         auto discriminant_expr = parse_expr ();
    3692          320 :         if (!discriminant_expr)
    3693            0 :           return Parse::Error::EnumVariant::make_child_error ();
    3694              : 
    3695          320 :         return std::make_unique<AST::EnumItemDiscriminant> (
    3696              :           std::move (item_name), std::move (vis),
    3697          320 :           std::move (discriminant_expr.value ()), std::move (outer_attrs),
    3698          640 :           item_name_tok->get_locus ());
    3699          320 :       }
    3700         1023 :     default:
    3701              :       // regular enum with just an identifier
    3702         1023 :       return std::make_unique<AST::EnumItem> (std::move (item_name),
    3703              :                                               std::move (vis),
    3704              :                                               std::move (outer_attrs),
    3705         1023 :                                               item_name_tok->get_locus ());
    3706              :     }
    3707         6785 : }
    3708              : 
    3709              : // Parses a C-style (and C-compat) untagged union declaration.
    3710              : template <typename ManagedTokenSource>
    3711              : std::unique_ptr<AST::Union>
    3712          131 : Parser<ManagedTokenSource>::parse_union (AST::Visibility vis,
    3713              :                                          AST::AttrVec outer_attrs)
    3714              : {
    3715              :   /* hack - "weak keyword" by finding identifier called "union" (lookahead in
    3716              :    * item switch) */
    3717          131 :   const_TokenPtr union_keyword = expect_token (IDENTIFIER);
    3718          131 :   rust_assert (union_keyword->get_str () == Values::WeakKeywords::UNION);
    3719          131 :   location_t locus = union_keyword->get_locus ();
    3720              : 
    3721              :   // parse actual union name
    3722          131 :   const_TokenPtr union_name_tok = expect_token (IDENTIFIER);
    3723          131 :   if (union_name_tok == nullptr)
    3724              :     {
    3725            0 :       skip_after_next_block ();
    3726            0 :       return nullptr;
    3727              :     }
    3728          262 :   Identifier union_name{union_name_tok};
    3729              : 
    3730              :   // parse optional generic parameters
    3731          131 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3732              :     = parse_generic_params_in_angles ();
    3733              : 
    3734              :   // parse optional where clause
    3735          131 :   AST::WhereClause where_clause = parse_where_clause ();
    3736              : 
    3737          131 :   if (!skip_token (LEFT_CURLY))
    3738              :     {
    3739            0 :       skip_after_end_block ();
    3740            0 :       return nullptr;
    3741              :     }
    3742              : 
    3743              :   /* parse union inner items as "struct fields" because hey, syntax reuse.
    3744              :    * Spec said so. */
    3745          131 :   std::vector<AST::StructField> union_fields
    3746              :     = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    3747              : 
    3748          131 :   if (!skip_token (RIGHT_CURLY))
    3749              :     {
    3750              :       // skip after somewhere
    3751            0 :       return nullptr;
    3752              :     }
    3753              : 
    3754              :   return std::unique_ptr<AST::Union> (
    3755          131 :     new AST::Union (std::move (union_name), std::move (vis),
    3756              :                     std::move (generic_params), std::move (where_clause),
    3757          131 :                     std::move (union_fields), std::move (outer_attrs), locus));
    3758          393 : }
    3759              : 
    3760              : /* Parses a "constant item" (compile-time constant to maybe "inline"
    3761              :  * throughout the program - like constexpr). */
    3762              : template <typename ManagedTokenSource>
    3763              : std::unique_ptr<AST::ConstantItem>
    3764         6161 : Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
    3765              :                                               AST::AttrVec outer_attrs)
    3766              : {
    3767         6161 :   location_t locus = lexer.peek_token ()->get_locus ();
    3768         6161 :   skip_token (CONST);
    3769              : 
    3770              :   /* get constant identifier - this is either a proper identifier or the _
    3771              :    * wildcard */
    3772         6161 :   const_TokenPtr ident_tok = lexer.peek_token ();
    3773              :   // make default identifier the underscore wildcard one
    3774         6161 :   std::string ident (Values::Keywords::UNDERSCORE);
    3775         6161 :   switch (ident_tok->get_id ())
    3776              :     {
    3777         6153 :     case IDENTIFIER:
    3778         6153 :       ident = ident_tok->get_str ();
    3779         6153 :       lexer.skip_token ();
    3780         6153 :       break;
    3781            8 :     case UNDERSCORE:
    3782              :       // do nothing - identifier is already "_"
    3783            8 :       lexer.skip_token ();
    3784            8 :       break;
    3785            0 :     default:
    3786            0 :       add_error (
    3787            0 :         Error (ident_tok->get_locus (),
    3788              :                "expected item name (identifier or %<_%>) in constant item "
    3789              :                "declaration - found %qs",
    3790              :                ident_tok->get_token_description ()));
    3791              : 
    3792            0 :       skip_after_semicolon ();
    3793            0 :       return nullptr;
    3794              :     }
    3795              : 
    3796         6161 :   if (!skip_token (COLON))
    3797              :     {
    3798            0 :       skip_after_semicolon ();
    3799            0 :       return nullptr;
    3800              :     }
    3801              : 
    3802              :   // parse constant type (required)
    3803         6161 :   std::unique_ptr<AST::Type> type = parse_type ();
    3804              : 
    3805              :   // A const with no given expression value
    3806        12322 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    3807              :     {
    3808            4 :       lexer.skip_token ();
    3809              :       return std::unique_ptr<AST::ConstantItem> (
    3810            8 :         new AST::ConstantItem (std::move (ident), std::move (vis),
    3811              :                                std::move (type), std::move (outer_attrs),
    3812            4 :                                locus));
    3813              :     }
    3814              : 
    3815         6157 :   if (!skip_token (EQUAL))
    3816              :     {
    3817            0 :       skip_after_semicolon ();
    3818            0 :       return nullptr;
    3819              :     }
    3820              : 
    3821              :   // parse constant expression (required)
    3822         6157 :   auto expr = parse_expr ();
    3823         6157 :   if (!expr)
    3824            1 :     return nullptr;
    3825              : 
    3826         6156 :   if (!skip_token (SEMICOLON))
    3827              :     {
    3828              :       // skip somewhere?
    3829            0 :       return nullptr;
    3830              :     }
    3831              : 
    3832              :   return std::unique_ptr<AST::ConstantItem> (
    3833        12312 :     new AST::ConstantItem (std::move (ident), std::move (vis), std::move (type),
    3834         6156 :                            std::move (expr.value ()), std::move (outer_attrs),
    3835         6156 :                            locus));
    3836        12322 : }
    3837              : 
    3838              : // Parses a "static item" (static storage item, with 'static lifetime).
    3839              : template <typename ManagedTokenSource>
    3840              : std::unique_ptr<AST::StaticItem>
    3841          381 : Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis,
    3842              :                                                AST::AttrVec outer_attrs)
    3843              : {
    3844          381 :   location_t locus = lexer.peek_token ()->get_locus ();
    3845          381 :   skip_token (STATIC_KW);
    3846              : 
    3847              :   // determine whether static item is mutable
    3848          381 :   bool is_mut = false;
    3849          762 :   if (lexer.peek_token ()->get_id () == MUT)
    3850              :     {
    3851           19 :       is_mut = true;
    3852           19 :       lexer.skip_token ();
    3853              :     }
    3854              : 
    3855          381 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    3856          381 :   if (ident_tok == nullptr)
    3857            1 :     return nullptr;
    3858              : 
    3859          760 :   Identifier ident{ident_tok};
    3860              : 
    3861          380 :   if (!skip_token (COLON))
    3862              :     {
    3863            1 :       skip_after_semicolon ();
    3864            1 :       return nullptr;
    3865              :     }
    3866              : 
    3867              :   // parse static item type (required)
    3868          379 :   std::unique_ptr<AST::Type> type = parse_type ();
    3869              : 
    3870          379 :   if (!skip_token (EQUAL))
    3871              :     {
    3872            0 :       skip_after_semicolon ();
    3873            0 :       return nullptr;
    3874              :     }
    3875              : 
    3876              :   // parse static item expression (required)
    3877          379 :   auto expr = parse_expr ();
    3878          379 :   if (!expr)
    3879            1 :     return nullptr;
    3880              : 
    3881          378 :   if (!skip_token (SEMICOLON))
    3882              :     {
    3883              :       // skip after somewhere
    3884            0 :       return nullptr;
    3885              :     }
    3886              : 
    3887              :   return std::unique_ptr<AST::StaticItem> (
    3888          756 :     new AST::StaticItem (std::move (ident), is_mut, std::move (type),
    3889          378 :                          std::move (expr.value ()), std::move (vis),
    3890          378 :                          std::move (outer_attrs), locus));
    3891          759 : }
    3892              : 
    3893              : // Parses a trait definition item, including unsafe ones.
    3894              : // also handles trait aliases
    3895              : template <typename ManagedTokenSource>
    3896              : std::unique_ptr<AST::VisItem>
    3897         5805 : Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
    3898              :                                          AST::AttrVec outer_attrs)
    3899              : {
    3900         5805 :   location_t locus = lexer.peek_token ()->get_locus ();
    3901         5805 :   bool is_unsafe = false;
    3902         5805 :   bool is_auto_trait = false;
    3903         5805 :   bool is_alias = false;
    3904              : 
    3905        11610 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    3906              :     {
    3907          201 :       is_unsafe = true;
    3908          201 :       lexer.skip_token ();
    3909              :     }
    3910              : 
    3911        11610 :   if (lexer.peek_token ()->get_id () == AUTO)
    3912              :     {
    3913           62 :       is_auto_trait = true;
    3914           62 :       lexer.skip_token ();
    3915              :     }
    3916              : 
    3917         5805 :   skip_token (TRAIT);
    3918              : 
    3919              :   // parse trait name
    3920         5805 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    3921         5805 :   if (ident_tok == nullptr)
    3922            0 :     return nullptr;
    3923              : 
    3924        11610 :   Identifier ident{ident_tok};
    3925              : 
    3926              :   // parse generic parameters (if they exist)
    3927         5805 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3928              :     = parse_generic_params_in_angles ();
    3929              : 
    3930              :   // create placeholder type param bounds in case they don't exist
    3931         5805 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    3932              : 
    3933              :   // this might be a trait alias
    3934        11610 :   if (lexer.peek_token ()->get_id () == EQUAL)
    3935              :     {
    3936            2 :       lexer.skip_token ();
    3937              : 
    3938            2 :       if (is_auto_trait)
    3939            0 :         rust_error_at (locus, "trait aliases cannot be %<auto%>");
    3940            2 :       if (is_unsafe)
    3941            0 :         rust_error_at (locus, "trait aliases cannot be %<unsafe%>");
    3942              : 
    3943            2 :       type_param_bounds = parse_type_param_bounds (
    3944            0 :         [] (TokenId id) { return id == WHERE || id == SEMICOLON; });
    3945              : 
    3946            2 :       is_alias = true;
    3947              :     }
    3948              :   // otherwise, parse type param bounds (if they exist)
    3949        11606 :   else if (lexer.peek_token ()->get_id () == COLON)
    3950              :     {
    3951         1158 :       lexer.skip_token ();
    3952              : 
    3953         1158 :       type_param_bounds = parse_type_param_bounds (
    3954          221 :         [] (TokenId id) { return id == WHERE || id == LEFT_CURLY; });
    3955              :       // type_param_bounds = parse_type_param_bounds ();
    3956              :     }
    3957              : 
    3958              :   // parse where clause (if it exists)
    3959         5805 :   AST::WhereClause where_clause = parse_where_clause ();
    3960              : 
    3961              :   // if this was a trait alias, finish up
    3962         5805 :   if (is_alias)
    3963              :     {
    3964            2 :       if (!skip_token (SEMICOLON))
    3965            0 :         return nullptr;
    3966              : 
    3967            2 :       return std::make_unique<AST::TraitAlias> (std::move (ident),
    3968              :                                                 std::move (generic_params),
    3969              :                                                 std::move (where_clause),
    3970              :                                                 std::move (type_param_bounds),
    3971              :                                                 std::move (vis),
    3972            2 :                                                 std::move (outer_attrs), locus);
    3973              :     }
    3974              : 
    3975         5803 :   if (!skip_token (LEFT_CURLY))
    3976              :     {
    3977            0 :       skip_after_end_block ();
    3978            0 :       return nullptr;
    3979              :     }
    3980              : 
    3981              :   // parse inner attrs (if they exist)
    3982         5803 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    3983              : 
    3984              :   // parse trait items
    3985         5803 :   std::vector<std::unique_ptr<AST::AssociatedItem>> trait_items;
    3986              : 
    3987         5803 :   const_TokenPtr t = lexer.peek_token ();
    3988        12815 :   while (t->get_id () != RIGHT_CURLY)
    3989              :     {
    3990         7012 :       std::unique_ptr<AST::AssociatedItem> trait_item = parse_trait_item ();
    3991              : 
    3992         7012 :       if (trait_item == nullptr)
    3993              :         {
    3994            0 :           Error error (lexer.peek_token ()->get_locus (),
    3995              :                        "failed to parse trait item in trait");
    3996            0 :           add_error (std::move (error));
    3997              : 
    3998            0 :           return nullptr;
    3999            0 :         }
    4000         7012 :       trait_items.push_back (std::move (trait_item));
    4001              : 
    4002         7012 :       t = lexer.peek_token ();
    4003              :     }
    4004              : 
    4005         5803 :   if (!skip_token (RIGHT_CURLY))
    4006              :     {
    4007              :       // skip after something
    4008            0 :       return nullptr;
    4009              :     }
    4010              : 
    4011         5803 :   trait_items.shrink_to_fit ();
    4012         5803 :   return std::unique_ptr<AST::Trait> (
    4013        11606 :     new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
    4014              :                     std::move (generic_params), std::move (type_param_bounds),
    4015              :                     std::move (where_clause), std::move (trait_items),
    4016              :                     std::move (vis), std::move (outer_attrs),
    4017         5803 :                     std::move (inner_attrs), locus));
    4018        11608 : }
    4019              : 
    4020              : // Parses a trait item used inside traits (not trait, the Item).
    4021              : template <typename ManagedTokenSource>
    4022              : std::unique_ptr<AST::AssociatedItem>
    4023         7014 : Parser<ManagedTokenSource>::parse_trait_item ()
    4024              : {
    4025              :   // parse outer attributes (if they exist)
    4026         7014 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4027              : 
    4028         7014 :   auto vis_res = parse_visibility ();
    4029         7014 :   if (!vis_res)
    4030            0 :     return nullptr;
    4031              : 
    4032         7014 :   auto vis = vis_res.value ();
    4033              : 
    4034              :   // lookahead to determine what type of trait item to parse
    4035         7014 :   const_TokenPtr tok = lexer.peek_token ();
    4036         7014 :   switch (tok->get_id ())
    4037              :     {
    4038            0 :     case SUPER:
    4039              :     case SELF:
    4040              :     case CRATE:
    4041              :     case DOLLAR_SIGN:
    4042              :       // these seem to be SimplePath tokens, so this is a macro invocation
    4043              :       // semi
    4044            0 :       return parse_macro_invocation_semi (std::move (outer_attrs));
    4045            1 :     case IDENTIFIER:
    4046            2 :       if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
    4047            0 :         return parse_function (std::move (vis), std::move (outer_attrs));
    4048              :       else
    4049            2 :         return parse_macro_invocation_semi (std::move (outer_attrs));
    4050         1180 :     case TYPE:
    4051         1180 :       return parse_trait_type (std::move (outer_attrs), vis);
    4052          195 :     case CONST:
    4053              :       // disambiguate with function qualifier
    4054          390 :       if (lexer.peek_token (1)->get_id () == IDENTIFIER)
    4055              :         {
    4056          388 :           return parse_trait_const (std::move (outer_attrs));
    4057              :         }
    4058              :       // else, fallthrough to function
    4059              :       // TODO: find out how to disable gcc "implicit fallthrough" error
    4060              :       gcc_fallthrough ();
    4061              :     case ASYNC:
    4062              :     case UNSAFE:
    4063              :     case EXTERN_KW:
    4064              :     case FN_KW:
    4065        11278 :       return parse_function (std::move (vis), std::move (outer_attrs));
    4066              :     default:
    4067              :       break;
    4068              :     }
    4069            0 :   add_error (Error (tok->get_locus (),
    4070              :                     "unrecognised token %qs for item in trait",
    4071              :                     tok->get_token_description ()));
    4072              :   // skip?
    4073            0 :   return nullptr;
    4074        14028 : }
    4075              : 
    4076              : // Parse a typedef trait item.
    4077              : template <typename ManagedTokenSource>
    4078              : std::unique_ptr<AST::TraitItemType>
    4079         1180 : Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs,
    4080              :                                               AST::Visibility vis)
    4081              : {
    4082         1180 :   location_t locus = lexer.peek_token ()->get_locus ();
    4083         1180 :   skip_token (TYPE);
    4084              : 
    4085         1180 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4086         1180 :   if (ident_tok == nullptr)
    4087            0 :     return nullptr;
    4088              : 
    4089         2360 :   Identifier ident{ident_tok};
    4090              : 
    4091              :   // Parse optional generic parameters for GATs (Generic Associated Types)
    4092         1180 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
    4093         2360 :   if (lexer.peek_token ()->get_id () == LEFT_ANGLE)
    4094              :     {
    4095           17 :       generic_params = parse_generic_params_in_angles ();
    4096              :     }
    4097              : 
    4098         1180 :   std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    4099              : 
    4100              :   // parse optional colon
    4101         2360 :   if (lexer.peek_token ()->get_id () == COLON)
    4102              :     {
    4103          139 :       lexer.skip_token ();
    4104              : 
    4105              :       // parse optional type param bounds
    4106              :       bounds
    4107          139 :         = parse_type_param_bounds ([] (TokenId id) { return id == SEMICOLON; });
    4108              :       // bounds = parse_type_param_bounds ();
    4109              :     }
    4110              : 
    4111         1180 :   if (!skip_token (SEMICOLON))
    4112              :     {
    4113              :       // skip?
    4114            0 :       return nullptr;
    4115              :     }
    4116              : 
    4117              :   return std::unique_ptr<AST::TraitItemType> (
    4118         1180 :     new AST::TraitItemType (std::move (ident), std::move (generic_params),
    4119              :                             std::move (bounds), std::move (outer_attrs), vis,
    4120         1180 :                             locus));
    4121         1180 : }
    4122              : 
    4123              : // Parses a constant trait item.
    4124              : template <typename ManagedTokenSource>
    4125              : std::unique_ptr<AST::ConstantItem>
    4126          194 : Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
    4127              : {
    4128          194 :   location_t locus = lexer.peek_token ()->get_locus ();
    4129          194 :   skip_token (CONST);
    4130              : 
    4131              :   // parse constant item name
    4132          194 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4133          194 :   if (ident_tok == nullptr)
    4134            0 :     return nullptr;
    4135              : 
    4136          388 :   Identifier ident{ident_tok};
    4137              : 
    4138          194 :   if (!skip_token (COLON))
    4139              :     {
    4140            0 :       skip_after_semicolon ();
    4141            0 :       return nullptr;
    4142              :     }
    4143              : 
    4144              :   // parse constant trait item type
    4145          194 :   std::unique_ptr<AST::Type> type = parse_type ();
    4146              : 
    4147              :   // parse constant trait body expression, if it exists
    4148          194 :   std::unique_ptr<AST::Expr> const_body = nullptr;
    4149          388 :   if (lexer.peek_token ()->get_id () == EQUAL)
    4150              :     {
    4151           12 :       lexer.skip_token ();
    4152              : 
    4153              :       // expression must exist, so parse it
    4154           12 :       auto expr = parse_expr ();
    4155           12 :       if (!expr)
    4156            0 :         return nullptr;
    4157           12 :       const_body = std::move (expr.value ());
    4158           12 :     }
    4159              : 
    4160          194 :   if (!skip_token (SEMICOLON))
    4161              :     {
    4162              :       // skip after something?
    4163            0 :       return nullptr;
    4164              :     }
    4165              : 
    4166          582 :   return std::unique_ptr<AST::ConstantItem> (new AST::ConstantItem (
    4167          388 :     std::move (ident), AST::Visibility::create_private (), std::move (type),
    4168          194 :     std::move (const_body), std::move (outer_attrs), locus));
    4169          388 : }
    4170              : 
    4171              : /* Parses a struct "impl" item (both inherent impl and trait impl can be
    4172              :  * parsed here), */
    4173              : template <typename ManagedTokenSource>
    4174              : std::unique_ptr<AST::Impl>
    4175        57684 : Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
    4176              :                                         AST::AttrVec outer_attrs)
    4177              : {
    4178              :   /* Note that only trait impls are allowed to be unsafe. So if unsafe, it
    4179              :    * must be a trait impl. However, this isn't enough for full disambiguation,
    4180              :    * so don't branch here. */
    4181        57684 :   location_t locus = lexer.peek_token ()->get_locus ();
    4182        57684 :   bool is_unsafe = false;
    4183       115368 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    4184              :     {
    4185         1361 :       lexer.skip_token ();
    4186         1361 :       is_unsafe = true;
    4187              :     }
    4188              : 
    4189        57684 :   if (!skip_token (IMPL))
    4190              :     {
    4191            0 :       skip_after_next_block ();
    4192            0 :       return nullptr;
    4193              :     }
    4194              : 
    4195              :   // parse generic params (shared by trait and inherent impls)
    4196        57684 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4197              :     = parse_generic_params_in_angles ();
    4198              : 
    4199              :   // Again, trait impl-only feature, but optional one, so can be used for
    4200              :   // branching yet.
    4201        57684 :   bool has_exclam = false;
    4202       115368 :   if (lexer.peek_token ()->get_id () == EXCLAM)
    4203              :     {
    4204          125 :       lexer.skip_token ();
    4205          125 :       has_exclam = true;
    4206              :     }
    4207              : 
    4208              :   /* FIXME: code that doesn't look shit for TypePath. Also, make sure this
    4209              :    * doesn't parse too much and not work. */
    4210        57684 :   AST::TypePath type_path = parse_type_path ();
    4211       115059 :   if (type_path.is_error () || lexer.peek_token ()->get_id () != FOR)
    4212              :     {
    4213              :       /* cannot parse type path (or not for token next, at least), so must be
    4214              :        * inherent impl */
    4215              : 
    4216              :       // hacky conversion of TypePath stack object to Type pointer
    4217         3671 :       std::unique_ptr<AST::Type> type = nullptr;
    4218         3671 :       if (!type_path.is_error ())
    4219         3362 :         type = std::unique_ptr<AST::TypePath> (
    4220         3362 :           new AST::TypePath (std::move (type_path)));
    4221              :       else
    4222          309 :         type = parse_type ();
    4223              : 
    4224              :       // Type is required, so error if null
    4225         3671 :       if (type == nullptr)
    4226              :         {
    4227            1 :           Error error (lexer.peek_token ()->get_locus (),
    4228              :                        "could not parse type in inherent impl");
    4229            1 :           add_error (std::move (error));
    4230              : 
    4231            1 :           skip_after_next_block ();
    4232            1 :           return nullptr;
    4233            1 :         }
    4234              : 
    4235              :       // parse optional where clause
    4236         3670 :       AST::WhereClause where_clause = parse_where_clause ();
    4237              : 
    4238         3670 :       if (!skip_token (LEFT_CURLY))
    4239              :         {
    4240              :           // TODO: does this still skip properly?
    4241            0 :           skip_after_end_block ();
    4242            0 :           return nullptr;
    4243              :         }
    4244              : 
    4245              :       // parse inner attributes (optional)
    4246         3670 :       AST::AttrVec inner_attrs = parse_inner_attributes ();
    4247              : 
    4248              :       // parse inherent impl items
    4249         3670 :       std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
    4250              : 
    4251         3670 :       const_TokenPtr t = lexer.peek_token ();
    4252        18003 :       while (t->get_id () != RIGHT_CURLY)
    4253              :         {
    4254        14343 :           std::unique_ptr<AST::AssociatedItem> impl_item
    4255              :             = parse_inherent_impl_item ();
    4256              : 
    4257        14343 :           if (impl_item == nullptr)
    4258              :             {
    4259           10 :               Error error (
    4260           10 :                 lexer.peek_token ()->get_locus (),
    4261              :                 "failed to parse inherent impl item in inherent impl");
    4262           10 :               add_error (std::move (error));
    4263              : 
    4264           10 :               return nullptr;
    4265           10 :             }
    4266              : 
    4267        14333 :           impl_items.push_back (std::move (impl_item));
    4268              : 
    4269        14333 :           t = lexer.peek_token ();
    4270              :         }
    4271              : 
    4272         3660 :       if (!skip_token (RIGHT_CURLY))
    4273              :         {
    4274              :           // skip somewhere
    4275            0 :           return nullptr;
    4276              :         }
    4277              : 
    4278              :       // DEBUG
    4279         3660 :       rust_debug ("successfully parsed inherent impl");
    4280              : 
    4281         3660 :       impl_items.shrink_to_fit ();
    4282              : 
    4283         3660 :       return std::unique_ptr<AST::InherentImpl> (new AST::InherentImpl (
    4284              :         std::move (impl_items), std::move (generic_params), std::move (type),
    4285              :         std::move (where_clause), std::move (vis), std::move (inner_attrs),
    4286         3660 :         std::move (outer_attrs), locus));
    4287         3671 :     }
    4288              :   else
    4289              :     {
    4290              :       // type path must both be valid and next token is for, so trait impl
    4291        54013 :       if (!skip_token (FOR))
    4292              :         {
    4293            0 :           skip_after_next_block ();
    4294            0 :           return nullptr;
    4295              :         }
    4296              : 
    4297              :       // parse type
    4298        54013 :       std::unique_ptr<AST::Type> type = parse_type ();
    4299              :       // ensure type is included as it is required
    4300        54013 :       if (type == nullptr)
    4301              :         {
    4302            0 :           Error error (lexer.peek_token ()->get_locus (),
    4303              :                        "could not parse type in trait impl");
    4304            0 :           add_error (std::move (error));
    4305              : 
    4306            0 :           skip_after_next_block ();
    4307            0 :           return nullptr;
    4308            0 :         }
    4309              : 
    4310              :       // parse optional where clause
    4311        54013 :       AST::WhereClause where_clause = parse_where_clause ();
    4312              : 
    4313        54013 :       if (!skip_token (LEFT_CURLY))
    4314              :         {
    4315              :           // TODO: does this still skip properly?
    4316            0 :           skip_after_end_block ();
    4317            0 :           return nullptr;
    4318              :         }
    4319              : 
    4320              :       // parse inner attributes (optional)
    4321        54013 :       AST::AttrVec inner_attrs = parse_inner_attributes ();
    4322              : 
    4323              :       // parse trait impl items
    4324        54013 :       std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
    4325              : 
    4326        54013 :       const_TokenPtr t = lexer.peek_token ();
    4327       133412 :       while (t->get_id () != RIGHT_CURLY)
    4328              :         {
    4329        79402 :           std::unique_ptr<AST::AssociatedItem> impl_item
    4330              :             = parse_trait_impl_item ();
    4331              : 
    4332        79402 :           if (impl_item == nullptr)
    4333              :             {
    4334            3 :               Error error (lexer.peek_token ()->get_locus (),
    4335              :                            "failed to parse trait impl item in trait impl");
    4336            3 :               add_error (std::move (error));
    4337              : 
    4338            3 :               return nullptr;
    4339            3 :             }
    4340              : 
    4341        79399 :           impl_items.push_back (std::move (impl_item));
    4342              : 
    4343        79399 :           t = lexer.peek_token ();
    4344              : 
    4345              :           // DEBUG
    4346        79399 :           rust_debug ("successfully parsed a trait impl item");
    4347              :         }
    4348              :       // DEBUG
    4349        54010 :       rust_debug ("successfully finished trait impl items");
    4350              : 
    4351        54010 :       if (!skip_token (RIGHT_CURLY))
    4352              :         {
    4353              :           // skip somewhere
    4354            0 :           return nullptr;
    4355              :         }
    4356              : 
    4357              :       // DEBUG
    4358        54010 :       rust_debug ("successfully parsed trait impl");
    4359              : 
    4360        54010 :       impl_items.shrink_to_fit ();
    4361              : 
    4362        54010 :       return std::unique_ptr<AST::TraitImpl> (
    4363       108020 :         new AST::TraitImpl (std::move (type_path), is_unsafe, has_exclam,
    4364              :                             std::move (impl_items), std::move (generic_params),
    4365              :                             std::move (type), std::move (where_clause),
    4366              :                             std::move (vis), std::move (inner_attrs),
    4367        54010 :                             std::move (outer_attrs), locus));
    4368        54013 :     }
    4369        57684 : }
    4370              : 
    4371              : // Parses a single inherent impl item (item inside an inherent impl block).
    4372              : template <typename ManagedTokenSource>
    4373              : std::unique_ptr<AST::AssociatedItem>
    4374        30377 : Parser<ManagedTokenSource>::parse_inherent_impl_item ()
    4375              : {
    4376              :   // parse outer attributes (if they exist)
    4377        30377 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4378              : 
    4379              :   // TODO: cleanup - currently an unreadable mess
    4380              : 
    4381              :   // branch on next token:
    4382        30377 :   const_TokenPtr t = lexer.peek_token ();
    4383        30377 :   switch (t->get_id ())
    4384              :     {
    4385         8721 :     case IDENTIFIER:
    4386              :       // FIXME: Arthur: Do we need to some lookahead here?
    4387        17442 :       return parse_macro_invocation_semi (outer_attrs);
    4388        20243 :     case SUPER:
    4389              :     case SELF:
    4390              :     case CRATE:
    4391              :     case PUB:
    4392              :       {
    4393              :         // visibility, so not a macro invocation semi - must be constant,
    4394              :         // function, or method
    4395        20243 :         auto vis_res = parse_visibility ();
    4396        20243 :         if (!vis_res)
    4397            0 :           return nullptr;
    4398        20243 :         auto vis = vis_res.value ();
    4399              : 
    4400              :         // TODO: is a recursive call to parse_inherent_impl_item better?
    4401        40486 :         switch (lexer.peek_token ()->get_id ())
    4402              :           {
    4403         8115 :           case EXTERN_KW:
    4404              :           case UNSAFE:
    4405              :           case FN_KW:
    4406              :             // function or method
    4407        16230 :             return parse_inherent_impl_function_or_method (std::move (vis),
    4408              :                                                            std::move (
    4409         8115 :                                                              outer_attrs));
    4410        12128 :           case CONST:
    4411              :             // lookahead to resolve production - could be function/method or
    4412              :             // const item
    4413        12128 :             t = lexer.peek_token (1);
    4414              : 
    4415        12128 :             switch (t->get_id ())
    4416              :               {
    4417          796 :               case IDENTIFIER:
    4418              :               case UNDERSCORE:
    4419         1592 :                 return parse_const_item (std::move (vis),
    4420          796 :                                          std::move (outer_attrs));
    4421        11332 :               case UNSAFE:
    4422              :               case EXTERN_KW:
    4423              :               case FN_KW:
    4424        22664 :                 return parse_inherent_impl_function_or_method (std::move (vis),
    4425              :                                                                std::move (
    4426        11332 :                                                                  outer_attrs));
    4427            0 :               default:
    4428            0 :                 add_error (Error (t->get_locus (),
    4429              :                                   "unexpected token %qs in some sort of const "
    4430              :                                   "item in inherent impl",
    4431              :                                   t->get_token_description ()));
    4432              : 
    4433            0 :                 lexer.skip_token (1); // TODO: is this right thing to do?
    4434            0 :                 return nullptr;
    4435              :               }
    4436            0 :           default:
    4437            0 :             add_error (
    4438            0 :               Error (t->get_locus (),
    4439              :                      "unrecognised token %qs for item in inherent impl",
    4440              :                      t->get_token_description ()));
    4441              :             // skip?
    4442            0 :             return nullptr;
    4443              :           }
    4444        40486 :       }
    4445         1271 :     case ASYNC:
    4446              :     case EXTERN_KW:
    4447              :     case UNSAFE:
    4448              :     case FN_KW:
    4449              :       // function or method
    4450         1271 :       return parse_inherent_impl_function_or_method (
    4451         1271 :         AST::Visibility::create_private (), std::move (outer_attrs));
    4452          142 :     case CONST:
    4453              :       /* lookahead to resolve production - could be function/method or const
    4454              :        * item */
    4455          142 :       t = lexer.peek_token (1);
    4456              : 
    4457          142 :       switch (t->get_id ())
    4458              :         {
    4459           45 :         case IDENTIFIER:
    4460              :         case UNDERSCORE:
    4461           90 :           return parse_const_item (AST::Visibility::create_private (),
    4462           45 :                                    std::move (outer_attrs));
    4463           97 :         case UNSAFE:
    4464              :         case EXTERN_KW:
    4465              :         case FN_KW:
    4466           97 :           return parse_inherent_impl_function_or_method (
    4467           97 :             AST::Visibility::create_private (), std::move (outer_attrs));
    4468            0 :         default:
    4469            0 :           add_error (Error (t->get_locus (),
    4470              :                             "unexpected token %qs in some sort of const item "
    4471              :                             "in inherent impl",
    4472              :                             t->get_token_description ()));
    4473              : 
    4474            0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    4475            0 :           return nullptr;
    4476              :         }
    4477              :       rust_unreachable ();
    4478            0 :     default:
    4479            0 :       add_error (Error (t->get_locus (),
    4480              :                         "unrecognised token %qs for item in inherent impl",
    4481              :                         t->get_token_description ()));
    4482              : 
    4483              :       // skip?
    4484            0 :       return nullptr;
    4485              :     }
    4486        30377 : }
    4487              : 
    4488              : /* For internal use only by parse_inherent_impl_item() - splits giant method
    4489              :  * into smaller ones and prevents duplication of logic. Strictly, this parses
    4490              :  * a function or method item inside an inherent impl item block. */
    4491              : // TODO: make this a templated function with "return type" as type param -
    4492              : // InherentImplItem is this specialisation of the template while TraitImplItem
    4493              : // will be the other.
    4494              : template <typename ManagedTokenSource>
    4495              : std::unique_ptr<AST::AssociatedItem>
    4496        20815 : Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
    4497              :   AST::Visibility vis, AST::AttrVec outer_attrs)
    4498              : {
    4499        20815 :   location_t locus = lexer.peek_token ()->get_locus ();
    4500              :   // parse function or method qualifiers
    4501        20815 :   auto qualifiers = parse_function_qualifiers ();
    4502        20815 :   if (!qualifiers)
    4503            0 :     return nullptr;
    4504              : 
    4505        20815 :   skip_token (FN_KW);
    4506              : 
    4507              :   // parse function or method name
    4508        20815 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4509        20815 :   if (ident_tok == nullptr)
    4510            7 :     return nullptr;
    4511              : 
    4512        41616 :   Identifier ident{ident_tok};
    4513              : 
    4514              :   // parse generic params
    4515        20808 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4516              :     = parse_generic_params_in_angles ();
    4517              : 
    4518        20808 :   if (!skip_token (LEFT_PAREN))
    4519              :     {
    4520              :       // skip after somewhere?
    4521            0 :       return nullptr;
    4522              :     }
    4523              : 
    4524              :   // now for function vs method disambiguation - method has opening "self"
    4525              :   // param
    4526        20808 :   auto initial_param = parse_self_param ();
    4527              : 
    4528        20808 :   if (!initial_param.has_value ()
    4529        20808 :       && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
    4530            3 :     return nullptr;
    4531              : 
    4532              :   /* FIXME: ensure that self param doesn't accidently consume tokens for a
    4533              :    * function one idea is to lookahead up to 4 tokens to see whether self is
    4534              :    * one of them */
    4535        20805 :   bool is_method = false;
    4536        20805 :   if (initial_param.has_value ())
    4537              :     {
    4538        16497 :       if ((*initial_param)->is_self ())
    4539              :         is_method = true;
    4540              : 
    4541              :       /* skip comma so function and method regular params can be parsed in
    4542              :        * same way */
    4543        32994 :       if (lexer.peek_token ()->get_id () == COMMA)
    4544         8809 :         lexer.skip_token ();
    4545              :     }
    4546              : 
    4547              :   // parse trait function params
    4548        20805 :   std::vector<std::unique_ptr<AST::Param>> function_params
    4549              :     = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    4550              : 
    4551        20805 :   if (initial_param.has_value ())
    4552        16497 :     function_params.insert (function_params.begin (),
    4553        16497 :                             std::move (*initial_param));
    4554              : 
    4555        20805 :   if (!skip_token (RIGHT_PAREN))
    4556              :     {
    4557            0 :       skip_after_end_block ();
    4558            0 :       return nullptr;
    4559              :     }
    4560              : 
    4561              :   // parse return type (optional)
    4562        20805 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    4563              : 
    4564              :   // parse where clause (optional)
    4565        20805 :   AST::WhereClause where_clause = parse_where_clause ();
    4566              : 
    4567        20805 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    4568        41610 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    4569            2 :     lexer.skip_token ();
    4570              :   else
    4571              :     {
    4572        20803 :       auto result = parse_block_expr ();
    4573              : 
    4574        20803 :       if (!result)
    4575              :         {
    4576            0 :           Error error (
    4577            0 :             lexer.peek_token ()->get_locus (),
    4578              :             "could not parse definition in inherent impl %s definition",
    4579              :             is_method ? "method" : "function");
    4580            0 :           add_error (std::move (error));
    4581              : 
    4582            0 :           skip_after_end_block ();
    4583            0 :           return nullptr;
    4584            0 :         }
    4585        20803 :       body = std::move (result.value ());
    4586        20803 :     }
    4587              : 
    4588        20805 :   return std::unique_ptr<AST::Function> (
    4589        83218 :     new AST::Function (std::move (ident), std::move (qualifiers.value ()),
    4590              :                        std::move (generic_params), std::move (function_params),
    4591              :                        std::move (return_type), std::move (where_clause),
    4592              :                        std::move (body), std::move (vis),
    4593        20805 :                        std::move (outer_attrs), locus));
    4594        62428 : }
    4595              : 
    4596              : // Parses a single trait impl item (item inside a trait impl block).
    4597              : template <typename ManagedTokenSource>
    4598              : std::unique_ptr<AST::AssociatedItem>
    4599        80474 : Parser<ManagedTokenSource>::parse_trait_impl_item ()
    4600              : {
    4601              :   // parse outer attributes (if they exist)
    4602        80474 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4603              : 
    4604        80474 :   auto vis_res = parse_visibility ();
    4605        80474 :   if (!vis_res)
    4606            0 :     return nullptr;
    4607        80474 :   auto visibility = vis_res.value ();
    4608              : 
    4609              :   // branch on next token:
    4610        80474 :   const_TokenPtr t = lexer.peek_token ();
    4611        80474 :   switch (t->get_id ())
    4612              :     {
    4613            0 :     case SUPER:
    4614              :     case SELF:
    4615              :     case CRATE:
    4616              :     case DOLLAR_SIGN:
    4617              :       // these seem to be SimplePath tokens, so this is a macro invocation
    4618              :       // semi
    4619            0 :       return parse_macro_invocation_semi (std::move (outer_attrs));
    4620          574 :     case IDENTIFIER:
    4621         1148 :       if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
    4622          540 :         return parse_trait_impl_function_or_method (visibility,
    4623          270 :                                                     std::move (outer_attrs));
    4624              :       else
    4625          608 :         return parse_macro_invocation_semi (std::move (outer_attrs));
    4626        22766 :     case TYPE:
    4627        45532 :       return parse_type_alias (visibility, std::move (outer_attrs));
    4628        56763 :     case EXTERN_KW:
    4629              :     case UNSAFE:
    4630              :     case FN_KW:
    4631              :       // function or method
    4632       113526 :       return parse_trait_impl_function_or_method (visibility,
    4633        56763 :                                                   std::move (outer_attrs));
    4634            1 :     case ASYNC:
    4635            2 :       return parse_async_item (visibility, std::move (outer_attrs));
    4636          370 :     case CONST:
    4637              :       // lookahead to resolve production - could be function/method or const
    4638              :       // item
    4639          370 :       t = lexer.peek_token (1);
    4640              : 
    4641          370 :       switch (t->get_id ())
    4642              :         {
    4643          369 :         case IDENTIFIER:
    4644              :         case UNDERSCORE:
    4645          738 :           return parse_const_item (visibility, std::move (outer_attrs));
    4646            1 :         case UNSAFE:
    4647              :         case EXTERN_KW:
    4648              :         case FN_KW:
    4649            2 :           return parse_trait_impl_function_or_method (visibility,
    4650            1 :                                                       std::move (outer_attrs));
    4651            0 :         default:
    4652            0 :           add_error (Error (
    4653              :             t->get_locus (),
    4654              :             "unexpected token %qs in some sort of const item in trait impl",
    4655              :             t->get_token_description ()));
    4656              : 
    4657            0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    4658            0 :           return nullptr;
    4659              :         }
    4660              :       rust_unreachable ();
    4661              :     default:
    4662              :       break;
    4663              :     }
    4664            0 :   add_error (Error (t->get_locus (),
    4665              :                     "unrecognised token %qs for item in trait impl",
    4666              :                     t->get_token_description ()));
    4667              : 
    4668              :   // skip?
    4669            0 :   return nullptr;
    4670       160948 : }
    4671              : 
    4672              : /* For internal use only by parse_trait_impl_item() - splits giant method into
    4673              :  * smaller ones and prevents duplication of logic. Strictly, this parses a
    4674              :  * function or method item inside a trait impl item block. */
    4675              : template <typename ManagedTokenSource>
    4676              : std::unique_ptr<AST::AssociatedItem>
    4677        57034 : Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
    4678              :   AST::Visibility vis, AST::AttrVec outer_attrs)
    4679              : {
    4680              :   // this shares virtually all logic with
    4681              :   // parse_inherent_impl_function_or_method
    4682              :   // - template?
    4683        57034 :   location_t locus = lexer.peek_token ()->get_locus ();
    4684              : 
    4685              :   // parse function or method qualifiers
    4686        57034 :   auto qualifiers = parse_function_qualifiers ();
    4687        57034 :   if (!qualifiers)
    4688            2 :     return nullptr;
    4689              : 
    4690        57032 :   skip_token (FN_KW);
    4691              : 
    4692              :   // parse function or method name
    4693        57032 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4694        57032 :   if (ident_tok == nullptr)
    4695              :     {
    4696            0 :       return nullptr;
    4697              :     }
    4698       114064 :   Identifier ident{ident_tok};
    4699              : 
    4700              :   // DEBUG:
    4701        57032 :   rust_debug (
    4702              :     "about to start parsing generic params in trait impl function or method");
    4703              : 
    4704              :   // parse generic params
    4705        57032 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4706              :     = parse_generic_params_in_angles ();
    4707              : 
    4708              :   // DEBUG:
    4709        57032 :   rust_debug (
    4710              :     "finished parsing generic params in trait impl function or method");
    4711              : 
    4712        57032 :   if (!skip_token (LEFT_PAREN))
    4713              :     {
    4714              :       // skip after somewhere?
    4715            0 :       return nullptr;
    4716              :     }
    4717              : 
    4718              :   // now for function vs method disambiguation - method has opening "self"
    4719              :   // param
    4720        57032 :   auto initial_param = parse_self_param ();
    4721              : 
    4722        57032 :   if (!initial_param.has_value ()
    4723        57032 :       && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
    4724            0 :     return nullptr;
    4725              : 
    4726              :   // FIXME: ensure that self param doesn't accidently consume tokens for a
    4727              :   // function
    4728        57032 :   bool is_method = false;
    4729        57032 :   if (initial_param.has_value ())
    4730              :     {
    4731        50434 :       if ((*initial_param)->is_self ())
    4732              :         is_method = true;
    4733              : 
    4734              :       // skip comma so function and method regular params can be parsed in
    4735              :       // same way
    4736       100868 :       if (lexer.peek_token ()->get_id () == COMMA)
    4737              :         {
    4738        41172 :           lexer.skip_token ();
    4739              :         }
    4740              : 
    4741              :       // DEBUG
    4742        50434 :       rust_debug ("successfully parsed self param in method trait impl item");
    4743              :     }
    4744              : 
    4745              :   // DEBUG
    4746        57032 :   rust_debug (
    4747              :     "started to parse function params in function or method trait impl item");
    4748              : 
    4749              :   // parse trait function params (only if next token isn't right paren)
    4750        57032 :   std::vector<std::unique_ptr<AST::Param>> function_params;
    4751       114064 :   if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
    4752              :     {
    4753              :       function_params
    4754        46555 :         = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    4755              : 
    4756        46555 :       if (function_params.empty ())
    4757              :         {
    4758            0 :           Error error (
    4759            0 :             lexer.peek_token ()->get_locus (),
    4760              :             "failed to parse function params in trait impl %s definition",
    4761              :             is_method ? "method" : "function");
    4762            0 :           add_error (std::move (error));
    4763              : 
    4764            0 :           skip_after_next_block ();
    4765            0 :           return nullptr;
    4766            0 :         }
    4767              :     }
    4768              : 
    4769        57032 :   if (initial_param.has_value ())
    4770        50434 :     function_params.insert (function_params.begin (),
    4771        50434 :                             std::move (*initial_param));
    4772              : 
    4773              :   // DEBUG
    4774        57032 :   rust_debug ("successfully parsed function params in function or method "
    4775              :               "trait impl item");
    4776              : 
    4777        57032 :   if (!skip_token (RIGHT_PAREN))
    4778              :     {
    4779            0 :       skip_after_next_block ();
    4780            0 :       return nullptr;
    4781              :     }
    4782              : 
    4783              :   // parse return type (optional)
    4784        57032 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    4785              : 
    4786              :   // DEBUG
    4787        57032 :   rust_debug (
    4788              :     "successfully parsed return type in function or method trait impl item");
    4789              : 
    4790              :   // parse where clause (optional)
    4791        57032 :   AST::WhereClause where_clause = parse_where_clause ();
    4792              : 
    4793              :   // DEBUG
    4794        57032 :   rust_debug (
    4795              :     "successfully parsed where clause in function or method trait impl item");
    4796              : 
    4797              :   // parse function definition (in block) - semicolon not allowed
    4798        57032 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    4799              : 
    4800       114064 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    4801            1 :     lexer.skip_token ();
    4802              :   else
    4803              :     {
    4804        57031 :       auto result = parse_block_expr ();
    4805        57031 :       if (!result)
    4806              :         {
    4807            1 :           Error error (lexer.peek_token ()->get_locus (),
    4808              :                        "could not parse definition in trait impl %s definition",
    4809              :                        is_method ? "method" : "function");
    4810            1 :           add_error (std::move (error));
    4811              : 
    4812            1 :           skip_after_end_block ();
    4813            1 :           return nullptr;
    4814            1 :         }
    4815        57030 :       body = std::move (result.value ());
    4816        57031 :     }
    4817              : 
    4818        57031 :   return std::unique_ptr<AST::Function> (
    4819       228123 :     new AST::Function (std::move (ident), std::move (qualifiers.value ()),
    4820              :                        std::move (generic_params), std::move (function_params),
    4821              :                        std::move (return_type), std::move (where_clause),
    4822              :                        std::move (body), std::move (vis),
    4823        57031 :                        std::move (outer_attrs), locus));
    4824       171096 : }
    4825              : 
    4826              : // Parses an extern block of declarations.
    4827              : template <typename ManagedTokenSource>
    4828              : std::unique_ptr<AST::ExternBlock>
    4829         2223 : Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
    4830              :                                                 AST::AttrVec outer_attrs)
    4831              : {
    4832         2223 :   location_t locus = lexer.peek_token ()->get_locus ();
    4833         2223 :   skip_token (EXTERN_KW);
    4834              : 
    4835              :   // detect optional abi name
    4836         2223 :   std::string abi;
    4837         2223 :   const_TokenPtr next_tok = lexer.peek_token ();
    4838         2223 :   if (next_tok->get_id () == STRING_LITERAL)
    4839              :     {
    4840         2222 :       lexer.skip_token ();
    4841         2222 :       abi = next_tok->get_str ();
    4842              :     }
    4843              : 
    4844         2223 :   if (!skip_token (LEFT_CURLY))
    4845              :     {
    4846            0 :       skip_after_end_block ();
    4847            0 :       return nullptr;
    4848              :     }
    4849              : 
    4850         2223 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    4851              : 
    4852              :   // parse declarations inside extern block
    4853         2223 :   std::vector<std::unique_ptr<AST::ExternalItem>> extern_items;
    4854              : 
    4855         2223 :   const_TokenPtr t = lexer.peek_token ();
    4856        12041 :   while (t->get_id () != RIGHT_CURLY)
    4857              :     {
    4858         9819 :       std::unique_ptr<AST::ExternalItem> extern_item = parse_external_item ();
    4859              : 
    4860         9819 :       if (extern_item == nullptr)
    4861              :         {
    4862            1 :           Error error (t->get_locus (),
    4863              :                        "failed to parse external item despite not reaching "
    4864              :                        "end of extern block");
    4865            1 :           add_error (std::move (error));
    4866              : 
    4867            1 :           return nullptr;
    4868            1 :         }
    4869              : 
    4870         9818 :       extern_items.push_back (std::move (extern_item));
    4871              : 
    4872         9818 :       t = lexer.peek_token ();
    4873              :     }
    4874              : 
    4875         2222 :   if (!skip_token (RIGHT_CURLY))
    4876              :     {
    4877              :       // skip somewhere
    4878            0 :       return nullptr;
    4879              :     }
    4880              : 
    4881         2222 :   extern_items.shrink_to_fit ();
    4882              : 
    4883              :   return std::unique_ptr<AST::ExternBlock> (
    4884         2222 :     new AST::ExternBlock (std::move (abi), std::move (extern_items),
    4885              :                           std::move (vis), std::move (inner_attrs),
    4886         2222 :                           std::move (outer_attrs), locus));
    4887         4446 : }
    4888              : 
    4889              : // Parses a single extern block item (static or function declaration).
    4890              : template <typename ManagedTokenSource>
    4891              : std::unique_ptr<AST::ExternalItem>
    4892         9822 : Parser<ManagedTokenSource>::parse_external_item ()
    4893              : {
    4894              :   // parse optional outer attributes
    4895         9822 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4896              : 
    4897         9822 :   location_t locus = lexer.peek_token ()->get_locus ();
    4898              : 
    4899              :   // parse optional visibility
    4900         9822 :   auto vis_res = parse_visibility ();
    4901         9822 :   if (!vis_res)
    4902            0 :     return nullptr;
    4903         9822 :   auto vis = vis_res.value ();
    4904              : 
    4905         9822 :   const_TokenPtr t = lexer.peek_token ();
    4906         9822 :   switch (t->get_id ())
    4907              :     {
    4908            2 :     case IDENTIFIER:
    4909            4 :       return parse_macro_invocation_semi (outer_attrs);
    4910            1 :     case STATIC_KW:
    4911              :       {
    4912              :         // parse extern static item
    4913            1 :         lexer.skip_token ();
    4914              : 
    4915              :         // parse mut (optional)
    4916            1 :         bool has_mut = false;
    4917            2 :         if (lexer.peek_token ()->get_id () == MUT)
    4918              :           {
    4919            0 :             lexer.skip_token ();
    4920            0 :             has_mut = true;
    4921              :           }
    4922              : 
    4923              :         // parse identifier
    4924            1 :         const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4925            1 :         if (ident_tok == nullptr)
    4926              :           {
    4927            0 :             skip_after_semicolon ();
    4928            0 :             return nullptr;
    4929              :           }
    4930            2 :         Identifier ident{ident_tok};
    4931              : 
    4932            1 :         if (!skip_token (COLON))
    4933              :           {
    4934            0 :             skip_after_semicolon ();
    4935            0 :             return nullptr;
    4936              :           }
    4937              : 
    4938              :         // parse type (required)
    4939            1 :         std::unique_ptr<AST::Type> type = parse_type ();
    4940            1 :         if (type == nullptr)
    4941              :           {
    4942            0 :             Error error (lexer.peek_token ()->get_locus (),
    4943              :                          "failed to parse type in external static item");
    4944            0 :             add_error (std::move (error));
    4945              : 
    4946            0 :             skip_after_semicolon ();
    4947            0 :             return nullptr;
    4948            0 :           }
    4949              : 
    4950            1 :         if (!skip_token (SEMICOLON))
    4951              :           {
    4952              :             // skip after somewhere?
    4953            0 :             return nullptr;
    4954              :           }
    4955              : 
    4956            1 :         return std::unique_ptr<AST::ExternalStaticItem> (
    4957            2 :           new AST::ExternalStaticItem (std::move (ident), std::move (type),
    4958              :                                        has_mut, std::move (vis),
    4959            1 :                                        std::move (outer_attrs), locus));
    4960            3 :       }
    4961         9804 :     case FN_KW:
    4962        19608 :       return parse_function (std::move (vis), std::move (outer_attrs), true);
    4963              : 
    4964           15 :     case TYPE:
    4965           15 :       return parse_external_type_item (std::move (vis),
    4966           15 :                                        std::move (outer_attrs));
    4967            0 :     default:
    4968              :       // error
    4969            0 :       add_error (
    4970            0 :         Error (t->get_locus (),
    4971              :                "unrecognised token %qs in extern block item declaration",
    4972              :                t->get_token_description ()));
    4973              : 
    4974            0 :       skip_after_semicolon ();
    4975            0 :       return nullptr;
    4976              :     }
    4977        19644 : }
    4978              : 
    4979              : // Parses a statement (will further disambiguate any statement).
    4980              : template <typename ManagedTokenSource>
    4981              : std::unique_ptr<AST::Stmt>
    4982         2200 : Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
    4983              : {
    4984              :   // quick exit for empty statement
    4985              :   // FIXME: Can we have empty statements without semicolons? Just nothing?
    4986         2200 :   const_TokenPtr t = lexer.peek_token ();
    4987         2200 :   if (t->get_id () == SEMICOLON)
    4988              :     {
    4989           30 :       lexer.skip_token ();
    4990           30 :       return std::unique_ptr<AST::EmptyStmt> (
    4991           30 :         new AST::EmptyStmt (t->get_locus ()));
    4992              :     }
    4993              : 
    4994              :   // parse outer attributes
    4995         2170 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4996              : 
    4997              :   // parsing this will be annoying because of the many different possibilities
    4998              :   /* best may be just to copy paste in parse_item switch, and failing that try
    4999              :    * to parse outer attributes, and then pass them in to either a let
    5000              :    * statement or (fallback) expression statement. */
    5001              :   // FIXME: think of a way to do this without such a large switch?
    5002         2170 :   t = lexer.peek_token ();
    5003         2170 :   switch (t->get_id ())
    5004              :     {
    5005          227 :     case LET:
    5006              :       // let statement
    5007          227 :       return parse_let_stmt (std::move (outer_attrs), restrictions);
    5008          186 :     case PUB:
    5009              :     case MOD:
    5010              :     case EXTERN_KW:
    5011              :     case USE:
    5012              :     case FN_KW:
    5013              :     case TYPE:
    5014              :     case STRUCT_KW:
    5015              :     case ENUM_KW:
    5016              :     case CONST:
    5017              :     case STATIC_KW:
    5018              :     case AUTO:
    5019              :     case TRAIT:
    5020              :     case IMPL:
    5021              :     case MACRO:
    5022              :     /* TODO: implement union keyword but not really because of
    5023              :      * context-dependence crappy hack way to parse a union written below to
    5024              :      * separate it from the good code. */
    5025              :     // case UNION:
    5026              :     case UNSAFE: // maybe - unsafe traits are a thing
    5027              :       /* if any of these (should be all possible VisItem prefixes), parse a
    5028              :        * VisItem can't parse item because would require reparsing outer
    5029              :        * attributes */
    5030              :       // may also be unsafe block
    5031          372 :       if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
    5032              :         {
    5033            1 :           return parse_expr_stmt (std::move (outer_attrs), restrictions);
    5034              :         }
    5035              :       else
    5036              :         {
    5037          185 :           return parse_vis_item (std::move (outer_attrs));
    5038              :         }
    5039              :       break;
    5040              :     // crappy hack to do union "keyword"
    5041          407 :     case IDENTIFIER:
    5042          407 :       if (t->get_str () == Values::WeakKeywords::UNION
    5043          407 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
    5044              :         {
    5045            0 :           return parse_vis_item (std::move (outer_attrs));
    5046              :           // or should this go straight to parsing union?
    5047              :         }
    5048          814 :       else if (is_macro_rules_def (t))
    5049              :         {
    5050              :           // macro_rules! macro item
    5051            2 :           return parse_macro_rules_def (std::move (outer_attrs));
    5052              :         }
    5053              :       gcc_fallthrough ();
    5054              :       // TODO: find out how to disable gcc "implicit fallthrough" warning
    5055              :     default:
    5056              :       // fallback: expression statement
    5057         1755 :       return parse_expr_stmt (std::move (outer_attrs), restrictions);
    5058              :       break;
    5059              :     }
    5060         2170 : }
    5061              : 
    5062              : // Parses a let statement.
    5063              : template <typename ManagedTokenSource>
    5064              : std::unique_ptr<AST::LetStmt>
    5065        94475 : Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
    5066              :                                             ParseRestrictions restrictions)
    5067              : {
    5068        94475 :   location_t locus = lexer.peek_token ()->get_locus ();
    5069        94475 :   skip_token (LET);
    5070              : 
    5071              :   // parse pattern (required)
    5072        94475 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    5073        94475 :   if (pattern == nullptr)
    5074              :     {
    5075            0 :       Error error (lexer.peek_token ()->get_locus (),
    5076              :                    "failed to parse pattern in let statement");
    5077            0 :       add_error (std::move (error));
    5078              : 
    5079            0 :       skip_after_semicolon ();
    5080            0 :       return nullptr;
    5081            0 :     }
    5082              : 
    5083              :   // parse type declaration (optional)
    5084        94475 :   std::unique_ptr<AST::Type> type = nullptr;
    5085       188950 :   if (lexer.peek_token ()->get_id () == COLON)
    5086              :     {
    5087              :       // must have a type declaration
    5088         6065 :       lexer.skip_token ();
    5089              : 
    5090         6065 :       type = parse_type ();
    5091         6065 :       if (type == nullptr)
    5092              :         {
    5093            0 :           Error error (lexer.peek_token ()->get_locus (),
    5094              :                        "failed to parse type in let statement");
    5095            0 :           add_error (std::move (error));
    5096              : 
    5097            0 :           skip_after_semicolon ();
    5098            0 :           return nullptr;
    5099            0 :         }
    5100              :     }
    5101              : 
    5102              :   // parse expression to set variable to (optional)
    5103        94475 :   std::unique_ptr<AST::Expr> expr = nullptr;
    5104       188950 :   if (lexer.peek_token ()->get_id () == EQUAL)
    5105              :     {
    5106              :       // must have an expression
    5107        93017 :       lexer.skip_token ();
    5108              : 
    5109        93017 :       auto expr_res = parse_expr ();
    5110        93017 :       if (!expr_res)
    5111              :         {
    5112           22 :           skip_after_semicolon ();
    5113           22 :           return nullptr;
    5114              :         }
    5115        92995 :       expr = std::move (expr_res.value ());
    5116        93017 :     }
    5117              : 
    5118        94453 :   tl::optional<std::unique_ptr<AST::Expr>> else_expr = tl::nullopt;
    5119        94453 :   if (maybe_skip_token (ELSE))
    5120              :     {
    5121            5 :       auto block_expr = parse_block_expr ();
    5122            5 :       if (block_expr)
    5123              :         else_expr = tl::optional<std::unique_ptr<AST::Expr>>{
    5124           10 :           std::move (block_expr.value ())};
    5125              :       else
    5126              :         else_expr = tl::nullopt;
    5127            5 :     }
    5128              : 
    5129        94453 :   if (restrictions.consume_semi)
    5130              :     {
    5131              :       // `stmt` macro variables are parsed without a semicolon, but should be
    5132              :       // parsed as a full statement when interpolated. This should be handled
    5133              :       // by having the interpolated statement be distinguishable from normal
    5134              :       // tokens, e.g. by NT tokens.
    5135        94308 :       if (restrictions.allow_close_after_expr_stmt)
    5136           82 :         maybe_skip_token (SEMICOLON);
    5137        94226 :       else if (!skip_token (SEMICOLON))
    5138            1 :         return nullptr;
    5139              :     }
    5140              : 
    5141              :   return std::unique_ptr<AST::LetStmt> (
    5142       188909 :     new AST::LetStmt (std::move (pattern), std::move (expr), std::move (type),
    5143        94452 :                       std::move (else_expr), std::move (outer_attrs), locus));
    5144        94475 : }
    5145              : 
    5146              : template <typename ManagedTokenSource>
    5147              : tl::optional<AST::GenericArg>
    5148       129888 : Parser<ManagedTokenSource>::parse_generic_arg ()
    5149              : {
    5150       129888 :   auto tok = lexer.peek_token ();
    5151       129888 :   std::unique_ptr<AST::Expr> expr = nullptr;
    5152              : 
    5153       129888 :   switch (tok->get_id ())
    5154              :     {
    5155       101586 :     case IDENTIFIER:
    5156              :       {
    5157              :         // This is a bit of a weird situation: With an identifier token, we
    5158              :         // could either have a valid type or a macro (FIXME: anything else?). So
    5159              :         // we need one bit of lookahead to differentiate if this is really
    5160       101586 :         auto next_tok = lexer.peek_token (1);
    5161       101586 :         if (next_tok->get_id () == LEFT_ANGLE
    5162        95162 :             || next_tok->get_id () == SCOPE_RESOLUTION
    5163       195785 :             || next_tok->get_id () == EXCLAM)
    5164              :           {
    5165         7395 :             auto type = parse_type ();
    5166         7395 :             if (type)
    5167         7395 :               return AST::GenericArg::create_type (std::move (type));
    5168              :             else
    5169            0 :               return tl::nullopt;
    5170         7395 :           }
    5171        94191 :         else if (next_tok->get_id () == COLON)
    5172              :           {
    5173            0 :             lexer.skip_token (); // skip ident
    5174            0 :             lexer.skip_token (); // skip colon
    5175              : 
    5176            0 :             auto tok = lexer.peek_token ();
    5177            0 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    5178              :               = parse_type_param_bounds ();
    5179              : 
    5180            0 :             auto type = std::unique_ptr<AST::TraitObjectType> (
    5181            0 :               new AST::TraitObjectType (std::move (bounds), tok->get_locus (),
    5182              :                                         false));
    5183            0 :             if (type)
    5184            0 :               return AST::GenericArg::create_type (std::move (type));
    5185              :             else
    5186              :               return tl::nullopt;
    5187            0 :           }
    5188        94191 :         lexer.skip_token ();
    5189       376764 :         return AST::GenericArg::create_ambiguous (tok->get_str (),
    5190        94191 :                                                   tok->get_locus ());
    5191       109035 :       }
    5192           23 :     case LEFT_CURLY:
    5193              :       {
    5194           23 :         auto res = parse_block_expr ();
    5195           23 :         if (res)
    5196           23 :           expr = std::move (res.value ());
    5197              :         else
    5198            0 :           return tl::nullopt;
    5199            0 :       }
    5200           23 :       break;
    5201          100 :     case MINUS:
    5202              :     case STRING_LITERAL:
    5203              :     case CHAR_LITERAL:
    5204              :     case INT_LITERAL:
    5205              :     case FLOAT_LITERAL:
    5206              :     case TRUE_LITERAL:
    5207              :     case FALSE_LITERAL:
    5208              :       {
    5209          100 :         auto res = parse_literal_expr ();
    5210          100 :         if (res)
    5211          100 :           expr = std::move (res.value ());
    5212              :         else
    5213            0 :           return tl::nullopt;
    5214            0 :       }
    5215          100 :       break;
    5216              :     // FIXME: Because of this, error reporting is garbage for const generic
    5217              :     // parameter's default values
    5218        28179 :     default:
    5219              :       {
    5220        28179 :         auto type = parse_type ();
    5221              :         // FIXME: Find a better way to do this?
    5222        28179 :         if (type)
    5223        28178 :           return AST::GenericArg::create_type (std::move (type));
    5224              :         else
    5225            1 :           return tl::nullopt;
    5226        28179 :       }
    5227              :     }
    5228              : 
    5229          123 :   if (!expr)
    5230            0 :     return tl::nullopt;
    5231              : 
    5232          123 :   return AST::GenericArg::create_const (std::move (expr));
    5233       129888 : }
    5234              : 
    5235              : // Parses the generic arguments in each path segment.
    5236              : template <typename ManagedTokenSource>
    5237              : AST::GenericArgs
    5238       131483 : Parser<ManagedTokenSource>::parse_path_generic_args ()
    5239              : {
    5240       262966 :   if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
    5241          119 :     lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
    5242              : 
    5243       131483 :   if (!skip_token (LEFT_ANGLE))
    5244              :     {
    5245              :       // skip after somewhere?
    5246            0 :       return AST::GenericArgs::create_empty ();
    5247              :     }
    5248              : 
    5249              :   // We need to parse all lifetimes, then parse types and const generics in
    5250              :   // any order.
    5251              : 
    5252              :   // try to parse lifetimes first
    5253       131483 :   std::vector<AST::Lifetime> lifetime_args;
    5254              : 
    5255       131483 :   const_TokenPtr t = lexer.peek_token ();
    5256       131483 :   location_t locus = t->get_locus ();
    5257       267327 :   while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
    5258              :     {
    5259       135844 :       auto lifetime = parse_lifetime (false);
    5260       135844 :       if (!lifetime)
    5261              :         {
    5262              :           // not necessarily an error
    5263              :           break;
    5264              :         }
    5265              : 
    5266        11642 :       lifetime_args.push_back (std::move (lifetime.value ()));
    5267              : 
    5268              :       // if next token isn't comma, then it must be end of list
    5269        23284 :       if (lexer.peek_token ()->get_id () != COMMA)
    5270              :         {
    5271              :           break;
    5272              :         }
    5273              :       // skip comma
    5274         4362 :       lexer.skip_token ();
    5275              : 
    5276         4362 :       t = lexer.peek_token ();
    5277              :     }
    5278              : 
    5279              :   // try to parse types and const generics second
    5280       131483 :   std::vector<AST::GenericArg> generic_args;
    5281              : 
    5282              :   // TODO: think of better control structure
    5283       131483 :   t = lexer.peek_token ();
    5284       264156 :   while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
    5285              :     {
    5286              :       // FIXME: Is it fine to break if there is one binding? Can't there be
    5287              :       // bindings in between types?
    5288              : 
    5289              :       // ensure not binding being parsed as type accidently
    5290       132673 :       if (t->get_id () == IDENTIFIER
    5291       265346 :           && (lexer.peek_token (1)->get_id () == EQUAL
    5292       234898 :               || lexer.peek_token (1)->get_id () == COLON))
    5293              :         break;
    5294              : 
    5295       129864 :       auto arg = parse_generic_arg ();
    5296       129864 :       if (arg)
    5297              :         {
    5298       129864 :           generic_args.emplace_back (std::move (arg.value ()));
    5299              :         }
    5300              : 
    5301              :       // FIXME: Do we need to break if we encounter an error?
    5302              : 
    5303              :       // if next token isn't comma, then it must be end of list
    5304       259728 :       if (lexer.peek_token ()->get_id () != COMMA)
    5305              :         break;
    5306              : 
    5307              :       // skip comma
    5308         8487 :       lexer.skip_token ();
    5309         8487 :       t = lexer.peek_token ();
    5310              :     }
    5311              : 
    5312              :   // try to parse bindings third
    5313       131483 :   std::vector<AST::GenericArgsBinding> binding_args;
    5314              : 
    5315              :   // TODO: think of better control structure
    5316       131483 :   t = lexer.peek_token ();
    5317       134351 :   while (!Parse::Utils::is_right_angle_tok (t->get_id ()))
    5318              :     {
    5319         2868 :       AST::GenericArgsBinding binding = parse_generic_args_binding ();
    5320         2868 :       if (binding.is_error ())
    5321              :         {
    5322              :           // not necessarily an error
    5323              :           break;
    5324              :         }
    5325              : 
    5326         2868 :       binding_args.push_back (std::move (binding));
    5327              : 
    5328              :       // if next token isn't comma, then it must be end of list
    5329         5736 :       if (lexer.peek_token ()->get_id () != COMMA)
    5330              :         {
    5331              :           break;
    5332              :         }
    5333              :       // skip comma
    5334           59 :       lexer.skip_token ();
    5335              : 
    5336           59 :       t = lexer.peek_token ();
    5337              :     }
    5338              : 
    5339              :   // skip any trailing commas
    5340       262966 :   if (lexer.peek_token ()->get_id () == COMMA)
    5341            0 :     lexer.skip_token ();
    5342              : 
    5343       131483 :   if (!skip_generics_right_angle ())
    5344            0 :     return AST::GenericArgs::create_empty ();
    5345              : 
    5346       131483 :   lifetime_args.shrink_to_fit ();
    5347       131483 :   generic_args.shrink_to_fit ();
    5348       131483 :   binding_args.shrink_to_fit ();
    5349              : 
    5350       131483 :   return AST::GenericArgs (std::move (lifetime_args), std::move (generic_args),
    5351       131483 :                            std::move (binding_args), locus);
    5352       262966 : }
    5353              : 
    5354              : // Parses a binding in a generic args path segment.
    5355              : template <typename ManagedTokenSource>
    5356              : AST::GenericArgsBinding
    5357         2868 : Parser<ManagedTokenSource>::parse_generic_args_binding ()
    5358              : {
    5359         2868 :   const_TokenPtr ident_tok = lexer.peek_token ();
    5360         2868 :   if (ident_tok->get_id () != IDENTIFIER)
    5361              :     {
    5362              :       // allow non error-inducing use
    5363              :       // skip somewhere?
    5364            0 :       return AST::GenericArgsBinding::create_error ();
    5365              :     }
    5366         2868 :   lexer.skip_token ();
    5367         5736 :   Identifier ident{ident_tok};
    5368              : 
    5369         5736 :   if (lexer.peek_token ()->get_id () == COLON)
    5370              :     {
    5371          641 :       lexer.skip_token ();
    5372          641 :       auto bounds_locus = lexer.peek_token ()->get_locus ();
    5373          641 :       auto bounds = parse_type_param_bounds ();
    5374          641 :       auto type = std::unique_ptr<AST::Type> (
    5375          641 :         new AST::ImplTraitType (std::move (bounds), bounds_locus));
    5376         1282 :       return AST::GenericArgsBinding (
    5377              :         std::move (ident), std::move (type), ident_tok->get_locus (),
    5378         1282 :         AST::GenericArgsBinding::Kind::Constraint);
    5379          641 :     }
    5380              : 
    5381         2227 :   if (!skip_token (EQUAL))
    5382            0 :     return AST::GenericArgsBinding::create_error ();
    5383              : 
    5384              :   // parse type (required)
    5385         2227 :   std::unique_ptr<AST::Type> type = parse_type ();
    5386         2227 :   if (type == nullptr)
    5387              :     {
    5388              :       // skip somewhere?
    5389            0 :       return AST::GenericArgsBinding::create_error ();
    5390              :     }
    5391              : 
    5392         4454 :   return AST::GenericArgsBinding (std::move (ident), std::move (type),
    5393         4454 :                                   ident_tok->get_locus ());
    5394         5095 : }
    5395              : 
    5396              : // Parses a self param. Also handles self param not existing.
    5397              : template <typename ManagedTokenSource>
    5398              : tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
    5399       131181 : Parser<ManagedTokenSource>::parse_self_param ()
    5400              : {
    5401       131181 :   bool has_reference = false;
    5402       131181 :   AST::Lifetime lifetime = AST::Lifetime::elided ();
    5403              : 
    5404       131181 :   location_t locus = lexer.peek_token ()->get_locus ();
    5405              : 
    5406              :   // TODO: Feels off, find a better way to clearly express this
    5407       524724 :   std::vector<std::vector<TokenId>> ptrs
    5408              :     = {{ASTERISK, SELF} /* *self */,
    5409              :        {ASTERISK, CONST, SELF} /* *const self */,
    5410              :        {ASTERISK, MUT, SELF} /* *mut self */};
    5411              : 
    5412       524718 :   for (auto &s : ptrs)
    5413              :     {
    5414              :       size_t i = 0;
    5415       393551 :       for (i = 0; i < s.size (); i++)
    5416       787096 :         if (lexer.peek_token (i)->get_id () != s[i])
    5417              :           break;
    5418       393540 :       if (i == s.size ())
    5419              :         {
    5420            3 :           Error error (lexer.peek_token ()->get_locus (),
    5421              :                        "cannot pass %<self%> by raw pointer");
    5422            3 :           add_error (std::move (error));
    5423            3 :           return Parse::Error::Self::make_self_raw_pointer ();
    5424            3 :         }
    5425              :     }
    5426              : 
    5427              :   // Trying to find those patterns:
    5428              :   //
    5429              :   // &'lifetime mut self
    5430              :   // &'lifetime self
    5431              :   // & mut self
    5432              :   // & self
    5433              :   // mut self
    5434              :   // self
    5435              :   //
    5436              :   // If not found, it is probably a function, exit and let function parsing
    5437              :   // handle it.
    5438              :   bool is_self = false;
    5439       787068 :   for (size_t i = 0; i < 5; i++)
    5440      1311780 :     if (lexer.peek_token (i)->get_id () == SELF)
    5441        71905 :       is_self = true;
    5442              : 
    5443       131178 :   if (!is_self)
    5444        59278 :     return Parse::Error::Self::make_not_self ();
    5445              : 
    5446              :   // test if self is a reference parameter
    5447       143800 :   if (lexer.peek_token ()->get_id () == AMP)
    5448              :     {
    5449        36816 :       has_reference = true;
    5450        36816 :       lexer.skip_token ();
    5451              : 
    5452              :       // now test whether it has a lifetime
    5453        73632 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    5454              :         {
    5455              :           // something went wrong somehow
    5456          708 :           if (auto parsed_lifetime = parse_lifetime (true))
    5457              :             {
    5458          354 :               lifetime = parsed_lifetime.value ();
    5459              :             }
    5460              :           else
    5461              :             {
    5462            0 :               Error error (lexer.peek_token ()->get_locus (),
    5463              :                            "failed to parse lifetime in self param");
    5464            0 :               add_error (std::move (error));
    5465              : 
    5466              :               // skip after somewhere?
    5467            0 :               return Parse::Error::Self::make_parsing_error ();
    5468            0 :             }
    5469              :         }
    5470              :     }
    5471              : 
    5472              :   // test for mut
    5473        71900 :   bool has_mut = false;
    5474       143800 :   if (lexer.peek_token ()->get_id () == MUT)
    5475              :     {
    5476        15612 :       has_mut = true;
    5477        15612 :       lexer.skip_token ();
    5478              :     }
    5479              : 
    5480              :   // skip self token
    5481        71900 :   const_TokenPtr self_tok = lexer.peek_token ();
    5482        71900 :   if (self_tok->get_id () != SELF)
    5483              :     {
    5484              :       // skip after somewhere?
    5485            4 :       return Parse::Error::Self::make_not_self ();
    5486              :     }
    5487        71896 :   lexer.skip_token ();
    5488              : 
    5489              :   // parse optional type
    5490        71896 :   std::unique_ptr<AST::Type> type = nullptr;
    5491       143792 :   if (lexer.peek_token ()->get_id () == COLON)
    5492              :     {
    5493          119 :       lexer.skip_token ();
    5494              : 
    5495              :       // type is now required
    5496          119 :       type = parse_type ();
    5497          119 :       if (type == nullptr)
    5498              :         {
    5499            0 :           Error error (lexer.peek_token ()->get_locus (),
    5500              :                        "could not parse type in self param");
    5501            0 :           add_error (std::move (error));
    5502              : 
    5503              :           // skip after somewhere?
    5504            0 :           return Parse::Error::Self::make_parsing_error ();
    5505            0 :         }
    5506              :     }
    5507              : 
    5508              :   // ensure that cannot have both type and reference
    5509        71896 :   if (type && has_reference)
    5510              :     {
    5511            0 :       Error error (
    5512            0 :         lexer.peek_token ()->get_locus (),
    5513              :         "cannot have both a reference and a type specified in a self param");
    5514            0 :       add_error (std::move (error));
    5515              : 
    5516              :       // skip after somewhere?
    5517            0 :       return Parse::Error::Self::make_parsing_error ();
    5518            0 :     }
    5519              : 
    5520        71896 :   if (has_reference)
    5521              :     {
    5522        36816 :       return std::make_unique<AST::SelfParam> (std::move (lifetime), has_mut,
    5523        36816 :                                                locus);
    5524              :     }
    5525              :   else
    5526              :     {
    5527              :       // note that type may be nullptr here and that's fine
    5528        35080 :       return std::make_unique<AST::SelfParam> (std::move (type), has_mut,
    5529        35080 :                                                locus);
    5530              :     }
    5531       203077 : }
    5532              : 
    5533              : /* Parses an expression or macro statement. */
    5534              : template <typename ManagedTokenSource>
    5535              : std::unique_ptr<AST::Stmt>
    5536         1756 : Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
    5537              :                                              ParseRestrictions restrictions)
    5538              : {
    5539         1756 :   location_t locus = lexer.peek_token ()->get_locus ();
    5540              : 
    5541         1756 :   tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr;
    5542              : 
    5543         3512 :   switch (lexer.peek_token ()->get_id ())
    5544              :     {
    5545          439 :     case IDENTIFIER:
    5546              :     case CRATE:
    5547              :     case SUPER:
    5548              :     case SELF:
    5549              :     case SELF_ALIAS:
    5550              :     case DOLLAR_SIGN:
    5551              :     case SCOPE_RESOLUTION:
    5552              :       {
    5553          439 :         AST::PathInExpression path = parse_path_in_expression ();
    5554              :         tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
    5555          439 :           null_denotation;
    5556              : 
    5557          878 :         if (lexer.peek_token ()->get_id () == EXCLAM)
    5558              :           {
    5559           61 :             std::unique_ptr<AST::MacroInvocation> invoc
    5560          122 :               = parse_macro_invocation_partial (std::move (path),
    5561              :                                                 std::move (outer_attrs));
    5562              : 
    5563           61 :             if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
    5564              :               {
    5565           59 :                 invoc->add_semicolon ();
    5566              :                 // Macro invocation with semicolon.
    5567           59 :                 return invoc;
    5568              :               }
    5569              : 
    5570            2 :             TokenId after_macro = lexer.peek_token ()->get_id ();
    5571              : 
    5572            2 :             if (restrictions.allow_close_after_expr_stmt
    5573            2 :                 && (after_macro == RIGHT_PAREN || after_macro == RIGHT_CURLY
    5574              :                     || after_macro == RIGHT_SQUARE))
    5575            2 :               return invoc;
    5576              : 
    5577            0 :             if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
    5578              :                   == AST::CURLY
    5579            0 :                 && after_macro != DOT && after_macro != QUESTION_MARK)
    5580              :               {
    5581            0 :                 rust_debug ("braced macro statement");
    5582            0 :                 return invoc;
    5583              :               }
    5584              : 
    5585            0 :             null_denotation = std::move (invoc);
    5586           61 :           }
    5587              :         else
    5588              :           {
    5589              :             null_denotation
    5590          756 :               = null_denotation_path (std::move (path), {}, restrictions);
    5591              :           }
    5592              : 
    5593         1512 :         expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
    5594              :                                  std::move (outer_attrs), restrictions);
    5595              :         break;
    5596          439 :       }
    5597         1317 :     default:
    5598         1317 :       restrictions.expr_can_be_stmt = true;
    5599         2634 :       expr = parse_expr (std::move (outer_attrs), restrictions);
    5600         1317 :       break;
    5601              :     }
    5602              : 
    5603         1695 :   if (!expr)
    5604              :     {
    5605              :       // expr is required, error
    5606            0 :       Error error (lexer.peek_token ()->get_locus (),
    5607              :                    "failed to parse expr in expr statement");
    5608            0 :       add_error (std::move (error));
    5609              : 
    5610            0 :       skip_after_semicolon ();
    5611            0 :       return nullptr;
    5612            0 :     }
    5613              : 
    5614         1695 :   bool has_semi = false;
    5615              : 
    5616         1695 :   if (restrictions.consume_semi)
    5617              :     {
    5618         1679 :       if (maybe_skip_token (SEMICOLON))
    5619              :         {
    5620          194 :           has_semi = true;
    5621              :         }
    5622         1485 :       else if (expr.value ()->is_expr_without_block ())
    5623              :         {
    5624          174 :           if (restrictions.allow_close_after_expr_stmt)
    5625              :             {
    5626          174 :               TokenId id = lexer.peek_token ()->get_id ();
    5627          174 :               if (id != RIGHT_PAREN && id != RIGHT_CURLY && id != RIGHT_SQUARE)
    5628              :                 {
    5629            3 :                   expect_token (SEMICOLON);
    5630            3 :                   return nullptr;
    5631              :                 }
    5632              :             }
    5633              :           else
    5634              :             {
    5635            0 :               expect_token (SEMICOLON);
    5636            0 :               return nullptr;
    5637              :             }
    5638              :         }
    5639              :     }
    5640              : 
    5641         1692 :   return std::make_unique<AST::ExprStmt> (std::move (expr.value ()), locus,
    5642         1692 :                                           has_semi);
    5643         1756 : }
    5644              : 
    5645              : // Parses a loop label used in loop expressions.
    5646              : template <typename ManagedTokenSource>
    5647              : tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
    5648           75 : Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
    5649              : {
    5650              :   // parse lifetime - if doesn't exist, assume no label
    5651           75 :   if (tok->get_id () != LIFETIME)
    5652              :     {
    5653              :       // not necessarily an error
    5654            0 :       return Parse::Error::LoopLabel::make_not_loop_label ();
    5655              :     }
    5656              :   /* FIXME: check for named lifetime requirement here? or check in semantic
    5657              :    * analysis phase? */
    5658          150 :   AST::Lifetime label = lifetime_from_token (tok);
    5659              : 
    5660           75 :   if (!skip_token (COLON))
    5661              :     {
    5662              :       // skip somewhere?
    5663           75 :       Parse::Error::LoopLabel::make_missing_colon ();
    5664              :     }
    5665              : 
    5666              :   return tl::expected<AST::LoopLabel, Parse::Error::LoopLabel> (
    5667           75 :     AST::LoopLabel (std::move (label), tok->get_locus ()));
    5668           75 : }
    5669              : 
    5670              : // Parses the "pattern" part of the match arm (the 'case x:' equivalent).
    5671              : template <typename ManagedTokenSource>
    5672              : AST::MatchArm
    5673       385013 : Parser<ManagedTokenSource>::parse_match_arm ()
    5674              : {
    5675              :   // parse optional outer attributes
    5676       385013 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5677              : 
    5678              :   // DEBUG
    5679       385013 :   rust_debug ("about to start parsing match arm patterns");
    5680              : 
    5681              :   // break early if find right curly
    5682       770026 :   if (lexer.peek_token ()->get_id () == RIGHT_CURLY)
    5683              :     {
    5684              :       // not an error
    5685            0 :       return AST::MatchArm::create_error ();
    5686              :     }
    5687              : 
    5688              :   // parse match arm patterns - at least 1 is required
    5689       385013 :   std::unique_ptr<AST::Pattern> match_arm_pattern
    5690              :     = parse_match_arm_pattern (RIGHT_CURLY);
    5691       385013 :   if (match_arm_pattern == nullptr)
    5692              :     {
    5693            0 :       Error error (lexer.peek_token ()->get_locus (),
    5694              :                    "failed to parse any patterns in match arm");
    5695            0 :       add_error (std::move (error));
    5696              : 
    5697              :       // skip somewhere?
    5698            0 :       return AST::MatchArm::create_error ();
    5699            0 :     }
    5700              : 
    5701              :   // DEBUG
    5702       385013 :   rust_debug ("successfully parsed match arm patterns");
    5703              : 
    5704              :   // parse match arm guard expr if it exists
    5705       385013 :   std::unique_ptr<AST::Expr> guard_expr = nullptr;
    5706       770026 :   if (lexer.peek_token ()->get_id () == IF)
    5707              :     {
    5708          335 :       lexer.skip_token ();
    5709              : 
    5710          335 :       auto guard_expr_res = parse_expr ();
    5711          335 :       if (!guard_expr_res)
    5712              :         {
    5713            0 :           Error error (lexer.peek_token ()->get_locus (),
    5714              :                        "failed to parse guard expression in match arm");
    5715            0 :           add_error (std::move (error));
    5716              : 
    5717              :           // skip somewhere?
    5718            0 :           return AST::MatchArm::create_error ();
    5719            0 :         }
    5720          335 :       guard_expr = std::move (guard_expr_res.value ());
    5721          335 :     }
    5722              : 
    5723              :   // DEBUG
    5724       385013 :   rust_debug ("successfully parsed match arm");
    5725              : 
    5726       770026 :   return AST::MatchArm (std::move (match_arm_pattern),
    5727       770026 :                         lexer.peek_token ()->get_locus (),
    5728       385013 :                         std::move (guard_expr), std::move (outer_attrs));
    5729       385013 : }
    5730              : 
    5731              : /* Parses the patterns used in a match arm. End token id is the id of the
    5732              :  * token that would exist after the patterns are done (e.g. '}' for match
    5733              :  * expr, '=' for if let and while let). */
    5734              : template <typename ManagedTokenSource>
    5735              : std::unique_ptr<AST::Pattern>
    5736       386040 : Parser<ManagedTokenSource>::parse_match_arm_pattern (TokenId end_token_id)
    5737              : {
    5738              :   // skip optional leading '|'
    5739       772080 :   if (lexer.peek_token ()->get_id () == PIPE)
    5740            0 :     lexer.skip_token ();
    5741              :   /* TODO: do I even need to store the result of this? can't be used.
    5742              :    * If semantically different, I need a wrapped "match arm patterns" object
    5743              :    * for this. */
    5744              : 
    5745       386040 :   std::unique_ptr<AST::Pattern> pattern;
    5746              : 
    5747              :   // quick break out if end_token_id
    5748       772080 :   if (lexer.peek_token ()->get_id () == end_token_id)
    5749            1 :     return pattern;
    5750              : 
    5751              :   // parse required pattern - if doesn't exist, return empty
    5752       386039 :   std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
    5753       386039 :   if (initial_pattern == nullptr)
    5754              :     {
    5755              :       // FIXME: should this be an error?
    5756            0 :       return pattern;
    5757              :     }
    5758              : 
    5759       386039 :   return initial_pattern;
    5760       386040 : }
    5761              : 
    5762              : // Parses a single parameter used in a closure definition.
    5763              : template <typename ManagedTokenSource>
    5764              : AST::ClosureParam
    5765         3187 : Parser<ManagedTokenSource>::parse_closure_param ()
    5766              : {
    5767         3187 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5768              : 
    5769              :   // parse pattern (which is required)
    5770         3187 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
    5771         3187 :   if (pattern == nullptr)
    5772              :     {
    5773              :       // not necessarily an error
    5774            0 :       return AST::ClosureParam::create_error ();
    5775              :     }
    5776              : 
    5777              :   // parse optional type of param
    5778         3187 :   std::unique_ptr<AST::Type> type = nullptr;
    5779         6374 :   if (lexer.peek_token ()->get_id () == COLON)
    5780              :     {
    5781          207 :       lexer.skip_token ();
    5782              : 
    5783              :       // parse type, which is now required
    5784          207 :       type = parse_type ();
    5785          207 :       if (type == nullptr)
    5786              :         {
    5787            0 :           Error error (lexer.peek_token ()->get_locus (),
    5788              :                        "failed to parse type in closure parameter");
    5789            0 :           add_error (std::move (error));
    5790              : 
    5791              :           // skip somewhere?
    5792            0 :           return AST::ClosureParam::create_error ();
    5793            0 :         }
    5794              :     }
    5795              : 
    5796         3187 :   location_t loc = pattern->get_locus ();
    5797         3187 :   return AST::ClosureParam (std::move (pattern), loc, std::move (type),
    5798         3187 :                             std::move (outer_attrs));
    5799         3187 : }
    5800              : 
    5801              : // Parses a type (will further disambiguate any type).
    5802              : template <typename ManagedTokenSource>
    5803              : std::unique_ptr<AST::Type>
    5804       555247 : Parser<ManagedTokenSource>::parse_type (bool save_errors)
    5805              : {
    5806              :   /* rules for all types:
    5807              :    * NeverType:               '!'
    5808              :    * SliceType:               '[' Type ']'
    5809              :    * InferredType:            '_'
    5810              :    * MacroInvocation:         SimplePath '!' DelimTokenTree
    5811              :    * ParenthesisedType:       '(' Type ')'
    5812              :    * ImplTraitType:           'impl' TypeParamBounds
    5813              :    *  TypeParamBounds (not type)  TypeParamBound ( '+' TypeParamBound )* '+'?
    5814              :    *  TypeParamBound          Lifetime | TraitBound
    5815              :    * ImplTraitTypeOneBound:   'impl' TraitBound
    5816              :    * TraitObjectType:         'dyn'? TypeParamBounds
    5817              :    * TraitObjectTypeOneBound: 'dyn'? TraitBound
    5818              :    *  TraitBound              '?'? ForLifetimes? TypePath | '(' '?'?
    5819              :    * ForLifetimes? TypePath ')' BareFunctionType:        ForLifetimes?
    5820              :    * FunctionQualifiers 'fn' etc. ForLifetimes (not type) 'for' '<'
    5821              :    * LifetimeParams '>' FunctionQualifiers      ( 'async' | 'const' )?
    5822              :    * 'unsafe'?
    5823              :    * ('extern' abi?)? QualifiedPathInType:     '<' Type ( 'as' TypePath )? '>'
    5824              :    * (
    5825              :    * '::' TypePathSegment )+ TypePath:                '::'? TypePathSegment (
    5826              :    * '::' TypePathSegment)* ArrayType:               '[' Type ';' Expr ']'
    5827              :    * ReferenceType:           '&' Lifetime? 'mut'? TypeNoBounds
    5828              :    * RawPointerType:          '*' ( 'mut' | 'const' ) TypeNoBounds
    5829              :    * TupleType:               '(' Type etc. - regular tuple stuff. Also
    5830              :    * regular tuple vs parenthesised precedence
    5831              :    *
    5832              :    * Disambiguate between macro and type path via type path being parsed, and
    5833              :    * then if '!' found, convert type path to simple path for macro. Usual
    5834              :    * disambiguation for tuple vs parenthesised. For ImplTraitType and
    5835              :    * TraitObjectType individual disambiguations, they seem more like "special
    5836              :    * cases", so probably just try to parse the more general ImplTraitType or
    5837              :    * TraitObjectType and return OneBound versions if they satisfy those
    5838              :    * criteria. */
    5839              : 
    5840       555247 :   const_TokenPtr t = lexer.peek_token ();
    5841       555247 :   switch (t->get_id ())
    5842              :     {
    5843          624 :     case EXCLAM:
    5844              :       // never type - can't be macro as no path beforehand
    5845          624 :       lexer.skip_token ();
    5846          624 :       return std::unique_ptr<AST::NeverType> (
    5847          624 :         new AST::NeverType (t->get_locus ()));
    5848         3848 :     case LEFT_SQUARE:
    5849              :       // slice type or array type - requires further disambiguation
    5850         3848 :       return parse_slice_or_array_type ();
    5851        26163 :     case LEFT_SHIFT:
    5852              :     case LEFT_ANGLE:
    5853              :       {
    5854              :         // qualified path in type
    5855        26163 :         AST::QualifiedPathInType path = parse_qualified_path_in_type ();
    5856        26163 :         if (path.is_error ())
    5857              :           {
    5858            0 :             if (save_errors)
    5859              :               {
    5860            0 :                 Error error (t->get_locus (),
    5861              :                              "failed to parse qualified path in type");
    5862            0 :                 add_error (std::move (error));
    5863            0 :               }
    5864              : 
    5865            0 :             return nullptr;
    5866              :           }
    5867        26163 :         return std::unique_ptr<AST::QualifiedPathInType> (
    5868        26163 :           new AST::QualifiedPathInType (std::move (path)));
    5869        26163 :       }
    5870         1814 :     case UNDERSCORE:
    5871              :       // inferred type
    5872         1814 :       lexer.skip_token ();
    5873         1814 :       return std::unique_ptr<AST::InferredType> (
    5874         1814 :         new AST::InferredType (t->get_locus ()));
    5875         9106 :     case ASTERISK:
    5876              :       // raw pointer type
    5877         9106 :       return parse_raw_pointer_type ();
    5878        64395 :     case AMP: // does this also include AMP_AMP?
    5879              :     case LOGICAL_AND:
    5880              :       // reference type
    5881        64395 :       return parse_reference_type ();
    5882            0 :     case LIFETIME:
    5883              :       {
    5884              :         /* probably a lifetime bound, so probably type param bounds in
    5885              :          * TraitObjectType */
    5886            0 :         std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    5887              :           = parse_type_param_bounds ();
    5888              : 
    5889            0 :         return std::unique_ptr<AST::TraitObjectType> (
    5890            0 :           new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    5891            0 :                                     false));
    5892            0 :       }
    5893       437097 :     case IDENTIFIER:
    5894              :     case SUPER:
    5895              :     case SELF:
    5896              :     case SELF_ALIAS:
    5897              :     case CRATE:
    5898              :     case DOLLAR_SIGN:
    5899              :     case SCOPE_RESOLUTION:
    5900              :       {
    5901              :         // macro invocation or type path - requires further disambiguation.
    5902              :         /* for parsing path component of each rule, perhaps parse it as a
    5903              :          * typepath and attempt conversion to simplepath if a trailing '!' is
    5904              :          * found */
    5905              :         /* Type path also includes TraitObjectTypeOneBound BUT if it starts
    5906              :          * with it, it is exactly the same as a TypePath syntactically, so
    5907              :          * this is a syntactical ambiguity. As such, the parser will parse it
    5908              :          * as a TypePath. This, however, does not prevent TraitObjectType from
    5909              :          * starting with a typepath. */
    5910              : 
    5911              :         // parse path as type path
    5912       437097 :         AST::TypePath path = parse_type_path ();
    5913       437097 :         if (path.is_error ())
    5914              :           {
    5915            0 :             if (save_errors)
    5916              :               {
    5917            0 :                 Error error (t->get_locus (),
    5918              :                              "failed to parse path as first component of type");
    5919            0 :                 add_error (std::move (error));
    5920            0 :               }
    5921              : 
    5922            0 :             return nullptr;
    5923              :           }
    5924       437097 :         location_t locus = path.get_locus ();
    5925              : 
    5926              :         // branch on next token
    5927       437097 :         t = lexer.peek_token ();
    5928       437097 :         switch (t->get_id ())
    5929              :           {
    5930         3773 :           case EXCLAM:
    5931              :             {
    5932              :               // macro invocation
    5933              :               // convert to simple path
    5934         3773 :               AST::SimplePath macro_path = path.as_simple_path ();
    5935         3773 :               if (macro_path.is_empty ())
    5936              :                 {
    5937            0 :                   if (save_errors)
    5938              :                     {
    5939            0 :                       Error error (t->get_locus (),
    5940              :                                    "failed to parse simple path in macro "
    5941              :                                    "invocation (for type)");
    5942            0 :                       add_error (std::move (error));
    5943            0 :                     }
    5944              : 
    5945            0 :                   return nullptr;
    5946              :                 }
    5947              : 
    5948         3773 :               lexer.skip_token ();
    5949              : 
    5950         3773 :               auto tok_tree = parse_delim_token_tree ();
    5951         3773 :               if (!tok_tree)
    5952            0 :                 return nullptr;
    5953              : 
    5954         7546 :               return AST::MacroInvocation::Regular (
    5955         7546 :                 AST::MacroInvocData (std::move (macro_path),
    5956         3773 :                                      std::move (tok_tree.value ())),
    5957         3773 :                 {}, locus);
    5958         7546 :             }
    5959          232 :           case PLUS:
    5960              :             {
    5961              :               // type param bounds
    5962          232 :               std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    5963              : 
    5964              :               // convert type path to trait bound
    5965          232 :               std::unique_ptr<AST::TraitBound> path_bound (
    5966          232 :                 new AST::TraitBound (std::move (path), locus, false, false));
    5967          232 :               bounds.push_back (std::move (path_bound));
    5968              : 
    5969              :               /* parse rest of bounds - FIXME: better way to find when to stop
    5970              :                * parsing */
    5971          464 :               while (t->get_id () == PLUS)
    5972              :                 {
    5973          232 :                   lexer.skip_token ();
    5974              : 
    5975              :                   // parse bound if it exists - if not, assume end of sequence
    5976          232 :                   std::unique_ptr<AST::TypeParamBound> bound
    5977              :                     = parse_type_param_bound ();
    5978          232 :                   if (bound == nullptr)
    5979              :                     {
    5980              :                       break;
    5981              :                     }
    5982          232 :                   bounds.push_back (std::move (bound));
    5983              : 
    5984          232 :                   t = lexer.peek_token ();
    5985              :                 }
    5986              : 
    5987          232 :               return std::unique_ptr<AST::TraitObjectType> (
    5988          232 :                 new AST::TraitObjectType (std::move (bounds), locus, false));
    5989          232 :             }
    5990       433092 :           default:
    5991              :             // assume that this is a type path and not an error
    5992       433092 :             return std::unique_ptr<AST::TypePath> (
    5993       433092 :               new AST::TypePath (std::move (path)));
    5994              :           }
    5995       437097 :       }
    5996         5966 :     case LEFT_PAREN:
    5997              :       /* tuple type or parenthesised type - requires further disambiguation
    5998              :        * (the usual). ok apparently can be a parenthesised TraitBound too, so
    5999              :        * could be TraitObjectTypeOneBound or TraitObjectType */
    6000         5966 :       return parse_paren_prefixed_type ();
    6001           12 :     case FOR:
    6002              :       // TraitObjectTypeOneBound or BareFunctionType
    6003           12 :       return parse_for_prefixed_type ();
    6004         5056 :     case ASYNC:
    6005              :     case CONST:
    6006              :     case UNSAFE:
    6007              :     case EXTERN_KW:
    6008              :     case FN_KW:
    6009              :       // bare function type (with no for lifetimes)
    6010         5056 :       return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
    6011          985 :     case IMPL:
    6012          985 :       lexer.skip_token ();
    6013         1970 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    6014              :         {
    6015              :           /* cannot be one bound because lifetime prevents it from being
    6016              :            * traitbound */
    6017            0 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    6018              :             = parse_type_param_bounds ();
    6019              : 
    6020            0 :           return std::unique_ptr<AST::ImplTraitType> (
    6021            0 :             new AST::ImplTraitType (std::move (bounds), t->get_locus ()));
    6022            0 :         }
    6023              :       else
    6024              :         {
    6025              :           // should be trait bound, so parse trait bound
    6026          985 :           std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    6027          985 :           if (initial_bound == nullptr)
    6028              :             {
    6029            0 :               if (save_errors)
    6030              :                 {
    6031            0 :                   Error error (lexer.peek_token ()->get_locus (),
    6032              :                                "failed to parse ImplTraitType initial bound");
    6033            0 :                   add_error (std::move (error));
    6034            0 :                 }
    6035              : 
    6036            0 :               return nullptr;
    6037              :             }
    6038              : 
    6039          985 :           location_t locus = t->get_locus ();
    6040              : 
    6041              :           // short cut if next token isn't '+'
    6042          985 :           t = lexer.peek_token ();
    6043          985 :           if (t->get_id () != PLUS)
    6044              :             {
    6045          967 :               return std::unique_ptr<AST::ImplTraitTypeOneBound> (
    6046          967 :                 new AST::ImplTraitTypeOneBound (std::move (initial_bound),
    6047          967 :                                                 locus));
    6048              :             }
    6049              : 
    6050              :           // parse additional type param bounds
    6051           18 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    6052           18 :           bounds.push_back (std::move (initial_bound));
    6053           36 :           while (t->get_id () == PLUS)
    6054              :             {
    6055           18 :               lexer.skip_token ();
    6056              : 
    6057              :               // parse bound if it exists
    6058           18 :               std::unique_ptr<AST::TypeParamBound> bound
    6059              :                 = parse_type_param_bound ();
    6060           18 :               if (bound == nullptr)
    6061              :                 {
    6062              :                   // not an error as trailing plus may exist
    6063              :                   break;
    6064              :                 }
    6065           18 :               bounds.push_back (std::move (bound));
    6066              : 
    6067           18 :               t = lexer.peek_token ();
    6068              :             }
    6069              : 
    6070           18 :           return std::unique_ptr<AST::ImplTraitType> (
    6071           18 :             new AST::ImplTraitType (std::move (bounds), locus));
    6072          985 :         }
    6073          165 :     case DYN:
    6074              :     case QUESTION_MARK:
    6075              :       {
    6076              :         // either TraitObjectType or TraitObjectTypeOneBound
    6077          165 :         bool has_dyn = false;
    6078          165 :         if (t->get_id () == DYN)
    6079              :           {
    6080          165 :             lexer.skip_token ();
    6081          165 :             has_dyn = true;
    6082              :           }
    6083              : 
    6084          330 :         if (lexer.peek_token ()->get_id () == LIFETIME)
    6085              :           {
    6086              :             /* cannot be one bound because lifetime prevents it from being
    6087              :              * traitbound */
    6088            0 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    6089              :               = parse_type_param_bounds ();
    6090              : 
    6091            0 :             return std::unique_ptr<AST::TraitObjectType> (
    6092            0 :               new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    6093            0 :                                         has_dyn));
    6094            0 :           }
    6095              :         else
    6096              :           {
    6097              :             // should be trait bound, so parse trait bound
    6098          165 :             std::unique_ptr<AST::TraitBound> initial_bound
    6099              :               = parse_trait_bound ();
    6100          165 :             if (initial_bound == nullptr)
    6101              :               {
    6102            2 :                 if (save_errors)
    6103              :                   {
    6104            2 :                     Error error (
    6105            2 :                       lexer.peek_token ()->get_locus (),
    6106              :                       "failed to parse TraitObjectType initial bound");
    6107            2 :                     add_error (std::move (error));
    6108            2 :                   }
    6109              : 
    6110            2 :                 return nullptr;
    6111              :               }
    6112              : 
    6113              :             // short cut if next token isn't '+'
    6114          163 :             t = lexer.peek_token ();
    6115          163 :             if (t->get_id () != PLUS)
    6116              :               {
    6117              :                 // convert trait bound to value object
    6118           41 :                 AST::TraitBound value_bound (*initial_bound);
    6119              : 
    6120              :                 // DEBUG: removed as unique ptr, so should auto delete
    6121              :                 // delete initial_bound;
    6122              : 
    6123           41 :                 return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    6124           82 :                   new AST::TraitObjectTypeOneBound (std::move (value_bound),
    6125           41 :                                                     t->get_locus (), has_dyn));
    6126           41 :               }
    6127              : 
    6128              :             // parse additional type param bounds
    6129          122 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    6130          122 :             bounds.push_back (std::move (initial_bound));
    6131          268 :             while (t->get_id () == PLUS)
    6132              :               {
    6133          146 :                 lexer.skip_token ();
    6134              : 
    6135              :                 // parse bound if it exists
    6136          146 :                 std::unique_ptr<AST::TypeParamBound> bound
    6137              :                   = parse_type_param_bound ();
    6138          146 :                 if (bound == nullptr)
    6139              :                   {
    6140              :                     // not an error as trailing plus may exist
    6141              :                     break;
    6142              :                   }
    6143          146 :                 bounds.push_back (std::move (bound));
    6144              : 
    6145          146 :                 t = lexer.peek_token ();
    6146              :               }
    6147              : 
    6148          122 :             return std::unique_ptr<AST::TraitObjectType> (
    6149          122 :               new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    6150          122 :                                         has_dyn));
    6151          165 :           }
    6152              :       }
    6153           16 :     default:
    6154           16 :       if (save_errors)
    6155           16 :         add_error (Error (t->get_locus (), "unrecognised token %qs in type",
    6156              :                           t->get_token_description ()));
    6157              : 
    6158           16 :       return nullptr;
    6159              :     }
    6160       555247 : }
    6161              : 
    6162              : /* Parses a type that has '(' as its first character. Returns a tuple type,
    6163              :  * parenthesised type, TraitObjectTypeOneBound, or TraitObjectType depending
    6164              :  * on following characters. */
    6165              : template <typename ManagedTokenSource>
    6166              : std::unique_ptr<AST::Type>
    6167         5966 : Parser<ManagedTokenSource>::parse_paren_prefixed_type ()
    6168              : {
    6169              :   /* NOTE: Syntactical ambiguity of a parenthesised trait bound is considered
    6170              :    * a trait bound, not a parenthesised type, so that it can still be used in
    6171              :    * type param bounds. */
    6172              : 
    6173              :   /* NOTE: this implementation is really shit but I couldn't think of a better
    6174              :    * one. It requires essentially breaking polymorphism and downcasting via
    6175              :    * virtual method abuse, as it was copied from the rustc implementation (in
    6176              :    * which types are reified due to tagged union), after a more OOP attempt by
    6177              :    * me failed. */
    6178         5966 :   location_t left_delim_locus = lexer.peek_token ()->get_locus ();
    6179              : 
    6180              :   // skip left delim
    6181         5966 :   lexer.skip_token ();
    6182              :   /* while next token isn't close delim, parse comma-separated types, saving
    6183              :    * whether trailing comma happens */
    6184         5966 :   const_TokenPtr t = lexer.peek_token ();
    6185         5966 :   bool trailing_comma = true;
    6186         5966 :   std::vector<std::unique_ptr<AST::Type>> types;
    6187              : 
    6188        15545 :   while (t->get_id () != RIGHT_PAREN)
    6189              :     {
    6190        13534 :       std::unique_ptr<AST::Type> type = parse_type ();
    6191        13534 :       if (type == nullptr)
    6192              :         {
    6193            0 :           Error error (t->get_locus (),
    6194              :                        "failed to parse type inside parentheses (probably "
    6195              :                        "tuple or parenthesised)");
    6196            0 :           add_error (std::move (error));
    6197              : 
    6198            0 :           return nullptr;
    6199            0 :         }
    6200        13534 :       types.push_back (std::move (type));
    6201              : 
    6202        13534 :       t = lexer.peek_token ();
    6203        13534 :       if (t->get_id () != COMMA)
    6204              :         {
    6205         3955 :           trailing_comma = false;
    6206              :           break;
    6207              :         }
    6208         9579 :       lexer.skip_token ();
    6209              : 
    6210         9579 :       t = lexer.peek_token ();
    6211              :     }
    6212              : 
    6213         5966 :   if (!skip_token (RIGHT_PAREN))
    6214              :     {
    6215            0 :       return nullptr;
    6216              :     }
    6217              : 
    6218              :   // if only one type and no trailing comma, then not a tuple type
    6219         5966 :   if (types.size () == 1 && !trailing_comma)
    6220              :     {
    6221              :       // must be a TraitObjectType (with more than one bound)
    6222           10 :       if (lexer.peek_token ()->get_id () == PLUS)
    6223              :         {
    6224              :           // create type param bounds vector
    6225            0 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    6226              : 
    6227              :           // HACK: convert type to traitbound and add to bounds
    6228            0 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
    6229            0 :           std::unique_ptr<AST::TraitBound> converted_bound (
    6230            0 :             released_ptr->to_trait_bound (true));
    6231            0 :           if (converted_bound == nullptr)
    6232              :             {
    6233            0 :               Error error (
    6234            0 :                 lexer.peek_token ()->get_locus (),
    6235              :                 "failed to hackily converted parsed type to trait bound");
    6236            0 :               add_error (std::move (error));
    6237              : 
    6238            0 :               return nullptr;
    6239            0 :             }
    6240            0 :           bounds.push_back (std::move (converted_bound));
    6241              : 
    6242            0 :           t = lexer.peek_token ();
    6243            0 :           while (t->get_id () == PLUS)
    6244              :             {
    6245            0 :               lexer.skip_token ();
    6246              : 
    6247              :               // attempt to parse typeparambound
    6248            0 :               std::unique_ptr<AST::TypeParamBound> bound
    6249              :                 = parse_type_param_bound ();
    6250            0 :               if (bound == nullptr)
    6251              :                 {
    6252              :                   // not an error if null
    6253              :                   break;
    6254              :                 }
    6255            0 :               bounds.push_back (std::move (bound));
    6256              : 
    6257            0 :               t = lexer.peek_token ();
    6258              :             }
    6259              : 
    6260            0 :           return std::unique_ptr<AST::TraitObjectType> (
    6261            0 :             new AST::TraitObjectType (std::move (bounds), left_delim_locus,
    6262            0 :                                       false));
    6263            0 :         }
    6264              :       else
    6265              :         {
    6266              :           // release vector pointer
    6267            5 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
    6268              :           /* HACK: attempt to convert to trait bound. if fails, parenthesised
    6269              :            * type */
    6270            5 :           std::unique_ptr<AST::TraitBound> converted_bound (
    6271            5 :             released_ptr->to_trait_bound (true));
    6272            5 :           if (converted_bound == nullptr)
    6273              :             {
    6274              :               // parenthesised type
    6275            5 :               return std::unique_ptr<AST::ParenthesisedType> (
    6276            5 :                 new AST::ParenthesisedType (std::move (released_ptr),
    6277            5 :                                             left_delim_locus));
    6278              :             }
    6279              :           else
    6280              :             {
    6281              :               // trait object type (one bound)
    6282              : 
    6283              :               // get value semantics trait bound
    6284            0 :               AST::TraitBound value_bound (*converted_bound);
    6285              : 
    6286            0 :               return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    6287            0 :                 new AST::TraitObjectTypeOneBound (value_bound,
    6288            0 :                                                   left_delim_locus));
    6289            0 :             }
    6290            5 :         }
    6291              :     }
    6292              :   else
    6293              :     {
    6294         5961 :       return std::unique_ptr<AST::TupleType> (
    6295         5961 :         new AST::TupleType (std::move (types), left_delim_locus));
    6296              :     }
    6297              :   /* TODO: ensure that this ensures that dynamic dispatch for traits is not
    6298              :    * lost somehow */
    6299         5966 : }
    6300              : 
    6301              : /* Parses a type that has 'for' as its first character. This means it has a
    6302              :  * "for lifetimes", so returns either a BareFunctionType, TraitObjectType, or
    6303              :  * TraitObjectTypeOneBound depending on following characters. */
    6304              : template <typename ManagedTokenSource>
    6305              : std::unique_ptr<AST::Type>
    6306           12 : Parser<ManagedTokenSource>::parse_for_prefixed_type ()
    6307              : {
    6308           12 :   location_t for_locus = lexer.peek_token ()->get_locus ();
    6309              :   // parse for lifetimes in type
    6310           12 :   std::vector<AST::LifetimeParam> for_lifetimes = parse_for_lifetimes ();
    6311              : 
    6312              :   // branch on next token - either function or a trait type
    6313           12 :   const_TokenPtr t = lexer.peek_token ();
    6314           12 :   switch (t->get_id ())
    6315              :     {
    6316            4 :     case ASYNC:
    6317              :     case CONST:
    6318              :     case UNSAFE:
    6319              :     case EXTERN_KW:
    6320              :     case FN_KW:
    6321            4 :       return parse_bare_function_type (std::move (for_lifetimes));
    6322            8 :     case SCOPE_RESOLUTION:
    6323              :     case IDENTIFIER:
    6324              :     case SUPER:
    6325              :     case SELF:
    6326              :     case SELF_ALIAS:
    6327              :     case CRATE:
    6328              :     case DOLLAR_SIGN:
    6329              :       {
    6330              :         // path, so trait type
    6331              : 
    6332              :         // parse type path to finish parsing trait bound
    6333            8 :         AST::TypePath path = parse_type_path ();
    6334              : 
    6335            8 :         t = lexer.peek_token ();
    6336            8 :         if (t->get_id () != PLUS)
    6337              :           {
    6338              :             // must be one-bound trait type
    6339              :             // create trait bound value object
    6340            8 :             AST::TraitBound bound (std::move (path), for_locus, false, false,
    6341              :                                    std::move (for_lifetimes));
    6342              : 
    6343            8 :             return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    6344           16 :               new AST::TraitObjectTypeOneBound (std::move (bound), for_locus));
    6345            8 :           }
    6346              : 
    6347              :         /* more than one bound trait type (or at least parsed as it - could be
    6348              :          * trailing '+') create trait bound pointer and bounds */
    6349            0 :         std::unique_ptr<AST::TraitBound> initial_bound (
    6350            0 :           new AST::TraitBound (std::move (path), for_locus, false, false,
    6351              :                                std::move (for_lifetimes)));
    6352            0 :         std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    6353            0 :         bounds.push_back (std::move (initial_bound));
    6354              : 
    6355            0 :         while (t->get_id () == PLUS)
    6356              :           {
    6357            0 :             lexer.skip_token ();
    6358              : 
    6359              :             // parse type param bound if it exists
    6360            0 :             std::unique_ptr<AST::TypeParamBound> bound
    6361              :               = parse_type_param_bound ();
    6362            0 :             if (bound == nullptr)
    6363              :               {
    6364              :                 // not an error - e.g. trailing plus
    6365            0 :                 return nullptr;
    6366              :               }
    6367            0 :             bounds.push_back (std::move (bound));
    6368              : 
    6369            0 :             t = lexer.peek_token ();
    6370              :           }
    6371              : 
    6372            0 :         return std::unique_ptr<AST::TraitObjectType> (
    6373            0 :           new AST::TraitObjectType (std::move (bounds), for_locus, false));
    6374            8 :       }
    6375            0 :     default:
    6376              :       // error
    6377            0 :       add_error (Error (t->get_locus (),
    6378              :                         "unrecognised token %qs in bare function type or trait "
    6379              :                         "object type or trait object type one bound",
    6380              :                         t->get_token_description ()));
    6381              : 
    6382            0 :       return nullptr;
    6383              :     }
    6384           12 : }
    6385              : 
    6386              : // Parses a maybe named param used in bare function types.
    6387              : template <typename ManagedTokenSource>
    6388              : AST::MaybeNamedParam
    6389        30158 : Parser<ManagedTokenSource>::parse_maybe_named_param (AST::AttrVec outer_attrs)
    6390              : {
    6391              :   /* Basically guess that param is named if first token is identifier or
    6392              :    * underscore and second token is semicolon. This should probably have no
    6393              :    * exceptions. rustc uses backtracking to parse these, but at the time of
    6394              :    * writing gccrs has no backtracking capabilities. */
    6395        30158 :   const_TokenPtr current = lexer.peek_token ();
    6396        30158 :   const_TokenPtr next = lexer.peek_token (1);
    6397              : 
    6398        30158 :   Identifier name;
    6399        30158 :   AST::MaybeNamedParam::ParamKind kind = AST::MaybeNamedParam::UNNAMED;
    6400              : 
    6401        30158 :   if (current->get_id () == IDENTIFIER && next->get_id () == COLON)
    6402              :     {
    6403              :       // named param
    6404            1 :       name = {current};
    6405            1 :       kind = AST::MaybeNamedParam::IDENTIFIER;
    6406            1 :       lexer.skip_token (1);
    6407              :     }
    6408        30157 :   else if (current->get_id () == UNDERSCORE && next->get_id () == COLON)
    6409              :     {
    6410              :       // wildcard param
    6411           12 :       name = {Values::Keywords::UNDERSCORE, current->get_locus ()};
    6412            6 :       kind = AST::MaybeNamedParam::WILDCARD;
    6413            6 :       lexer.skip_token (1);
    6414              :     }
    6415              : 
    6416              :   // parse type (required)
    6417        30158 :   std::unique_ptr<AST::Type> type = parse_type ();
    6418        30158 :   if (type == nullptr)
    6419              :     {
    6420            0 :       Error error (lexer.peek_token ()->get_locus (),
    6421              :                    "failed to parse type in maybe named param");
    6422            0 :       add_error (std::move (error));
    6423              : 
    6424            0 :       return AST::MaybeNamedParam::create_error ();
    6425            0 :     }
    6426              : 
    6427        60316 :   return AST::MaybeNamedParam (std::move (name), kind, std::move (type),
    6428        30158 :                                std::move (outer_attrs), current->get_locus ());
    6429        60316 : }
    6430              : 
    6431              : /* Parses a bare function type (with the given for lifetimes for convenience -
    6432              :  * does not parse them itself). */
    6433              : template <typename ManagedTokenSource>
    6434              : std::unique_ptr<AST::BareFunctionType>
    6435         5062 : Parser<ManagedTokenSource>::parse_bare_function_type (
    6436              :   std::vector<AST::LifetimeParam> for_lifetimes)
    6437              : {
    6438              :   // TODO: pass in for lifetime location as param
    6439         5062 :   location_t best_try_locus = lexer.peek_token ()->get_locus ();
    6440              : 
    6441         5062 :   auto qualifiers = parse_function_qualifiers ();
    6442         5062 :   if (!qualifiers)
    6443            0 :     return nullptr;
    6444              : 
    6445         5062 :   if (!skip_token (FN_KW))
    6446            0 :     return nullptr;
    6447              : 
    6448         5062 :   if (!skip_token (LEFT_PAREN))
    6449            0 :     return nullptr;
    6450              : 
    6451              :   // parse function params, if they exist
    6452         5062 :   std::vector<AST::MaybeNamedParam> params;
    6453         5062 :   bool is_variadic = false;
    6454         5062 :   AST::AttrVec variadic_attrs;
    6455              : 
    6456         5062 :   const_TokenPtr t = lexer.peek_token ();
    6457        63676 :   while (t->get_id () != RIGHT_PAREN)
    6458              :     {
    6459        31694 :       AST::AttrVec temp_attrs = parse_outer_attributes ();
    6460              : 
    6461        63388 :       if (lexer.peek_token ()->get_id () == ELLIPSIS)
    6462              :         {
    6463         1536 :           lexer.skip_token ();
    6464         1536 :           is_variadic = true;
    6465         1536 :           variadic_attrs = std::move (temp_attrs);
    6466              : 
    6467         1536 :           t = lexer.peek_token ();
    6468              : 
    6469         1536 :           if (t->get_id () != RIGHT_PAREN)
    6470              :             {
    6471            0 :               Error error (t->get_locus (),
    6472              :                            "expected right parentheses after variadic in maybe "
    6473              :                            "named function "
    6474              :                            "parameters, found %qs",
    6475              :                            t->get_token_description ());
    6476            0 :               add_error (std::move (error));
    6477              : 
    6478            0 :               return nullptr;
    6479            0 :             }
    6480              : 
    6481              :           break;
    6482              :         }
    6483              : 
    6484        30158 :       AST::MaybeNamedParam param
    6485        30158 :         = parse_maybe_named_param (std::move (temp_attrs));
    6486        30158 :       if (param.is_error ())
    6487              :         {
    6488            0 :           Error error (
    6489            0 :             lexer.peek_token ()->get_locus (),
    6490              :             "failed to parse maybe named param in bare function type");
    6491            0 :           add_error (std::move (error));
    6492              : 
    6493            0 :           return nullptr;
    6494            0 :         }
    6495        30158 :       params.push_back (std::move (param));
    6496              : 
    6497        60316 :       if (lexer.peek_token ()->get_id () != COMMA)
    6498              :         break;
    6499              : 
    6500        26920 :       lexer.skip_token ();
    6501        26920 :       t = lexer.peek_token ();
    6502              :     }
    6503              : 
    6504         5062 :   if (!skip_token (RIGHT_PAREN))
    6505            0 :     return nullptr;
    6506              : 
    6507              :   // bare function return type, if exists
    6508         5062 :   std::unique_ptr<AST::TypeNoBounds> return_type = nullptr;
    6509        10124 :   if (lexer.peek_token ()->get_id () == RETURN_TYPE)
    6510              :     {
    6511         4977 :       lexer.skip_token ();
    6512              : 
    6513              :       // parse required TypeNoBounds
    6514         4977 :       return_type = parse_type_no_bounds ();
    6515         4977 :       if (return_type == nullptr)
    6516              :         {
    6517            0 :           Error error (lexer.peek_token ()->get_locus (),
    6518              :                        "failed to parse return type (type no bounds) in bare "
    6519              :                        "function type");
    6520            0 :           add_error (std::move (error));
    6521              : 
    6522            0 :           return nullptr;
    6523            0 :         }
    6524              :     }
    6525              : 
    6526         5062 :   return std::unique_ptr<AST::BareFunctionType> (new AST::BareFunctionType (
    6527         5062 :     std::move (for_lifetimes), std::move (qualifiers.value ()),
    6528              :     std::move (params), is_variadic, std::move (variadic_attrs),
    6529         5062 :     std::move (return_type), best_try_locus));
    6530        10124 : }
    6531              : 
    6532              : template <typename ManagedTokenSource>
    6533              : std::unique_ptr<AST::ReferenceType>
    6534        64612 : Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
    6535              : {
    6536              :   // parse optional lifetime
    6537        64612 :   AST::Lifetime lifetime = AST::Lifetime::elided ();
    6538       129224 :   if (lexer.peek_token ()->get_id () == LIFETIME)
    6539              :     {
    6540        10858 :       auto parsed_lifetime = parse_lifetime (true);
    6541        10858 :       if (parsed_lifetime)
    6542              :         {
    6543        10858 :           lifetime = parsed_lifetime.value ();
    6544              :         }
    6545              :       else
    6546              :         {
    6547            0 :           Error error (lexer.peek_token ()->get_locus (),
    6548              :                        "failed to parse lifetime in reference type");
    6549            0 :           add_error (std::move (error));
    6550              : 
    6551            0 :           return nullptr;
    6552            0 :         }
    6553        10858 :     }
    6554              : 
    6555        64612 :   bool is_mut = false;
    6556       129224 :   if (lexer.peek_token ()->get_id () == MUT)
    6557              :     {
    6558        10851 :       lexer.skip_token ();
    6559        10851 :       is_mut = true;
    6560              :     }
    6561              : 
    6562              :   // parse type no bounds, which is required
    6563        64612 :   std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
    6564        64612 :   if (type == nullptr)
    6565              :     {
    6566            0 :       Error error (lexer.peek_token ()->get_locus (),
    6567              :                    "failed to parse referenced type in reference type");
    6568            0 :       add_error (std::move (error));
    6569              : 
    6570            0 :       return nullptr;
    6571            0 :     }
    6572              : 
    6573              :   return std::unique_ptr<AST::ReferenceType> (
    6574       193836 :     new AST::ReferenceType (is_mut, std::move (type), locus,
    6575        64612 :                             std::move (lifetime)));
    6576        64612 : }
    6577              : 
    6578              : // Parses a reference type (mutable or immutable, with given lifetime).
    6579              : template <typename ManagedTokenSource>
    6580              : std::unique_ptr<AST::ReferenceType>
    6581        64612 : Parser<ManagedTokenSource>::parse_reference_type ()
    6582              : {
    6583        64612 :   auto t = lexer.peek_token ();
    6584        64612 :   auto locus = t->get_locus ();
    6585              : 
    6586        64612 :   switch (t->get_id ())
    6587              :     {
    6588        64389 :     case AMP:
    6589        64389 :       skip_token (AMP);
    6590        64389 :       return parse_reference_type_inner (locus);
    6591          223 :     case LOGICAL_AND:
    6592          223 :       skip_token (LOGICAL_AND);
    6593              :       return std::unique_ptr<AST::ReferenceType> (
    6594          669 :         new AST::ReferenceType (false, parse_reference_type_inner (locus),
    6595          223 :                                 locus));
    6596            0 :     default:
    6597            0 :       rust_unreachable ();
    6598              :     }
    6599        64612 : }
    6600              : 
    6601              : // Parses a raw (unsafe) pointer type.
    6602              : template <typename ManagedTokenSource>
    6603              : std::unique_ptr<AST::RawPointerType>
    6604        18648 : Parser<ManagedTokenSource>::parse_raw_pointer_type ()
    6605              : {
    6606        18648 :   location_t locus = lexer.peek_token ()->get_locus ();
    6607        18648 :   skip_token (ASTERISK);
    6608              : 
    6609        18648 :   AST::RawPointerType::PointerType kind = AST::RawPointerType::CONST;
    6610              : 
    6611              :   // branch on next token for pointer kind info
    6612        18648 :   const_TokenPtr t = lexer.peek_token ();
    6613        18648 :   switch (t->get_id ())
    6614              :     {
    6615         6566 :     case MUT:
    6616         6566 :       kind = AST::RawPointerType::MUT;
    6617         6566 :       lexer.skip_token ();
    6618         6566 :       break;
    6619        12082 :     case CONST:
    6620        12082 :       kind = AST::RawPointerType::CONST;
    6621        12082 :       lexer.skip_token ();
    6622        12082 :       break;
    6623            0 :     default:
    6624            0 :       add_error (Error (t->get_locus (),
    6625              :                         "unrecognised token %qs in raw pointer type",
    6626              :                         t->get_token_description ()));
    6627              : 
    6628            0 :       return nullptr;
    6629              :     }
    6630              : 
    6631              :   // parse type no bounds (required)
    6632        18648 :   std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
    6633        18648 :   if (type == nullptr)
    6634              :     {
    6635            0 :       Error error (lexer.peek_token ()->get_locus (),
    6636              :                    "failed to parse pointed type of raw pointer type");
    6637            0 :       add_error (std::move (error));
    6638              : 
    6639            0 :       return nullptr;
    6640            0 :     }
    6641              : 
    6642              :   return std::unique_ptr<AST::RawPointerType> (
    6643        18648 :     new AST::RawPointerType (kind, std::move (type), locus));
    6644        18648 : }
    6645              : 
    6646              : /* Parses a slice or array type, depending on following arguments (as
    6647              :  * lookahead is not possible). */
    6648              : template <typename ManagedTokenSource>
    6649              : std::unique_ptr<AST::TypeNoBounds>
    6650         9346 : Parser<ManagedTokenSource>::parse_slice_or_array_type ()
    6651              : {
    6652         9346 :   location_t locus = lexer.peek_token ()->get_locus ();
    6653         9346 :   skip_token (LEFT_SQUARE);
    6654              : 
    6655              :   // parse inner type (required)
    6656         9346 :   std::unique_ptr<AST::Type> inner_type = parse_type ();
    6657         9346 :   if (inner_type == nullptr)
    6658              :     {
    6659            0 :       Error error (lexer.peek_token ()->get_locus (),
    6660              :                    "failed to parse inner type in slice or array type");
    6661            0 :       add_error (std::move (error));
    6662              : 
    6663            0 :       return nullptr;
    6664            0 :     }
    6665              : 
    6666              :   // branch on next token
    6667         9346 :   const_TokenPtr t = lexer.peek_token ();
    6668         9346 :   switch (t->get_id ())
    6669              :     {
    6670         5834 :     case RIGHT_SQUARE:
    6671              :       // slice type
    6672         5834 :       lexer.skip_token ();
    6673              : 
    6674         5834 :       return std::unique_ptr<AST::SliceType> (
    6675         5834 :         new AST::SliceType (std::move (inner_type), locus));
    6676         3512 :     case SEMICOLON:
    6677              :       {
    6678              :         // array type
    6679         3512 :         lexer.skip_token ();
    6680              : 
    6681              :         // parse required array size expression
    6682         3512 :         auto size = parse_anon_const ();
    6683              : 
    6684         3512 :         if (!size)
    6685              :           {
    6686            1 :             Error error (lexer.peek_token ()->get_locus (),
    6687              :                          "failed to parse size expression in array type");
    6688            1 :             add_error (std::move (error));
    6689              : 
    6690            1 :             return nullptr;
    6691            1 :           }
    6692              : 
    6693         3511 :         if (!skip_token (RIGHT_SQUARE))
    6694              :           {
    6695            0 :             return nullptr;
    6696              :           }
    6697              : 
    6698         3511 :         return std::unique_ptr<AST::ArrayType> (
    6699         7009 :           new AST::ArrayType (std::move (inner_type), std::move (*size),
    6700         3511 :                               locus));
    6701         3512 :       }
    6702            0 :     default:
    6703              :       // error
    6704            0 :       add_error (
    6705            0 :         Error (t->get_locus (),
    6706              :                "unrecognised token %qs in slice or array type after inner type",
    6707              :                t->get_token_description ()));
    6708              : 
    6709            0 :       return nullptr;
    6710              :     }
    6711         9346 : }
    6712              : 
    6713              : // Parses a type, taking into account type boundary disambiguation.
    6714              : template <typename ManagedTokenSource>
    6715              : std::unique_ptr<AST::TypeNoBounds>
    6716       124642 : Parser<ManagedTokenSource>::parse_type_no_bounds ()
    6717              : {
    6718       124642 :   const_TokenPtr t = lexer.peek_token ();
    6719       124642 :   switch (t->get_id ())
    6720              :     {
    6721           26 :     case EXCLAM:
    6722              :       // never type - can't be macro as no path beforehand
    6723           26 :       lexer.skip_token ();
    6724           26 :       return std::unique_ptr<AST::NeverType> (
    6725           26 :         new AST::NeverType (t->get_locus ()));
    6726         5498 :     case LEFT_SQUARE:
    6727              :       // slice type or array type - requires further disambiguation
    6728         5498 :       return parse_slice_or_array_type ();
    6729           34 :     case LEFT_SHIFT:
    6730              :     case LEFT_ANGLE:
    6731              :       {
    6732              :         // qualified path in type
    6733           34 :         AST::QualifiedPathInType path = parse_qualified_path_in_type ();
    6734           34 :         if (path.is_error ())
    6735              :           {
    6736            0 :             Error error (t->get_locus (),
    6737              :                          "failed to parse qualified path in type");
    6738            0 :             add_error (std::move (error));
    6739              : 
    6740            0 :             return nullptr;
    6741            0 :           }
    6742           34 :         return std::unique_ptr<AST::QualifiedPathInType> (
    6743           34 :           new AST::QualifiedPathInType (std::move (path)));
    6744           34 :       }
    6745          820 :     case UNDERSCORE:
    6746              :       // inferred type
    6747          820 :       lexer.skip_token ();
    6748          820 :       return std::unique_ptr<AST::InferredType> (
    6749          820 :         new AST::InferredType (t->get_locus ()));
    6750         9542 :     case ASTERISK:
    6751              :       // raw pointer type
    6752         9542 :       return parse_raw_pointer_type ();
    6753          217 :     case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND?
    6754              :     case LOGICAL_AND:
    6755              :       // reference type
    6756          217 :       return parse_reference_type ();
    6757            0 :     case LIFETIME:
    6758              :       /* probably a lifetime bound, so probably type param bounds in
    6759              :        * TraitObjectType. this is not allowed, but detection here for error
    6760              :        * message */
    6761            0 :       add_error (Error (t->get_locus (),
    6762              :                         "lifetime bounds (i.e. in type param bounds, in "
    6763              :                         "TraitObjectType) are not allowed as TypeNoBounds"));
    6764              : 
    6765            0 :       return nullptr;
    6766       105677 :     case IDENTIFIER:
    6767              :     case SUPER:
    6768              :     case SELF:
    6769              :     case SELF_ALIAS:
    6770              :     case CRATE:
    6771              :     case DOLLAR_SIGN:
    6772              :     case SCOPE_RESOLUTION:
    6773              :       {
    6774              :         // macro invocation or type path - requires further disambiguation.
    6775              :         /* for parsing path component of each rule, perhaps parse it as a
    6776              :          * typepath and attempt conversion to simplepath if a trailing '!' is
    6777              :          * found */
    6778              :         /* Type path also includes TraitObjectTypeOneBound BUT if it starts
    6779              :          * with it, it is exactly the same as a TypePath syntactically, so
    6780              :          * this is a syntactical ambiguity. As such, the parser will parse it
    6781              :          * as a TypePath. This, however, does not prevent TraitObjectType from
    6782              :          * starting with a typepath. */
    6783              : 
    6784              :         // parse path as type path
    6785       105677 :         AST::TypePath path = parse_type_path ();
    6786       105677 :         if (path.is_error ())
    6787              :           {
    6788            0 :             Error error (
    6789              :               t->get_locus (),
    6790              :               "failed to parse path as first component of type no bounds");
    6791            0 :             add_error (std::move (error));
    6792              : 
    6793            0 :             return nullptr;
    6794            0 :           }
    6795       105677 :         location_t locus = path.get_locus ();
    6796              : 
    6797              :         // branch on next token
    6798       105677 :         t = lexer.peek_token ();
    6799       105677 :         switch (t->get_id ())
    6800              :           {
    6801            1 :           case EXCLAM:
    6802              :             {
    6803              :               // macro invocation
    6804              :               // convert to simple path
    6805            1 :               AST::SimplePath macro_path = path.as_simple_path ();
    6806            1 :               if (macro_path.is_empty ())
    6807              :                 {
    6808            0 :                   Error error (t->get_locus (),
    6809              :                                "failed to parse simple path in macro "
    6810              :                                "invocation (for type)");
    6811            0 :                   add_error (std::move (error));
    6812              : 
    6813            0 :                   return nullptr;
    6814            0 :                 }
    6815              : 
    6816            1 :               lexer.skip_token ();
    6817              : 
    6818            1 :               auto tok_tree = parse_delim_token_tree ();
    6819            1 :               if (!tok_tree)
    6820            0 :                 return nullptr;
    6821              : 
    6822            2 :               return AST::MacroInvocation::Regular (
    6823            2 :                 AST::MacroInvocData (std::move (macro_path),
    6824            1 :                                      std::move (tok_tree.value ())),
    6825            1 :                 {}, locus);
    6826            2 :             }
    6827       105676 :           default:
    6828              :             // assume that this is a type path and not an error
    6829       105676 :             return std::unique_ptr<AST::TypePath> (
    6830       105676 :               new AST::TypePath (std::move (path)));
    6831              :           }
    6832       105677 :       }
    6833         2327 :     case LEFT_PAREN:
    6834              :       /* tuple type or parenthesised type - requires further disambiguation
    6835              :        * (the usual). ok apparently can be a parenthesised TraitBound too, so
    6836              :        * could be TraitObjectTypeOneBound */
    6837         2327 :       return parse_paren_prefixed_type_no_bounds ();
    6838            2 :     case FOR:
    6839              :     case ASYNC:
    6840              :     case CONST:
    6841              :     case UNSAFE:
    6842              :     case EXTERN_KW:
    6843              :     case FN_KW:
    6844              :       // bare function type (with no for lifetimes)
    6845            2 :       return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
    6846          142 :     case IMPL:
    6847          142 :       lexer.skip_token ();
    6848          284 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    6849              :         {
    6850              :           /* cannot be one bound because lifetime prevents it from being
    6851              :            * traitbound not allowed as type no bounds, only here for error
    6852              :            * message */
    6853            0 :           Error error (
    6854            0 :             lexer.peek_token ()->get_locus (),
    6855              :             "lifetime (probably lifetime bound, in type param "
    6856              :             "bounds, in ImplTraitType) is not allowed in TypeNoBounds");
    6857            0 :           add_error (std::move (error));
    6858              : 
    6859            0 :           return nullptr;
    6860            0 :         }
    6861              :       else
    6862              :         {
    6863              :           // should be trait bound, so parse trait bound
    6864          142 :           std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    6865          142 :           if (initial_bound == nullptr)
    6866              :             {
    6867            0 :               Error error (lexer.peek_token ()->get_locus (),
    6868              :                            "failed to parse ImplTraitTypeOneBound bound");
    6869            0 :               add_error (std::move (error));
    6870              : 
    6871            0 :               return nullptr;
    6872            0 :             }
    6873              : 
    6874          142 :           location_t locus = t->get_locus ();
    6875              : 
    6876              :           // ensure not a trait with multiple bounds
    6877          142 :           t = lexer.peek_token ();
    6878          142 :           if (t->get_id () == PLUS)
    6879              :             {
    6880            0 :               Error error (t->get_locus (),
    6881              :                            "plus after trait bound means an ImplTraitType, "
    6882              :                            "which is not allowed as a TypeNoBounds");
    6883            0 :               add_error (std::move (error));
    6884              : 
    6885            0 :               return nullptr;
    6886            0 :             }
    6887              : 
    6888          142 :           return std::unique_ptr<AST::ImplTraitTypeOneBound> (
    6889          142 :             new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus));
    6890          142 :         }
    6891          357 :     case DYN:
    6892              :     case QUESTION_MARK:
    6893              :       {
    6894              :         // either TraitObjectTypeOneBound
    6895          357 :         bool has_dyn = false;
    6896          357 :         if (t->get_id () == DYN)
    6897              :           {
    6898          357 :             lexer.skip_token ();
    6899          357 :             has_dyn = true;
    6900              :           }
    6901              : 
    6902          714 :         if (lexer.peek_token ()->get_id () == LIFETIME)
    6903              :           {
    6904              :             /* means that cannot be TraitObjectTypeOneBound - so here for
    6905              :              * error message */
    6906            0 :             Error error (lexer.peek_token ()->get_locus (),
    6907              :                          "lifetime as bound in TraitObjectTypeOneBound "
    6908              :                          "is not allowed, so cannot be TypeNoBounds");
    6909            0 :             add_error (std::move (error));
    6910              : 
    6911            0 :             return nullptr;
    6912            0 :           }
    6913              : 
    6914              :         // should be trait bound, so parse trait bound
    6915          357 :         std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    6916          357 :         if (initial_bound == nullptr)
    6917              :           {
    6918            0 :             Error error (
    6919            0 :               lexer.peek_token ()->get_locus (),
    6920              :               "failed to parse TraitObjectTypeOneBound initial bound");
    6921            0 :             add_error (std::move (error));
    6922              : 
    6923            0 :             return nullptr;
    6924            0 :           }
    6925              : 
    6926          357 :         location_t locus = t->get_locus ();
    6927              : 
    6928              :         // detect error with plus as next token
    6929          357 :         t = lexer.peek_token ();
    6930          357 :         if (t->get_id () == PLUS)
    6931              :           {
    6932            0 :             Error error (t->get_locus (),
    6933              :                          "plus after trait bound means a TraitObjectType, "
    6934              :                          "which is not allowed as a TypeNoBounds");
    6935            0 :             add_error (std::move (error));
    6936              : 
    6937            0 :             return nullptr;
    6938            0 :           }
    6939              : 
    6940              :         // convert trait bound to value object
    6941          357 :         AST::TraitBound value_bound (*initial_bound);
    6942              : 
    6943          357 :         return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    6944          714 :           new AST::TraitObjectTypeOneBound (std::move (value_bound), locus,
    6945          357 :                                             has_dyn));
    6946          357 :       }
    6947            0 :     default:
    6948            0 :       add_error (Error (t->get_locus (),
    6949              :                         "unrecognised token %qs in type no bounds",
    6950              :                         t->get_token_description ()));
    6951              : 
    6952            0 :       return nullptr;
    6953              :     }
    6954       124642 : }
    6955              : 
    6956              : // Parses a type no bounds beginning with '('.
    6957              : template <typename ManagedTokenSource>
    6958              : std::unique_ptr<AST::TypeNoBounds>
    6959         2327 : Parser<ManagedTokenSource>::parse_paren_prefixed_type_no_bounds ()
    6960              : {
    6961              :   /* NOTE: this could probably be parsed without the HACK solution of
    6962              :    * parse_paren_prefixed_type, but I was lazy. So FIXME for future.*/
    6963              : 
    6964              :   /* NOTE: again, syntactical ambiguity of a parenthesised trait bound is
    6965              :    * considered a trait bound, not a parenthesised type, so that it can still
    6966              :    * be used in type param bounds. */
    6967              : 
    6968         2327 :   location_t left_paren_locus = lexer.peek_token ()->get_locus ();
    6969              : 
    6970              :   // skip left delim
    6971         2327 :   lexer.skip_token ();
    6972              :   /* while next token isn't close delim, parse comma-separated types, saving
    6973              :    * whether trailing comma happens */
    6974         2327 :   const_TokenPtr t = lexer.peek_token ();
    6975         2327 :   bool trailing_comma = true;
    6976         2327 :   std::vector<std::unique_ptr<AST::Type>> types;
    6977              : 
    6978         7372 :   while (t->get_id () != RIGHT_PAREN)
    6979              :     {
    6980         5174 :       std::unique_ptr<AST::Type> type = parse_type ();
    6981         5174 :       if (type == nullptr)
    6982              :         {
    6983            0 :           Error error (t->get_locus (),
    6984              :                        "failed to parse type inside parentheses (probably "
    6985              :                        "tuple or parenthesised)");
    6986            0 :           add_error (std::move (error));
    6987              : 
    6988            0 :           return nullptr;
    6989            0 :         }
    6990         5174 :       types.push_back (std::move (type));
    6991              : 
    6992         5174 :       t = lexer.peek_token ();
    6993         5174 :       if (t->get_id () != COMMA)
    6994              :         {
    6995          129 :           trailing_comma = false;
    6996              :           break;
    6997              :         }
    6998         5045 :       lexer.skip_token ();
    6999              : 
    7000         5045 :       t = lexer.peek_token ();
    7001              :     }
    7002              : 
    7003         2327 :   if (!skip_token (RIGHT_PAREN))
    7004              :     {
    7005            0 :       return nullptr;
    7006              :     }
    7007              : 
    7008              :   // if only one type and no trailing comma, then not a tuple type
    7009         2327 :   if (types.size () == 1 && !trailing_comma)
    7010              :     {
    7011              :       // must be a TraitObjectType (with more than one bound)
    7012          152 :       if (lexer.peek_token ()->get_id () == PLUS)
    7013              :         {
    7014              :           // error - this is not allowed for type no bounds
    7015            0 :           Error error (lexer.peek_token ()->get_locus (),
    7016              :                        "plus (implying TraitObjectType as type param "
    7017              :                        "bounds) is not allowed in type no bounds");
    7018            0 :           add_error (std::move (error));
    7019              : 
    7020            0 :           return nullptr;
    7021            0 :         }
    7022              :       else
    7023              :         {
    7024              :           // release vector pointer
    7025           76 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
    7026              :           /* HACK: attempt to convert to trait bound. if fails, parenthesised
    7027              :            * type */
    7028           76 :           std::unique_ptr<AST::TraitBound> converted_bound (
    7029           76 :             released_ptr->to_trait_bound (true));
    7030           76 :           if (converted_bound == nullptr)
    7031              :             {
    7032              :               // parenthesised type
    7033           76 :               return std::unique_ptr<AST::ParenthesisedType> (
    7034           76 :                 new AST::ParenthesisedType (std::move (released_ptr),
    7035           76 :                                             left_paren_locus));
    7036              :             }
    7037              :           else
    7038              :             {
    7039              :               // trait object type (one bound)
    7040              : 
    7041              :               // get value semantics trait bound
    7042            0 :               AST::TraitBound value_bound (*converted_bound);
    7043              : 
    7044            0 :               return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    7045            0 :                 new AST::TraitObjectTypeOneBound (value_bound,
    7046            0 :                                                   left_paren_locus));
    7047            0 :             }
    7048           76 :         }
    7049              :     }
    7050              :   else
    7051              :     {
    7052         2251 :       return std::unique_ptr<AST::TupleType> (
    7053         2251 :         new AST::TupleType (std::move (types), left_paren_locus));
    7054              :     }
    7055              :   /* TODO: ensure that this ensures that dynamic dispatch for traits is not
    7056              :    * lost somehow */
    7057         2327 : }
    7058              : 
    7059              : // Parses tuple struct items if they exist. Does not parse parentheses.
    7060              : template <typename ManagedTokenSource>
    7061              : std::unique_ptr<AST::TupleStructItems>
    7062         7279 : Parser<ManagedTokenSource>::parse_tuple_struct_items ()
    7063              : {
    7064         7279 :   std::vector<std::unique_ptr<AST::Pattern>> lower_patterns;
    7065              : 
    7066              :   // DEBUG
    7067         7279 :   rust_debug ("started parsing tuple struct items");
    7068              : 
    7069              :   // check for '..' at front
    7070        14558 :   if (lexer.peek_token ()->get_id () == DOT_DOT)
    7071              :     {
    7072              :       // only parse upper patterns
    7073          132 :       lexer.skip_token ();
    7074              : 
    7075              :       // DEBUG
    7076          132 :       rust_debug ("'..' at front in tuple struct items detected");
    7077              : 
    7078          132 :       std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
    7079              : 
    7080          132 :       const_TokenPtr t = lexer.peek_token ();
    7081          164 :       while (t->get_id () == COMMA)
    7082              :         {
    7083           32 :           lexer.skip_token ();
    7084              : 
    7085              :           // break if right paren
    7086           64 :           if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    7087              :             break;
    7088              : 
    7089              :           // parse pattern, which is now required
    7090           32 :           std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    7091           32 :           if (pattern == nullptr)
    7092              :             {
    7093            0 :               Error error (lexer.peek_token ()->get_locus (),
    7094              :                            "failed to parse pattern in tuple struct items");
    7095            0 :               add_error (std::move (error));
    7096              : 
    7097            0 :               return nullptr;
    7098            0 :             }
    7099           32 :           upper_patterns.push_back (std::move (pattern));
    7100              : 
    7101           32 :           t = lexer.peek_token ();
    7102              :         }
    7103              : 
    7104              :       // DEBUG
    7105          132 :       rust_debug (
    7106              :         "finished parsing tuple struct items ranged (upper/none only)");
    7107              : 
    7108          132 :       return std::unique_ptr<AST::TupleStructItemsHasRest> (
    7109          132 :         new AST::TupleStructItemsHasRest (std::move (lower_patterns),
    7110          132 :                                           std::move (upper_patterns)));
    7111          132 :     }
    7112              : 
    7113              :   // has at least some lower patterns
    7114         7147 :   const_TokenPtr t = lexer.peek_token ();
    7115         7463 :   while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
    7116              :     {
    7117              :       // DEBUG
    7118         7382 :       rust_debug ("about to parse pattern in tuple struct items");
    7119              : 
    7120              :       // parse pattern, which is required
    7121         7382 :       std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    7122         7382 :       if (pattern == nullptr)
    7123              :         {
    7124            0 :           Error error (t->get_locus (),
    7125              :                        "failed to parse pattern in tuple struct items");
    7126            0 :           add_error (std::move (error));
    7127              : 
    7128            0 :           return nullptr;
    7129            0 :         }
    7130         7382 :       lower_patterns.push_back (std::move (pattern));
    7131              : 
    7132              :       // DEBUG
    7133         7382 :       rust_debug ("successfully parsed pattern in tuple struct items");
    7134              : 
    7135        14764 :       if (lexer.peek_token ()->get_id () != COMMA)
    7136              :         {
    7137              :           // DEBUG
    7138         7066 :           rust_debug ("broke out of parsing patterns in tuple struct "
    7139              :                       "items as no comma");
    7140              : 
    7141              :           break;
    7142              :         }
    7143          316 :       lexer.skip_token ();
    7144          316 :       t = lexer.peek_token ();
    7145              :     }
    7146              : 
    7147              :   // branch on next token
    7148         7147 :   t = lexer.peek_token ();
    7149         7147 :   switch (t->get_id ())
    7150              :     {
    7151         7094 :     case RIGHT_PAREN:
    7152         7094 :       return std::unique_ptr<AST::TupleStructItemsNoRest> (
    7153         7094 :         new AST::TupleStructItemsNoRest (std::move (lower_patterns)));
    7154           52 :     case DOT_DOT:
    7155              :       {
    7156              :         // has an upper range that must be parsed separately
    7157           52 :         lexer.skip_token ();
    7158              : 
    7159           52 :         std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
    7160              : 
    7161           52 :         t = lexer.peek_token ();
    7162           66 :         while (t->get_id () == COMMA)
    7163              :           {
    7164           14 :             lexer.skip_token ();
    7165              : 
    7166              :             // break if next token is right paren
    7167           28 :             if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    7168              :               break;
    7169              : 
    7170              :             // parse pattern, which is required
    7171           14 :             std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    7172           14 :             if (pattern == nullptr)
    7173              :               {
    7174            0 :                 Error error (lexer.peek_token ()->get_locus (),
    7175              :                              "failed to parse pattern in tuple struct items");
    7176            0 :                 add_error (std::move (error));
    7177              : 
    7178            0 :                 return nullptr;
    7179            0 :               }
    7180           14 :             upper_patterns.push_back (std::move (pattern));
    7181              : 
    7182           14 :             t = lexer.peek_token ();
    7183              :           }
    7184              : 
    7185           52 :         return std::unique_ptr<AST::TupleStructItemsHasRest> (
    7186           52 :           new AST::TupleStructItemsHasRest (std::move (lower_patterns),
    7187           52 :                                             std::move (upper_patterns)));
    7188           52 :       }
    7189            1 :     default:
    7190              :       // error
    7191            1 :       add_error (Error (t->get_locus (),
    7192              :                         "unexpected token %qs in tuple struct items",
    7193              :                         t->get_token_description ()));
    7194              : 
    7195            1 :       return nullptr;
    7196              :     }
    7197         7279 : }
    7198              : 
    7199              : /* Parses a statement or expression (depending on whether a trailing semicolon
    7200              :  * exists). Useful for block expressions where it cannot be determined through
    7201              :  * lookahead whether it is a statement or expression to be parsed. */
    7202              : template <typename ManagedTokenSource>
    7203              : tl::expected<ExprOrStmt, Parse::Error::Node>
    7204       308152 : Parser<ManagedTokenSource>::parse_stmt_or_expr ()
    7205              : {
    7206              :   // quick exit for empty statement
    7207       308152 :   const_TokenPtr t = lexer.peek_token ();
    7208       308152 :   if (t->get_id () == SEMICOLON)
    7209              :     {
    7210           18 :       lexer.skip_token ();
    7211           18 :       std::unique_ptr<AST::EmptyStmt> stmt (
    7212           18 :         new AST::EmptyStmt (t->get_locus ()));
    7213           36 :       return ExprOrStmt (std::move (stmt));
    7214           18 :     }
    7215              : 
    7216              :   // parse outer attributes
    7217       308134 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    7218       308134 :   ParseRestrictions restrictions;
    7219       308134 :   restrictions.expr_can_be_stmt = true;
    7220              : 
    7221              :   // Defered child error checking: we need to check for a semicolon
    7222       308134 :   tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr;
    7223              : 
    7224              :   // parsing this will be annoying because of the many different possibilities
    7225              :   /* best may be just to copy paste in parse_item switch, and failing that try
    7226              :    * to parse outer attributes, and then pass them in to either a let
    7227              :    * statement or (fallback) expression statement. */
    7228              :   // FIXME: think of a way to do this without such a large switch?
    7229              : 
    7230              :   /* FIXME: for expressions at least, the only way that they can really be
    7231              :    * parsed properly in this way is if they don't support operators on them.
    7232              :    * They must be pratt-parsed otherwise. As such due to composability, only
    7233              :    * explicit statements will have special cases here. This should roughly
    7234              :    * correspond to "expr-with-block", but this warning is here in case it
    7235              :    * isn't the case. */
    7236       308134 :   t = lexer.peek_token ();
    7237       308134 :   switch (t->get_id ())
    7238              :     {
    7239        94248 :     case LET:
    7240              :       {
    7241              :         // let statement
    7242        94248 :         std::unique_ptr<AST::LetStmt> stmt (
    7243        94248 :           parse_let_stmt (std::move (outer_attrs)));
    7244       188496 :         return ExprOrStmt (std::move (stmt));
    7245        94248 :       }
    7246          364 :     case CONST:
    7247          728 :       if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
    7248              :         {
    7249              :           // const block: parse as expression
    7250           50 :           expr = parse_expr (std::move (outer_attrs), restrictions);
    7251           25 :           break;
    7252              :         }
    7253              :       gcc_fallthrough ();
    7254              :     case PUB:
    7255              :     case MOD:
    7256              :     case EXTERN_KW:
    7257              :     case USE:
    7258              :     case FN_KW:
    7259              :     case TYPE:
    7260              :     case STRUCT_KW:
    7261              :     case ENUM_KW:
    7262              :     case STATIC_KW:
    7263              :     case AUTO:
    7264              :     case TRAIT:
    7265              :     case IMPL:
    7266              :       {
    7267         4439 :         std::unique_ptr<AST::VisItem> item (
    7268         4439 :           parse_vis_item (std::move (outer_attrs)));
    7269         8878 :         return ExprOrStmt (std::move (item));
    7270         4439 :       }
    7271              :     /* TODO: implement union keyword but not really because of
    7272              :      * context-dependence crappy hack way to parse a union written below to
    7273              :      * separate it from the good code. */
    7274              :     // case UNION:
    7275        10507 :     case UNSAFE:
    7276              :       { // maybe - unsafe traits are a thing
    7277              :         /* if any of these (should be all possible VisItem prefixes), parse a
    7278              :          * VisItem - can't parse item because would require reparsing outer
    7279              :          * attributes */
    7280        10507 :         const_TokenPtr t2 = lexer.peek_token (1);
    7281        10507 :         switch (t2->get_id ())
    7282              :           {
    7283        10463 :           case LEFT_CURLY:
    7284              :             {
    7285              :               // unsafe block: parse as expression
    7286        20926 :               expr = parse_expr (std::move (outer_attrs), restrictions);
    7287              :               break;
    7288              :             }
    7289            0 :           case AUTO:
    7290              :           case TRAIT:
    7291              :             {
    7292              :               // unsafe trait
    7293            0 :               std::unique_ptr<AST::VisItem> item (
    7294            0 :                 parse_vis_item (std::move (outer_attrs)));
    7295            0 :               return ExprOrStmt (std::move (item));
    7296            0 :             }
    7297           44 :           case EXTERN_KW:
    7298              :           case FN_KW:
    7299              :             {
    7300              :               // unsafe function
    7301           44 :               std::unique_ptr<AST::VisItem> item (
    7302           44 :                 parse_vis_item (std::move (outer_attrs)));
    7303           88 :               return ExprOrStmt (std::move (item));
    7304           44 :             }
    7305            0 :           case IMPL:
    7306              :             {
    7307              :               // unsafe trait impl
    7308            0 :               std::unique_ptr<AST::VisItem> item (
    7309            0 :                 parse_vis_item (std::move (outer_attrs)));
    7310            0 :               return ExprOrStmt (std::move (item));
    7311            0 :             }
    7312            0 :           default:
    7313            0 :             add_error (Error (t2->get_locus (),
    7314              :                               "unrecognised token %qs after parsing unsafe - "
    7315              :                               "expected beginning of expression or statement",
    7316              :                               t->get_token_description ()));
    7317              : 
    7318              :             // skip somewhere?
    7319              :             return tl::unexpected<Parse::Error::Node> (
    7320            0 :               Parse::Error::Node::MALFORMED);
    7321              :           }
    7322              :         break;
    7323       210119 :       }
    7324              :       /* FIXME: this is either a macro invocation or macro invocation semi.
    7325              :        * start parsing to determine which one it is. */
    7326              :       // FIXME: old code there
    7327              : 
    7328              :     // crappy hack to do union "keyword"
    7329       125677 :     case IDENTIFIER:
    7330       125677 :       if (t->get_str () == Values::WeakKeywords::UNION
    7331       125713 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
    7332              :         {
    7333            1 :           std::unique_ptr<AST::VisItem> item (
    7334            1 :             parse_vis_item (std::move (outer_attrs)));
    7335            2 :           return ExprOrStmt (std::move (item));
    7336              :           // or should this go straight to parsing union?
    7337            1 :         }
    7338       125676 :       else if (t->get_str () == Values::WeakKeywords::MACRO_RULES
    7339       129756 :                && lexer.peek_token (1)->get_id () == EXCLAM)
    7340              :         {
    7341              :           // macro_rules! macro item
    7342         4080 :           std::unique_ptr<AST::Item> item (
    7343         4080 :             parse_macro_rules_def (std::move (outer_attrs)));
    7344         8160 :           return ExprOrStmt (std::move (item));
    7345         4080 :         }
    7346              :       gcc_fallthrough ();
    7347              :     case SUPER:
    7348              :     case SELF:
    7349              :     case SELF_ALIAS:
    7350              :     case CRATE:
    7351              :     case SCOPE_RESOLUTION:
    7352              :     case DOLLAR_SIGN:
    7353              :       {
    7354       145151 :         AST::PathInExpression path = parse_path_in_expression ();
    7355              :         tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
    7356       145151 :           null_denotation;
    7357              : 
    7358       290302 :         if (lexer.peek_token ()->get_id () == EXCLAM)
    7359              :           {
    7360        16306 :             std::unique_ptr<AST::MacroInvocation> invoc
    7361        32612 :               = parse_macro_invocation_partial (std::move (path),
    7362              :                                                 std::move (outer_attrs));
    7363        16306 :             if (invoc == nullptr)
    7364              :               return tl::unexpected<Parse::Error::Node> (
    7365            0 :                 Parse::Error::Node::CHILD_ERROR);
    7366              : 
    7367        16306 :             if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
    7368              :               {
    7369         8286 :                 invoc->add_semicolon ();
    7370              :                 // Macro invocation with semicolon.
    7371         8286 :                 return ExprOrStmt (
    7372        16572 :                   std::unique_ptr<AST::Stmt> (std::move (invoc)));
    7373              :               }
    7374              : 
    7375        16040 :             TokenId after_macro = lexer.peek_token ()->get_id ();
    7376              : 
    7377         8020 :             AST::DelimType delim_type = invoc->get_invoc_data ()
    7378         8020 :                                           .get_delim_tok_tree ()
    7379         8020 :                                           .get_delim_type ();
    7380              : 
    7381         8020 :             if (delim_type == AST::CURLY && after_macro != DOT
    7382           20 :                 && after_macro != QUESTION_MARK)
    7383              :               {
    7384           20 :                 rust_debug ("braced macro statement");
    7385           20 :                 return ExprOrStmt (
    7386           40 :                   std::unique_ptr<AST::Stmt> (std::move (invoc)));
    7387              :               }
    7388              : 
    7389         8000 :             null_denotation = std::move (invoc);
    7390        16306 :           }
    7391              :         else
    7392              :           {
    7393              :             null_denotation
    7394       257689 :               = null_denotation_path (std::move (path), {}, restrictions);
    7395              :           }
    7396              : 
    7397       547374 :         expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
    7398              :                                  std::move (outer_attrs), restrictions);
    7399              :         break;
    7400       145151 :       }
    7401        49683 :     default:
    7402              :       /* expression statement or expression itself - parse
    7403              :        * expression then make it statement if semi afterwards */
    7404        99336 :       expr = parse_expr (std::move (outer_attrs), restrictions);
    7405        49683 :       break;
    7406              :     }
    7407              : 
    7408       197016 :   const_TokenPtr after_expr = lexer.peek_token ();
    7409       197016 :   if (after_expr->get_id () == SEMICOLON)
    7410              :     {
    7411              :       // must be expression statement
    7412        49838 :       lexer.skip_token ();
    7413              : 
    7414        49838 :       if (expr)
    7415              :         {
    7416        49837 :           return ExprOrStmt (
    7417        99674 :             std::make_unique<AST::ExprStmt> (std::move (expr.value ()),
    7418       149511 :                                              t->get_locus (), true));
    7419              :         }
    7420              :       else
    7421              :         {
    7422              :           return tl::unexpected<Parse::Error::Node> (
    7423            1 :             Parse::Error::Node::CHILD_ERROR);
    7424              :         }
    7425              :     }
    7426              : 
    7427       147178 :   if (expr)
    7428              :     {
    7429              :       // block expression statement.
    7430       147146 :       if (!expr.value ()->is_expr_without_block ()
    7431       147146 :           && after_expr->get_id () != RIGHT_CURLY)
    7432        10721 :         return ExprOrStmt (
    7433        21442 :           std::make_unique<AST::ExprStmt> (std::move (expr.value ()),
    7434        32163 :                                            t->get_locus (), false));
    7435              : 
    7436              :       // Check if expr_without_block is properly terminated
    7437       136425 :       if (expr.value ()->is_expr_without_block ()
    7438       136425 :           && after_expr->get_id () != RIGHT_CURLY)
    7439              :         {
    7440              :           // expr_without_block must be followed by ';' or '}'
    7441            1 :           Error error (after_expr->get_locus (),
    7442              :                        "expected %<;%> or %<}%> after expression, found %qs",
    7443              :                        after_expr->get_token_description ());
    7444            1 :           add_error (std::move (error));
    7445              :           return tl::unexpected<Parse::Error::Node> (
    7446            1 :             Parse::Error::Node::MALFORMED);
    7447            1 :         }
    7448              :     }
    7449              : 
    7450              :   // return expression
    7451       136456 :   if (expr)
    7452       272848 :     return ExprOrStmt (std::move (expr.value ()));
    7453              :   else
    7454           32 :     return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);
    7455       505150 : }
    7456              : 
    7457              : } // namespace Rust
    7458              : 
    7459              : #include "rust-parse-impl-utils.hxx"
    7460              : #include "rust-parse-impl-attribute.hxx"
    7461              : #include "rust-parse-impl-ttree.hxx"
    7462              : #include "rust-parse-impl-macro.hxx"
    7463              : #include "rust-parse-impl-path.hxx"
    7464              : #include "rust-parse-impl-pattern.hxx"
    7465              : #include "rust-parse-impl-expr.hxx"
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.