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

Consider methods on fundamental impl when method is not found on numeric type #70784

Merged
merged 1 commit into from
Apr 10, 2020
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
28 changes: 26 additions & 2 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut candidates = all_traits(self.tcx).into_iter().filter_map(|info| {
self.associated_item(info.def_id, item_name, Namespace::ValueNS)
});
if let (true, false, SelfSource::MethodCall(expr), Some(_)) = (
// There are methods that are defined on the primitive types and won't be
// found when exploring `all_traits`, but we also need them to be acurate on
// our suggestions (#47759).
let fund_assoc = |opt_def_id: Option<DefId>| {
opt_def_id
.and_then(|id| self.associated_item(id, item_name, Namespace::ValueNS))
.is_some()
};
let lang_items = tcx.lang_items();
let found_candidate = candidates.next().is_some()
|| fund_assoc(lang_items.i8_impl())
|| fund_assoc(lang_items.i16_impl())
|| fund_assoc(lang_items.i32_impl())
|| fund_assoc(lang_items.i64_impl())
|| fund_assoc(lang_items.i128_impl())
|| fund_assoc(lang_items.u8_impl())
|| fund_assoc(lang_items.u16_impl())
|| fund_assoc(lang_items.u32_impl())
|| fund_assoc(lang_items.u64_impl())
|| fund_assoc(lang_items.u128_impl())
|| fund_assoc(lang_items.f32_impl())
|| fund_assoc(lang_items.f32_runtime_impl())
|| fund_assoc(lang_items.f64_impl())
|| fund_assoc(lang_items.f64_runtime_impl());
if let (true, false, SelfSource::MethodCall(expr), true) = (
actual.is_numeric(),
actual.has_concrete_skeleton(),
source,
candidates.next(),
found_candidate,
) {
let mut err = struct_span_err!(
tcx.sess,
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/issues/issue-29181.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ extern crate issue_29181 as foo;

fn main() {
0.homura(); //~ ERROR no method named `homura` found
// Issue #47759, detect existing method on the fundamental impl:
let _ = |x: f64| x * 2.0.exp(); //~ ERROR can't call method `exp` on ambiguous numeric type
}
16 changes: 14 additions & 2 deletions src/test/ui/issues/issue-29181.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ error[E0599]: no method named `homura` found for type `{integer}` in the current
LL | 0.homura();
| ^^^^^^ method not found in `{integer}`

error: aborting due to previous error
error[E0689]: can't call method `exp` on ambiguous numeric type `{float}`
--> $DIR/issue-29181.rs:8:30
|
LL | let _ = |x: f64| x * 2.0.exp();
| ^^^
|
help: you must specify a concrete type for this numeric value, like `f32`
|
LL | let _ = |x: f64| x * 2.0_f32.exp();
| ^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0599`.
Some errors have detailed explanations: E0599, E0689.
For more information about an error, try `rustc --explain E0599`.