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

fix: don't suggest similar method when unstable #109212

Merged
merged 1 commit into from
Mar 19, 2023
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
9 changes: 9 additions & 0 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
true
}
})
// ensure that we don't suggest unstable methods
.filter(|candidate| {
// note that `DUMMY_SP` is ok here because it is only used for
// suggestions and macro stuff which isn't applicable here.
!matches!(
self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this call might be introducing a cycle, which manifests as an .unwrap() ICE. Can you check if calling tcx.lookup_stability(def_id) also hits it?

Copy link
Contributor Author

@Ezrashaw Ezrashaw Mar 16, 2023

Choose a reason for hiding this comment

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

Yeah, that's what I was vaguely thinking. tcx.lookup_stability isn't what we want either, it calls eval_stability internally and emits an error if the DefId is unstable (definitely not what we want). (EDIT: I'm talking about check_stability, lookup_stabilty is the low-level one 🤦) I'm not sure how to fix the cycle.

stability::EvalResult::Deny { .. }
)
})
.map(|candidate| candidate.item.ident(self.tcx))
.filter(|&name| set.insert(name))
.collect();
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/stability-attribute/auxiliary/similar-unstable-method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(staged_api)]
#![stable(feature = "libfoo", since = "1.0.0")]

#[unstable(feature = "foo", reason = "...", issue = "none")]
pub fn foo() {}

#[stable(feature = "libfoo", since = "1.0.0")]
pub struct Foo;

impl Foo {
#[unstable(feature = "foo", reason = "...", issue = "none")]
pub fn foo(&self) {}
}
13 changes: 13 additions & 0 deletions tests/ui/stability-attribute/issue-109177.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// aux-build: similar-unstable-method.rs

extern crate similar_unstable_method;

fn main() {
// FIXME: this function should not suggest the `foo` function.
similar_unstable_method::foo1();
//~^ ERROR cannot find function `foo1` in crate `similar_unstable_method` [E0425]

let foo = similar_unstable_method::Foo;
foo.foo1();
//~^ ERROR no method named `foo1` found for struct `Foo` in the current scope [E0599]
}
21 changes: 21 additions & 0 deletions tests/ui/stability-attribute/issue-109177.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0425]: cannot find function `foo1` in crate `similar_unstable_method`
--> $DIR/issue-109177.rs:7:30
|
LL | similar_unstable_method::foo1();
| ^^^^ help: a function with a similar name exists: `foo`
|
::: $DIR/auxiliary/similar-unstable-method.rs:5:1
|
LL | pub fn foo() {}
| ------------ similarly named function `foo` defined here

error[E0599]: no method named `foo1` found for struct `Foo` in the current scope
--> $DIR/issue-109177.rs:11:9
|
LL | foo.foo1();
| ^^^^ method not found in `Foo`

error: aborting due to 2 previous errors

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