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

Suggest syntax when finding method on array #44970

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ impl fmt::Debug for Stmt {
}


#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub enum StmtKind {
/// A local (let) binding.
Local(P<Local>),
Expand Down
26 changes: 25 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3670,7 +3670,9 @@ impl<'a> Parser<'a> {
} else {
None
};
debug!("parse_local ty {:?}", ty);
let init = self.parse_initializer()?;
debug!("parse_local init {:?}", init);
Ok(P(ast::Local {
ty,
pat,
Expand Down Expand Up @@ -4190,7 +4192,29 @@ impl<'a> Parser<'a> {
if macro_legacy_warnings && self.token != token::Semi {
self.warn_missing_semicolon();
} else {
self.expect_one_of(&[token::Semi], &[])?;
match self.expect_one_of(&[token::Semi], &[]) {
Ok(_) => (),
Err(mut err) => {
if let (token::ModSep, StmtKind::Local(local)) = (self.token,
stmt.node) {
if let Some(ref init) = local.init {
// We have an initializer for the `let` binding, which means
// that "something" was parsed correctly as a value there, but
// was followed by more code.
if let ExprKind::Repeat(_, _) = init.node {
let expr_str = self.sess.codemap()
.span_to_snippet(init.span)
.unwrap_or(pprust::expr_to_string(init));
err.span_suggestion(init.span.to(self.span),
"did you mean to use an associated \
type instead?",
format!("<{}>::", expr_str));
}
}
}
return Err(err);
}
}
}
}
_ => {}
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/associated-item-from-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 main() {
let a = [1, 2, 3];
let _ = [i32; 3]::clone(&a);
}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/associated-item-from-array.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: expected one of `.`, `;`, `?`, or an operator, found `::`
--> $DIR/associated-item-from-array.rs:13:21
|
13 | let _ = [i32; 3]::clone(&a);
| --------^^
| | |
| | expected one of `.`, `;`, `?`, or an operator here
| help: did you mean to use an associated type?: `<[i32; 3]>::`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone is not an associated type.
"associated item" maybe?


error: aborting due to previous error