-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix: Complete methods on all unsuffixed numeric literals #21329
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes method completion for unsuffixed numeric literals to show methods from all applicable numeric types rather than just the default inferred type.
- Adds constructor methods for all numeric builtin types (i8-i128, u8-u128, f16-f128) in HIR
- Enhances dot completion logic to check methods across all integer types for integer literals (e.g.,
23.) and all float types for float literals (e.g.,2.0.) - Implements deduplication using
FxHashSetto prevent duplicate method suggestions
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/hir/src/lib.rs | Adds constructor methods for all numeric builtin types (i8, i16, i64, i128, isize, u8, u16, u32, u64, u128, usize, f16, f32, f64, f128) |
| crates/ide-completion/src/completions/dot.rs | Implements multi-type method completion for unsuffixed numeric literals with deduplication logic |
| crates/ide-completion/src/tests/expression.rs | Adds test cases for integer and float literal method completion |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Thanks for the reminder — sorry, I missed the disclosure requirement. I’ll add the required AI‑tools disclosure, fix the issue it just mentioned , and reopen the PR shortly. |
|
@ChayimFriedman2 Thanks for the reminder and sorry for missing the disclosure earlier. What I changed
I have reopened the PR — please let me know if anything else is needed. |
ChayimFriedman2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely don't enumerate methods for all types. That'll lead to a lot of effectively duplicated methods. Choose one type - probably f64. Look at how this is implemented for unsuffixed integers.
Got it, Thanks for the feedback! I'm enumerating all numeric types to discover traits implemented only on non-default types (e.g., a trait only on f32, not f64) - these would actually compile due to type inference but would be missed if we only check f64. To avoid duplicate inherent methods (multiple
|
This comment has been minimized.
This comment has been minimized.
Fixed method completion for unsuffixed numeric literals to show methods from all applicable types, not just the default inferred type.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Use FieldKind in DotAccessKind, replace the boolean receiver_is_ambiguous_float_literal with a FieldKind enum
016bdf4 to
d5f9ced
Compare
|
Hey! @ChayimFriedman2, I just looked back at this PR and have two questions. 1) About your comment
I may have misunderstood your previous comment. Are you saying there isn't an issue with unsuffixed integers? I couldn't find any relevant existing implementation, and I believe integers have the same issue. Could you point me to the specific code you're referring to? 2) Some limitationsIt works when there's a single implementation: trait OnlyI64 { fn method(&self); }
impl OnlyI64 for i64 {}
fn foo() {
23.method() // ✅ Compiles (infers i64)
}But with multiple implementations, Rust falls back to the default type (i32/f64): For example, this only compiles if trait Multi { fn method(&self); }
impl Multi for u64 {}
impl Multi for i64 {}
fn foo() {
23.method()
}Currently, my implementation doesn't detect whether it's a single-impl or multi-impl situation. It just shows all trait methods from all integer(or float) types. The question: Is it worth adding extra complexity to detect and avoid this ambiguous case? Given that:
|
Yes. I meant #20110, which isn't exactly the same but is similar in spirit.
Nope. Just list all methods for |
Got it! But I want to clarify my understanding: Current behavior (before my PR):
Same issue exists for integers:
Example of the problem: trait OnlyF32 { fn method(&self); }
impl OnlyF32 for f32 {}
trait OnlyU32 { fn method(&self); }
impl OnlyU32 for u32 {}
fn foo() {
2.0.method() // ✅ Compiles (type inference picks f32)
// ❌ But no completion shown
2.method() // ✅ Compiles (type inference picks u32)
// ❌ But no completion shown
}Should I close this PR, since before my PR it already listed all methods for |
|
Oh, I must admit that I misread the issue, and now I believe that we don't want to fix this issue. Sorry for dragging you into implementing useless work! @rust-lang/rust-analyzer just confirm you're okay with closing this and the issue as "won't fix"? |
|
No problem, and thanks for the review! |
Close: #21312
Release Note:
This PR fixes method completion for unsuffixed numeric literals (like
23.and2.0.) to show methods from all applicable numeric types, not just the default inferred type.Problem
Previously:
23.only showed methods fromi32(noti64,u64, etc.)2.0.only showed methods fromf64(notf32,f16, etc.)This made #21312 occured.
Solution
Now checks methods on ALL applicable types:
23.): Checks all integer types (i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize)2.0.): Checks all float types (f16, f32, f64, f128)FxHashSet<Function>to ensure each method appears only onceChanges
crates/hir/src/lib.rs: Added constructor methods for all numeric builtin typescrates/ide-completion/src/completions/dot.rs: Enhanced completion logic with deduplicationcrates/ide-completion/src/tests/expression.rs: Added comprehensive testsExample
Before:
After:
Usage of AI tools
I implemented the logic and wrote the code.
I used AI only to brainstorm ideas , propose variable/struct names, and draft inline comments.
I manually reviewed and edited every AI suggestion before committing. No code was blindly accepted from the tool.