Skip to content

Commit

Permalink
Auto merge of #40887 - estebank:ty-placeholder, r=petrochenkov
Browse files Browse the repository at this point in the history
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
```

Fix #34264.
  • Loading branch information
bors committed Apr 8, 2017
2 parents 4cadff6 + 8c31412 commit fe39e94
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
TyTypeof(expression) => {
visitor.visit_nested_body(expression)
}
TyInfer => {}
TyInfer | TyErr => {}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ impl<'a> LoweringContext<'a> {
fn lower_ty(&mut self, t: &Ty) -> P<hir::Ty> {
let kind = match t.node {
TyKind::Infer => hir::TyInfer,
TyKind::Err => hir::TyErr,
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty)),
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)),
TyKind::Rptr(ref region, ref mt) => {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,8 @@ pub enum Ty_ {
/// TyInfer means the type should be inferred instead of it having been
/// specified. This can appear anywhere in a type.
TyInfer,
/// Placeholder for a type that has failed to be defined.
TyErr,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ impl<'a> State<'a> {
hir::TyInfer => {
word(&mut self.s, "_")?;
}
hir::TyErr => {
word(&mut self.s, "?")?;
}
}
self.end()
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for hir::Ty {
hir::TyTraitObject(..) |
hir::TyImplTrait(..) |
hir::TyTypeof(..) |
hir::TyErr |
hir::TyInfer => {
NodeIdHashingMode::Ignore
}
Expand Down Expand Up @@ -282,6 +283,7 @@ impl_stable_hash_for!(enum hir::Ty_ {
TyTraitObject(trait_refs, lifetime),
TyImplTrait(bounds),
TyTypeof(body_id),
TyErr,
TyInfer
});

Expand Down
3 changes: 3 additions & 0 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// handled specially and will not descend into this routine.
self.ty_infer(ast_ty.span)
}
hir::TyErr => {
tcx.types.err
}
};

cache.borrow_mut().insert(ast_ty.id, result_ty);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ impl Clean<Type> for hir::Ty {
}
TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
TyImplTrait(ref bounds) => ImplTrait(bounds.clean(cx)),
TyInfer => Infer,
TyInfer | TyErr => Infer,
TyTypeof(..) => panic!("Unimplemented type {:?}", self.node),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,8 @@ pub enum TyKind {
ImplicitSelf,
// A macro in the type position.
Mac(Mac),
/// Placeholder for a kind that has failed to be defined.
Err,
}

/// Inline assembly dialect.
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
t.map(|Ty {id, node, span}| Ty {
id: fld.new_id(id),
node: match node {
TyKind::Infer | TyKind::ImplicitSelf => node,
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => node,
TyKind::Slice(ty) => TyKind::Slice(fld.fold_ty(ty)),
TyKind::Ptr(mt) => TyKind::Ptr(fld.fold_mt(mt)),
TyKind::Rptr(region, mt) => {
Expand Down
25 changes: 24 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -4376,8 +4395,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)))
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,9 @@ impl<'a> State<'a> {
ast::TyKind::Infer => {
word(&mut self.s, "_")?;
}
ast::TyKind::Err => {
word(&mut self.s, "?")?;
}
ast::TyKind::ImplicitSelf => {
word(&mut self.s, "Self")?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
TyKind::Typeof(ref expression) => {
visitor.visit_expr(expression)
}
TyKind::Infer | TyKind::ImplicitSelf => {}
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
TyKind::Mac(ref mac) => {
visitor.visit_mac(mac)
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/span/issue-34264.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn foo(Option<i32>, String) {}
fn bar(x, y: usize) {}

fn main() {
foo(Some(42), 2);
foo(Some(42), 2, "");
bar("", "");
bar(1, 2);
bar(1, 2, 3);
}
49 changes: 49 additions & 0 deletions src/test/ui/span/issue-34264.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error: expected one of `:` or `@`, found `<`
--> $DIR/issue-34264.rs:11:14
|
11 | fn foo(Option<i32>, String) {}
| ^ expected one of `:` or `@` here

error: expected one of `:` or `@`, found `)`
--> $DIR/issue-34264.rs:11:27
|
11 | fn foo(Option<i32>, String) {}
| ^ expected one of `:` or `@` here

error: expected one of `:` or `@`, found `,`
--> $DIR/issue-34264.rs:12:9
|
12 | fn bar(x, y: usize) {}
| ^ expected one of `:` or `@` here

error[E0061]: this function takes 2 parameters but 3 parameters were supplied
--> $DIR/issue-34264.rs:16:9
|
11 | fn foo(Option<i32>, String) {}
| ------------------------------ defined here
...
16 | foo(Some(42), 2, "");
| ^^^^^^^^^^^^^^^ expected 2 parameters

error[E0308]: mismatched types
--> $DIR/issue-34264.rs:17:13
|
17 | bar("", "");
| ^^ expected usize, found reference
|
= note: expected type `usize`
found type `&'static str`
= help: here are some functions which might fulfill your needs:
- .len()

error[E0061]: this function takes 2 parameters but 3 parameters were supplied
--> $DIR/issue-34264.rs:19:9
|
12 | fn bar(x, y: usize) {}
| ---------------------- defined here
...
19 | bar(1, 2, 3);
| ^^^^^^^ expected 2 parameters

error: aborting due to 3 previous errors

0 comments on commit fe39e94

Please sign in to comment.