Skip to content

Filter out intrinsics if we have other import candidates to suggest #97822

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

Merged
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
14 changes: 13 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {

// Try to lookup name in more relaxed fashion for better error reporting.
let ident = path.last().unwrap().ident;
let candidates = self
let mut candidates = self
.r
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
.into_iter()
Expand All @@ -396,6 +396,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
})
.collect::<Vec<_>>();
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::")
Comment on lines +403 to +404
Copy link
Contributor

Choose a reason for hiding this comment

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

You could add a #[diagnostic_item] attribute to the intrinsics extern block and check that the parent of the suggestion is not that specific diagnostic item.

Copy link
Member Author

Choose a reason for hiding this comment

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

do I have access to diagnostic items in rustc_resolve? i don't have a tcx here, i don't think.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh right :D

})
.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)) {
let mut enum_candidates: Vec<_> = self
.r
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/resolve/filter-intrinsics.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions src/test/ui/resolve/filter-intrinsics.stderr
Original file line number Diff line number Diff line change
@@ -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`.