Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide suggestion on missing let in binding statement #100111

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_ast as ast;
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId};
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::{Features, GateIssue};
use rustc_session::parse::{feature_err, feature_err_issue};
Expand Down Expand Up @@ -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) {
match e.kind {
ast::ExprKind::Box(_) => {
Expand Down Expand Up @@ -795,8 +821,6 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
// 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
// declared lang feature
use rustc_errors::Applicability;

if !sess.opts.unstable_features.is_nightly_build() {
let lang_features = &sess.features_untracked().declared_lang_features;
if lang_features.len() == 0 {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/type/missing-let-in-binding.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// run-rustfix
fn main() {
let mut _foo: i32 = 1;
let _foo: i32 = 4; //~ ERROR type ascription is experimental
}
5 changes: 5 additions & 0 deletions src/test/ui/type/missing-let-in-binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// run-rustfix
fn main() {
let mut _foo: i32 = 1;
_foo: i32 = 4; //~ ERROR type ascription is experimental
}
16 changes: 16 additions & 0 deletions src/test/ui/type/missing-let-in-binding.stderr
Original file line number Diff line number Diff line change
@@ -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`.