Skip to content

Commit ab0938d

Browse files
Filter out intrinsics if we have other import candidates to suggest
1 parent 9f7e997 commit ab0938d

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
384384

385385
// Try to lookup name in more relaxed fashion for better error reporting.
386386
let ident = path.last().unwrap().ident;
387-
let candidates = self
387+
let mut candidates = self
388388
.r
389389
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
390390
.into_iter()
@@ -396,6 +396,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
396396
})
397397
.collect::<Vec<_>>();
398398
let crate_def_id = CRATE_DEF_ID.to_def_id();
399+
// Try to filter out intrinsics candidates, as long as we have
400+
// some other candidates to suggest.
401+
let intrinsic_candidates: Vec<_> = candidates
402+
.drain_filter(|sugg| {
403+
let path = path_names_to_string(&sugg.path);
404+
path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
405+
})
406+
.collect();
407+
if candidates.is_empty() {
408+
// Put them back if we have no more candidates to suggest...
409+
candidates.extend(intrinsic_candidates);
410+
}
399411
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
400412
let mut enum_candidates: Vec<_> = self
401413
.r
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
// Should suggest only `std::mem::size_of`
3+
let _ = size_of::<usize>();
4+
//~^ ERROR cannot find
5+
6+
// Should suggest `std::intrinsics::fabsf64`,
7+
// since there is no non-intrinsic to suggest.
8+
let _ = fabsf64(1.0);
9+
//~^ ERROR cannot find
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0425]: cannot find function `size_of` in this scope
2+
--> $DIR/filter-intrinsics.rs:3:13
3+
|
4+
LL | let _ = size_of::<usize>();
5+
| ^^^^^^^ not found in this scope
6+
|
7+
help: consider importing this function
8+
|
9+
LL | use std::mem::size_of;
10+
|
11+
12+
error[E0425]: cannot find function `fabsf64` in this scope
13+
--> $DIR/filter-intrinsics.rs:8:13
14+
|
15+
LL | let _ = fabsf64(1.0);
16+
| ^^^^^^^ not found in this scope
17+
|
18+
help: consider importing this function
19+
|
20+
LL | use std::intrinsics::fabsf64;
21+
|
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)