Skip to content

Commit 0ee1504

Browse files
authored
Rollup merge of #97822 - compiler-errors:hesitate-to-suggest-intrinsics, r=oli-obk
Filter out intrinsics if we have other import candidates to suggest Fixes #97618 Also open to just sorting these candidates to be last. Pretty easy to modify the code to do that, too.
2 parents 2d1e075 + ab0938d commit 0ee1504

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
@@ -396,7 +396,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
396396

397397
// Try to lookup name in more relaxed fashion for better error reporting.
398398
let ident = path.last().unwrap().ident;
399-
let candidates = self
399+
let mut candidates = self
400400
.r
401401
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
402402
.into_iter()
@@ -408,6 +408,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
408408
})
409409
.collect::<Vec<_>>();
410410
let crate_def_id = CRATE_DEF_ID.to_def_id();
411+
// Try to filter out intrinsics candidates, as long as we have
412+
// some other candidates to suggest.
413+
let intrinsic_candidates: Vec<_> = candidates
414+
.drain_filter(|sugg| {
415+
let path = path_names_to_string(&sugg.path);
416+
path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
417+
})
418+
.collect();
419+
if candidates.is_empty() {
420+
// Put them back if we have no more candidates to suggest...
421+
candidates.extend(intrinsic_candidates);
422+
}
411423
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
412424
let mut enum_candidates: Vec<_> = self
413425
.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)