Rollup merge of #100111 - estebank:missing-let, r=compiler-errors
Provide suggestion on missing `let` in binding statement Fix #78907. Fallout from the type ascription syntax.
This commit is contained in:
commit
d4bd4ae27a
4 changed files with 53 additions and 3 deletions
|
@ -2,7 +2,7 @@ use rustc_ast as ast;
|
||||||
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
|
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
|
||||||
use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId};
|
use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId};
|
||||||
use rustc_ast::{PatKind, RangeEnd, VariantData};
|
use rustc_ast::{PatKind, RangeEnd, VariantData};
|
||||||
use rustc_errors::struct_span_err;
|
use rustc_errors::{struct_span_err, Applicability};
|
||||||
use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
|
use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
|
||||||
use rustc_feature::{Features, GateIssue};
|
use rustc_feature::{Features, GateIssue};
|
||||||
use rustc_session::parse::{feature_err, feature_err_issue};
|
use rustc_session::parse::{feature_err, feature_err_issue};
|
||||||
|
@ -577,6 +577,32 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
|
||||||
|
if let ast::StmtKind::Semi(expr) = &stmt.kind
|
||||||
|
&& let ast::ExprKind::Assign(lhs, _, _) = &expr.kind
|
||||||
|
&& let ast::ExprKind::Type(..) = lhs.kind
|
||||||
|
&& self.sess.parse_sess.span_diagnostic.err_count() == 0
|
||||||
|
&& !self.features.type_ascription
|
||||||
|
&& !lhs.span.allows_unstable(sym::type_ascription)
|
||||||
|
{
|
||||||
|
// When we encounter a statement of the form `foo: Ty = val;`, this will emit a type
|
||||||
|
// ascription error, but the likely intention was to write a `let` statement. (#78907).
|
||||||
|
feature_err_issue(
|
||||||
|
&self.sess.parse_sess,
|
||||||
|
sym::type_ascription,
|
||||||
|
lhs.span,
|
||||||
|
GateIssue::Language,
|
||||||
|
"type ascription is experimental",
|
||||||
|
).span_suggestion_verbose(
|
||||||
|
lhs.span.shrink_to_lo(),
|
||||||
|
"you might have meant to introduce a new binding",
|
||||||
|
"let ".to_string(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
).emit();
|
||||||
|
}
|
||||||
|
visit::walk_stmt(self, stmt);
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_expr(&mut self, e: &'a ast::Expr) {
|
fn visit_expr(&mut self, e: &'a ast::Expr) {
|
||||||
match e.kind {
|
match e.kind {
|
||||||
ast::ExprKind::Box(_) => {
|
ast::ExprKind::Box(_) => {
|
||||||
|
@ -795,8 +821,6 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
|
||||||
// checks if `#![feature]` has been used to enable any lang feature
|
// checks if `#![feature]` has been used to enable any lang feature
|
||||||
// does not check the same for lib features unless there's at least one
|
// does not check the same for lib features unless there's at least one
|
||||||
// declared lang feature
|
// declared lang feature
|
||||||
use rustc_errors::Applicability;
|
|
||||||
|
|
||||||
if !sess.opts.unstable_features.is_nightly_build() {
|
if !sess.opts.unstable_features.is_nightly_build() {
|
||||||
let lang_features = &sess.features_untracked().declared_lang_features;
|
let lang_features = &sess.features_untracked().declared_lang_features;
|
||||||
if lang_features.len() == 0 {
|
if lang_features.len() == 0 {
|
||||||
|
|
5
src/test/ui/type/missing-let-in-binding.fixed
Normal file
5
src/test/ui/type/missing-let-in-binding.fixed
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// run-rustfix
|
||||||
|
fn main() {
|
||||||
|
let mut _foo: i32 = 1;
|
||||||
|
let _foo: i32 = 4; //~ ERROR type ascription is experimental
|
||||||
|
}
|
5
src/test/ui/type/missing-let-in-binding.rs
Normal file
5
src/test/ui/type/missing-let-in-binding.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// run-rustfix
|
||||||
|
fn main() {
|
||||||
|
let mut _foo: i32 = 1;
|
||||||
|
_foo: i32 = 4; //~ ERROR type ascription is experimental
|
||||||
|
}
|
16
src/test/ui/type/missing-let-in-binding.stderr
Normal file
16
src/test/ui/type/missing-let-in-binding.stderr
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
error[E0658]: type ascription is experimental
|
||||||
|
--> $DIR/missing-let-in-binding.rs:4:5
|
||||||
|
|
|
||||||
|
LL | _foo: i32 = 4;
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
|
||||||
|
= help: add `#![feature(type_ascription)]` to the crate attributes to enable
|
||||||
|
help: you might have meant to introduce a new binding
|
||||||
|
|
|
||||||
|
LL | let _foo: i32 = 4;
|
||||||
|
| +++
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0658`.
|
Loading…
Add table
Reference in a new issue