1
Fork 0

Rollup merge of #59150 - estebank:type-ascription, r=varkor

Expand suggestions for type ascription parse errors

Fix #51222. CC #48016, #47666, #54516, #34255.
This commit is contained in:
Mazdak Farrokhzad 2019-03-26 09:05:39 +01:00 committed by GitHub
commit b316514dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 302 additions and 22 deletions

View file

@ -3262,11 +3262,21 @@ impl<'a> Resolver<'a> {
resolution
}
fn type_ascription_suggestion(&self,
/// Only used in a specific case of type ascription suggestions
#[doc(hidden)]
fn get_colon_suggestion_span(&self, start: Span) -> Span {
let cm = self.session.source_map();
start.to(cm.next_point(start))
}
fn type_ascription_suggestion(
&self,
err: &mut DiagnosticBuilder<'_>,
base_span: Span) {
base_span: Span,
) {
debug!("type_ascription_suggetion {:?}", base_span);
let cm = self.session.source_map();
let base_snippet = cm.span_to_snippet(base_span);
debug!("self.current_type_ascription {:?}", self.current_type_ascription);
if let Some(sp) = self.current_type_ascription.last() {
let mut sp = *sp;
@ -3274,13 +3284,10 @@ impl<'a> Resolver<'a> {
// Try to find the `:`; bail on first non-':' / non-whitespace.
sp = cm.next_point(sp);
if let Ok(snippet) = cm.span_to_snippet(sp.to(cm.next_point(sp))) {
debug!("snippet {:?}", snippet);
let line_sp = cm.lookup_char_pos(sp.hi()).line;
let line_base_sp = cm.lookup_char_pos(base_span.lo()).line;
debug!("{:?} {:?}", line_sp, line_base_sp);
if snippet == ":" {
err.span_label(base_span,
"expecting a type here because of type ascription");
let mut show_label = true;
if line_sp != line_base_sp {
err.span_suggestion_short(
sp,
@ -3288,6 +3295,49 @@ impl<'a> Resolver<'a> {
";".to_string(),
Applicability::MaybeIncorrect,
);
} else {
let colon_sp = self.get_colon_suggestion_span(sp);
let after_colon_sp = self.get_colon_suggestion_span(
colon_sp.shrink_to_hi(),
);
if !cm.span_to_snippet(after_colon_sp).map(|s| s == " ")
.unwrap_or(false)
{
err.span_suggestion(
colon_sp,
"maybe you meant to write a path separator here",
"::".to_string(),
Applicability::MaybeIncorrect,
);
show_label = false;
}
if let Ok(base_snippet) = base_snippet {
let mut sp = after_colon_sp;
for _ in 0..100 {
// Try to find an assignment
sp = cm.next_point(sp);
let snippet = cm.span_to_snippet(sp.to(cm.next_point(sp)));
match snippet {
Ok(ref x) if x.as_str() == "=" => {
err.span_suggestion(
base_span,
"maybe you meant to write an assignment here",
format!("let {}", base_snippet),
Applicability::MaybeIncorrect,
);
show_label = false;
break;
}
Ok(ref x) if x.as_str() == "\n" => break,
Err(_) => break,
Ok(_) => {}
}
}
}
}
if show_label {
err.span_label(base_span,
"expecting a type here because of type ascription");
}
break;
} else if !snippet.trim().is_empty() {

View file

@ -1836,9 +1836,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_feature_post!(&self, box_syntax, e.span, EXPLAIN_BOX_SYNTAX);
}
ast::ExprKind::Type(..) => {
// To avoid noise about type ascription in common syntax errors, only emit if it
// is the *only* error.
if self.context.parse_sess.span_diagnostic.err_count() == 0 {
gate_feature_post!(&self, type_ascription, e.span,
"type ascription is experimental");
}
}
ast::ExprKind::ObsoleteInPlace(..) => {
// these get a hard error in ast-validation
}

View file

@ -3538,22 +3538,19 @@ impl<'a> Parser<'a> {
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
continue
} else if op == AssocOp::Colon {
let maybe_path = self.could_ascription_be_path(&lhs.node);
let next_sp = self.span;
lhs = match self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type) {
Ok(lhs) => lhs,
Err(mut err) => {
err.span_label(self.span,
"expecting a type here because of type ascription");
let cm = self.sess.source_map();
let cur_pos = cm.lookup_char_pos(self.span.lo());
let op_pos = cm.lookup_char_pos(cur_op_span.hi());
if cur_pos.line != op_pos.line {
err.span_suggestion(
self.bad_type_ascription(
&mut err,
lhs_span,
cur_op_span,
"try using a semicolon",
";".to_string(),
Applicability::MaybeIncorrect // speculative
next_sp,
maybe_path,
);
}
return Err(err);
}
};
@ -3658,6 +3655,58 @@ impl<'a> Parser<'a> {
Ok(lhs)
}
fn could_ascription_be_path(&self, node: &ast::ExprKind) -> bool {
self.token.is_ident() &&
if let ast::ExprKind::Path(..) = node { true } else { false } &&
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren)) ||
self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`
self.look_ahead(2, |t| t.is_ident()) ||
self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar:baz`
self.look_ahead(2, |t| t.is_ident()) ||
self.look_ahead(1, |t| t == &token::ModSep) && // `foo:bar::baz`
self.look_ahead(2, |t| t.is_ident())
}
fn bad_type_ascription(
&self,
err: &mut DiagnosticBuilder<'a>,
lhs_span: Span,
cur_op_span: Span,
next_sp: Span,
maybe_path: bool,
) {
err.span_label(self.span, "expecting a type here because of type ascription");
let cm = self.sess.source_map();
let next_pos = cm.lookup_char_pos(next_sp.lo());
let op_pos = cm.lookup_char_pos(cur_op_span.hi());
if op_pos.line != next_pos.line {
err.span_suggestion(
cur_op_span,
"try using a semicolon",
";".to_string(),
Applicability::MaybeIncorrect,
);
} else {
if maybe_path {
err.span_suggestion(
cur_op_span,
"maybe you meant to write a path separator here",
"::".to_string(),
Applicability::MaybeIncorrect,
);
} else {
err.note("type ascription is a nightly-only feature that lets \
you annotate an expression with a type: `<expr>: <type>`");
err.span_note(
lhs_span,
"this expression expects an ascribed type after the colon",
);
err.help("this might be indicative of a syntax error elsewhere");
}
}
}
fn parse_assoc_op_cast(&mut self, lhs: P<Expr>, lhs_span: Span,
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind)
-> PResult<'a, P<Expr>> {

View file

@ -3,6 +3,14 @@ error: expected type, found `1`
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/E0423.rs:12:36
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected expression, found `==`
--> $DIR/E0423.rs:15:13
@ -15,6 +23,14 @@ error: expected type, found `0`
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/E0423.rs:21:32
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^^^^^
= help: this might be indicative of a syntax error elsewhere
error[E0423]: expected function, found struct `Foo`
--> $DIR/E0423.rs:4:13

View file

@ -88,6 +88,14 @@ error: expected type, found `4`
|
LL | println!("{}", a: &mut 4);
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/issue-22644.rs:34:20
|
LL | println!("{}", a: &mut 4);
| ^
= help: this might be indicative of a syntax error elsewhere
error: aborting due to 9 previous errors

View file

@ -0,0 +1,10 @@
enum Test {
Drill {
field: i32,
}
}
fn main() {
Test::Drill(field: 42);
//~^ ERROR expected type, found
}

View file

@ -0,0 +1,16 @@
error: expected type, found `42`
--> $DIR/issue-34255-1.rs:8:24
|
LL | Test::Drill(field: 42);
| ^^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/issue-34255-1.rs:8:17
|
LL | Test::Drill(field: 42);
| ^^^^^
= help: this might be indicative of a syntax error elsewhere
error: aborting due to previous error

View file

@ -13,6 +13,14 @@ error: expected type, found keyword `loop`
|
LL | loop { break 'label: loop { break 'label 42; }; }
| ^^^^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/lifetime_starts_expressions.rs:6:12
|
LL | loop { break 'label: loop { break 'label 42; }; }
| ^^^^^^^^^^^^
= help: this might be indicative of a syntax error elsewhere
error: aborting due to 2 previous errors

View file

@ -3,6 +3,14 @@ error: expected type, found `3`
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-for.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-for.rs:14:12

View file

@ -3,6 +3,14 @@ error: expected type, found `3`
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-if.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-if.rs:14:12

View file

@ -3,6 +3,14 @@ error: expected type, found `3`
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-while.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-while.rs:14:12

View file

@ -3,6 +3,14 @@ error: expected type, found `3`
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-restrictions-in-lamda.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-restrictions-in-lamda.rs:14:12

View file

@ -0,0 +1,10 @@
fn fun(x: i32) -> i32 { x }
fn main() {
let closure_annotated = |value: i32| -> i32 {
temp: i32 = fun(5i32);
//~^ ERROR cannot find value `temp` in this scope
temp + value + 1
//~^ ERROR cannot find value `temp` in this scope
};
}

View file

@ -0,0 +1,18 @@
error[E0425]: cannot find value `temp` in this scope
--> $DIR/type-ascription-instead-of-let.rs:5:9
|
LL | temp: i32 = fun(5i32);
| ^^^^
| |
| not found in this scope
| help: maybe you meant to write an assignment here: `let temp`
error[E0425]: cannot find value `temp` in this scope
--> $DIR/type-ascription-instead-of-let.rs:7:9
|
LL | temp + value + 1
| ^^^^ not found in this scope
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0425`.

View file

@ -0,0 +1,4 @@
fn main() {
Box:new("foo".to_string())
//~^ ERROR expected type, found
}

View file

@ -0,0 +1,10 @@
error: expected type, found `"foo"`
--> $DIR/type-ascription-instead-of-method.rs:2:13
|
LL | Box:new("foo".to_string())
| - ^^^^^ expecting a type here because of type ascription
| |
| help: maybe you meant to write a path separator here: `::`
error: aborting due to previous error

View file

@ -0,0 +1,5 @@
fn main() {
std:io::stdin();
//~^ ERROR failed to resolve: use of undeclared type or module `io`
//~| ERROR expected value, found module
}

View file

@ -0,0 +1,18 @@
error[E0433]: failed to resolve: use of undeclared type or module `io`
--> $DIR/type-ascription-instead-of-path.rs:2:9
|
LL | std:io::stdin();
| ^^ use of undeclared type or module `io`
error[E0423]: expected value, found module `std`
--> $DIR/type-ascription-instead-of-path.rs:2:5
|
LL | std:io::stdin();
| ^^^- help: maybe you meant to write a path separator here: `::`
| |
| not a value
error: aborting due to 2 previous errors
Some errors occurred: E0423, E0433.
For more information about an error, try `rustc --explain E0423`.

View file

@ -0,0 +1,4 @@
fn main() {
let _ = Option:Some("");
//~^ ERROR expected type, found
}

View file

@ -0,0 +1,10 @@
error: expected type, found `""`
--> $DIR/type-ascription-instead-of-variant.rs:2:25
|
LL | let _ = Option:Some("");
| - ^^ expecting a type here because of type ascription
| |
| help: maybe you meant to write a path separator here: `::`
error: aborting due to previous error

View file

@ -11,6 +11,14 @@ error: expected type, found `0`
|
LL | println!("test"): 0;
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/type-ascription-instead-of-statement-end.rs:9:5
|
LL | println!("test"): 0;
| ^^^^^^^^^^^^^^^^
= help: this might be indicative of a syntax error elsewhere
error: aborting due to 2 previous errors