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.
This commit is contained in:
commit
0ee15040d5
3 changed files with 48 additions and 1 deletions
|
@ -396,7 +396,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||||
|
|
||||||
// Try to lookup name in more relaxed fashion for better error reporting.
|
// Try to lookup name in more relaxed fashion for better error reporting.
|
||||||
let ident = path.last().unwrap().ident;
|
let ident = path.last().unwrap().ident;
|
||||||
let candidates = self
|
let mut candidates = self
|
||||||
.r
|
.r
|
||||||
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
|
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -408,6 +408,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let crate_def_id = CRATE_DEF_ID.to_def_id();
|
let crate_def_id = CRATE_DEF_ID.to_def_id();
|
||||||
|
// Try to filter out intrinsics candidates, as long as we have
|
||||||
|
// some other candidates to suggest.
|
||||||
|
let intrinsic_candidates: Vec<_> = candidates
|
||||||
|
.drain_filter(|sugg| {
|
||||||
|
let path = path_names_to_string(&sugg.path);
|
||||||
|
path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
if candidates.is_empty() {
|
||||||
|
// Put them back if we have no more candidates to suggest...
|
||||||
|
candidates.extend(intrinsic_candidates);
|
||||||
|
}
|
||||||
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
|
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
|
||||||
let mut enum_candidates: Vec<_> = self
|
let mut enum_candidates: Vec<_> = self
|
||||||
.r
|
.r
|
||||||
|
|
10
src/test/ui/resolve/filter-intrinsics.rs
Normal file
10
src/test/ui/resolve/filter-intrinsics.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
fn main() {
|
||||||
|
// Should suggest only `std::mem::size_of`
|
||||||
|
let _ = size_of::<usize>();
|
||||||
|
//~^ ERROR cannot find
|
||||||
|
|
||||||
|
// Should suggest `std::intrinsics::fabsf64`,
|
||||||
|
// since there is no non-intrinsic to suggest.
|
||||||
|
let _ = fabsf64(1.0);
|
||||||
|
//~^ ERROR cannot find
|
||||||
|
}
|
25
src/test/ui/resolve/filter-intrinsics.stderr
Normal file
25
src/test/ui/resolve/filter-intrinsics.stderr
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
error[E0425]: cannot find function `size_of` in this scope
|
||||||
|
--> $DIR/filter-intrinsics.rs:3:13
|
||||||
|
|
|
||||||
|
LL | let _ = size_of::<usize>();
|
||||||
|
| ^^^^^^^ not found in this scope
|
||||||
|
|
|
||||||
|
help: consider importing this function
|
||||||
|
|
|
||||||
|
LL | use std::mem::size_of;
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0425]: cannot find function `fabsf64` in this scope
|
||||||
|
--> $DIR/filter-intrinsics.rs:8:13
|
||||||
|
|
|
||||||
|
LL | let _ = fabsf64(1.0);
|
||||||
|
| ^^^^^^^ not found in this scope
|
||||||
|
|
|
||||||
|
help: consider importing this function
|
||||||
|
|
|
||||||
|
LL | use std::intrinsics::fabsf64;
|
||||||
|
|
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0425`.
|
Loading…
Add table
Add a link
Reference in a new issue