-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Do not suggest unresolvable builder methods #125397
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Tests that we suggest the right alternatives when | ||
// a builder method cannot be resolved | ||
|
||
use std::net::TcpStream; | ||
|
||
trait SomeTrait {} | ||
|
||
struct Foo<T> { | ||
v : T | ||
} | ||
|
||
impl<T: SomeTrait + Default> Foo<T> { | ||
// Should not be suggested if constraint on the impl not met | ||
fn new() -> Self { | ||
Self { v: T::default() } | ||
} | ||
} | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
// Should be suggested | ||
fn build() -> Self { | ||
Self {} | ||
} | ||
|
||
// Method with self can't be a builder. | ||
// Should not be suggested | ||
fn build_with_self(&self) -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
mod SomeMod { | ||
use Bar; | ||
|
||
impl Bar { | ||
// Public method. Should be suggested | ||
pub fn build_public() -> Self { | ||
Self {} | ||
} | ||
|
||
// Private method. Should not be suggested | ||
fn build_private() -> Self { | ||
Self {} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
// `new` not found on `TcpStream` and `connect` should be suggested | ||
let _stream = TcpStream::new(); | ||
//~^ ERROR no function or associated item named `new` found | ||
|
||
// Although `new` is found on `<impl Foo<T>>` it should not be | ||
// suggested because `u8` does not meet the `T: SomeTrait` constraint | ||
let _foo = Foo::<u8>::new(); | ||
//~^ ERROR the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied | ||
|
||
// Should suggest only `<impl Bar>::build()` and `SomeMod::<impl Bar>::build_public()`. | ||
// Other methods should not suggested because they are private or are not a builder | ||
let _bar = Bar::new(); | ||
//~^ ERROR no function or associated item named `new` found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
error[E0599]: no function or associated item named `new` found for struct `TcpStream` in the current scope | ||
--> $DIR/suggest-builder-fn.rs:52:29 | ||
| | ||
LL | let _stream = TcpStream::new(); | ||
| ^^^ function or associated item not found in `TcpStream` | ||
| | ||
note: if you're trying to build a new `TcpStream` consider using one of the following associated functions: | ||
TcpStream::connect | ||
TcpStream::connect_timeout | ||
--> $SRC_DIR/std/src/net/tcp.rs:LL:COL | ||
|
||
error[E0599]: the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied | ||
--> $DIR/suggest-builder-fn.rs:57:27 | ||
| | ||
LL | struct Foo<T> { | ||
| ------------- function or associated item `new` not found for this struct | ||
... | ||
LL | let _foo = Foo::<u8>::new(); | ||
| ^^^ function or associated item cannot be called on `Foo<u8>` due to unsatisfied trait bounds | ||
| | ||
note: trait bound `u8: SomeTrait` was not satisfied | ||
--> $DIR/suggest-builder-fn.rs:12:9 | ||
| | ||
LL | impl<T: SomeTrait + Default> Foo<T> { | ||
| ^^^^^^^^^ ------ | ||
| | | ||
| unsatisfied trait bound introduced here | ||
|
||
error[E0599]: no function or associated item named `new` found for struct `Bar` in the current scope | ||
--> $DIR/suggest-builder-fn.rs:62:21 | ||
| | ||
LL | struct Bar; | ||
| ---------- function or associated item `new` not found for this struct | ||
... | ||
LL | let _bar = Bar::new(); | ||
| ^^^ function or associated item not found in `Bar` | ||
| | ||
note: if you're trying to build a new `Bar` consider using one of the following associated functions: | ||
Bar::build | ||
SomeMod::<impl Bar>::build_public | ||
--> $DIR/suggest-builder-fn.rs:23:5 | ||
| | ||
LL | fn build() -> Self { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | pub fn build_public() -> Self { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Now that we're just passing in the expr id, I feel like
rcvr_opt: Option<_>
,SelfSource: _
,args: Option<_>
are all redundant, since they can all be derived from the expression pointed to by theexpr_id
.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.
And the span too
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.
I looked into it. Not much is derivable from
expr_id
.These are the two calls to
report_method_error
:rust/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Lines 835 to 845 in 9cdfe28
rust/compiler/rustc_hir_typeck/src/expr.rs
Lines 1347 to 1357 in 9cdfe28
As you can see
rcvr_opt
(second arg) is being explicitly passed asNone
in one case andSome(rcvr)
in the other. Similarlyrcvr_ty
(third arg) isty.normalized
in one case andrcvr_ty
in the other and both are obtained in different ways.source
(fifth arg) too has different values with somewhat convoluted provenance and the same can be said ofargs
(seventh arg).So I don't feel we should change any of this.
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.
SelfSource
,rcvr_opt
, andargs
are all a property of whether theexpr_id
(which really should be passed in as a wholehir::Expr
) isExprKind::MethodCall
orExprKind::Path
.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.
Similarly,
segment.ident
should be derivable from that expression (it's directly stored in theMethodCall
and is also as the final segment inExprKind::Path
, or something like that).I don't think I said to touch
rcvr_ty
, which can continue to be passed in.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.
Similarly,
span
can be derived from the expression without much trouble as well.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.
Okay, let me have another look at it
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.
@compiler-errors Turns out the
expr_id
isn't necessarily pointing to anExpr
always. it could also be aPat
becausecheck_pat
also callsreport_method_error
(check_pat
->resolve_ty_and_res_fully_qualified_call
->report_method_error
) . So if I were to derive all these bits fromexpr_id
I'll have to takePat
into account in addition toExpr
, which means I might have to repeat code like this fromresolve_ty_and_res_fully_qualified_call
:rust/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Lines 749 to 772 in 78dd504
within
report_method_error
. I have a feeling that complicates things instead of making them simpler 😫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.
That didn't end up being too complicated, for the record: 2227c1f