Introduce TyErr independent from TyInfer

Add a `TyErr` type to represent unknown types in places where
parse errors have happened, while still able to build the AST.

Initially only used to represent incorrectly written fn arguments and
avoid "expected X parameters, found Y" errors when called with the
appropriate amount of parameters. We cannot use `TyInfer` for this as
`_` is not allowed as a valid argument type.

Example output:

```rust
error: expected one of `:` or `@`, found `,`
  --> file.rs:12:9
   |
12 | fn bar(x, y: usize) {}
   |         ^

error[E0061]: this function takes 2 parameters but 3 parameters were supplied
  --> file.rs:19:9
   |
12 | fn bar(x, y) {}
   | --------------- defined here
...
19 |     bar(1, 2, 3);
   |         ^^^^^^^ expected 2 parameters
```
This commit is contained in:
Esteban Küber 2017-03-28 18:56:29 -07:00
parent 5e122f59ba
commit b83352e44c
14 changed files with 115 additions and 7 deletions

View file

@ -407,6 +407,25 @@ impl From<P<Expr>> for LhsExpr {
}
}
/// Create a placeholder argument.
fn dummy_arg(span: Span) -> Arg {
let spanned = Spanned {
span: span,
node: keywords::Invalid.ident()
};
let pat = P(Pat {
id: ast::DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), spanned, None),
span: span
});
let ty = Ty {
node: TyKind::Err,
span: span,
id: ast::DUMMY_NODE_ID
};
Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID }
}
impl<'a> Parser<'a> {
pub fn new(sess: &'a ParseSess,
tokens: TokenStream,
@ -4343,8 +4362,12 @@ impl<'a> Parser<'a> {
Ok(arg) => Ok(Some(arg)),
Err(mut e) => {
e.emit();
let lo = p.prev_span;
// Skip every token until next possible arg or end.
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
Ok(None)
// Create a placeholder argument for proper arg count (#34264).
let span = lo.to(p.prev_span);
Ok(Some(dummy_arg(span)))
}
}
}