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

Don't suggest methods in certain cases #43829

Closed
Closed
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
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
@@ -936,10 +936,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
self.need_type_info(body_id, span, self_ty);
} else {
let mut err = struct_span_err!(self.tcx.sess,
span, E0283,
"type annotations required: \
span, E0283,
"type annotations required: \
cannot resolve `{}`",
predicate);
predicate);
self.note_obligation_cause(&mut err, obligation);
err.emit();
}
80 changes: 69 additions & 11 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

// Checks that the type of `expr` can be coerced to `expected`.
//
// NB: This code relies on `self.diverges` to be accurate. In
// NB: This code relies on `self.diverges` to be accurate. In
// particular, assignments to `!` will be permitted if the
// diverges flag is currently "always".
pub fn demand_coerce_diag(&self,
@@ -128,16 +128,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
expected) {
err.help(&suggestion);
} else {
let mode = probe::Mode::MethodCall;
let suggestions = self.probe_for_return_type(syntax_pos::DUMMY_SP,
mode,
expected,
checked_ty,
ast::DUMMY_NODE_ID);
if suggestions.len() > 0 {
err.help(&format!("here are some functions which \
might fulfill your needs:\n{}",
self.get_best_match(&suggestions).join("\n")));
// Before getting potentially matching method, we make a first filter.
if self.is_really_a_match(checked_ty, expected) {
let mode = probe::Mode::MethodCall;
let suggestions = self.probe_for_return_type(syntax_pos::DUMMY_SP,
mode,
expected,
checked_ty,
ast::DUMMY_NODE_ID);
if suggestions.len() > 0 {
err.help(&format!("here are some functions which \
might fulfill your needs:\n{}",
self.get_best_match(&suggestions).join("\n")));
}
}
}
return Some(err);
@@ -175,6 +178,61 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}

// In cases such as:
//
// ```
// struct Foo {}
// struct Bar {}
//
// fn hey(pouet: Vec<Bar>){}
//
// fn main() {
// let foo = vec![Foo {}];
// hey(foo); // shouldn't suggest ".to_vec()"
// }
// ```
//
// Or in here:
//
// ```
// fn foo(b: &[u16]) {}
//
// fn main() {
// let a: Vec<u8> = Vec::new();
// foo(&a); // shouldn't suggest ".as_slice()"
// }
// ```
//
// The primitive type is considered the same even though it shouldn't. It fixes it a bit.
fn is_really_a_match(&self, ty_: Ty<'tcx>, expected: Ty<'tcx>) -> bool {
match (self.get_ty(expected), self.get_ty(ty_)) {
(Some(expected), Some(ty_)) => expected == ty_,
_ => true,
}
}

// For now it only checks slices, arrays and Vecs.
fn get_ty(&self, ty_: Ty<'tcx>) -> Option<Ty<'tcx>> {
match ty_.sty {
ty::TypeVariants::TyRawPtr(t) | ty::TypeVariants::TyRef(_, t) => self.get_ty(t.ty),
ty::TypeVariants::TyArray(ty_, _) | ty::TypeVariants::TySlice(ty_) => {
Some(ty_)
}
ty::TypeVariants::TyAdt(_, subs) => {
if ty_.to_string().split("::").last().unwrap_or("").starts_with("Vec<") {
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep... I needed to check if it was a Vec but if you have a better to check it, I'm all for it!

Copy link
Contributor

Choose a reason for hiding this comment

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

Like, ..., use an attribute?

if let Some(new_ty) = subs[0].as_type() {
Some(new_ty)
} else {
None
}
} else {
None
}
}
_ => None,
}
}

// This function checks if the method isn't static and takes other arguments than `self`.
fn has_no_input_arg(&self, method: &AssociatedItem) -> bool {
match method.def() {
26 changes: 26 additions & 0 deletions src/test/ui/no_suggestions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

struct Foo {}
struct Bar {}

fn hey(pouet: Vec<Bar>){}

fn foo(_b: &[u16]) {}

fn another(_b: String) {}

fn main() {
let a: Vec<u8> = Vec::new();
foo(&a);
let foo = vec![Foo {}];
hey(foo);
another("salutations")
}
35 changes: 35 additions & 0 deletions src/test/ui/no_suggestions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0308]: mismatched types
--> $DIR/no_suggestions.rs:22:9
|
22 | foo(&a);
| ^^ expected slice, found struct `std::vec::Vec`
|
= note: expected type `&[u16]`
found type `&std::vec::Vec<u8>`

error[E0308]: mismatched types
--> $DIR/no_suggestions.rs:24:9
|
24 | hey(foo);
| ^^^ expected struct `Bar`, found struct `Foo`
|
= note: expected type `std::vec::Vec<Bar>`
found type `std::vec::Vec<Foo>`

error[E0308]: mismatched types
--> $DIR/no_suggestions.rs:25:13
|
25 | another("salutations")
| ^^^^^^^^^^^^^ expected struct `std::string::String`, found reference
|
= note: expected type `std::string::String`
found type `&'static str`
= help: here are some functions which might fulfill your needs:
- .escape_debug()
- .escape_default()
- .escape_unicode()
- .to_lowercase()
- .to_uppercase()

error: aborting due to 3 previous errors