-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Print type of every call in a method call chain #96918
Closed
+58
−1
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1621,6 +1621,36 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { | |
"type mismatch resolving `{}`", | ||
predicate | ||
); | ||
let mut v = HirFinder { needle: obligation.cause.span, found: None }; | ||
let body = self.tcx.hir().get(obligation.cause.body_id); | ||
if let hir::Node::Expr(expr) = body { | ||
v.visit_expr(expr); | ||
} | ||
if let Some(expr) = v.found { | ||
// FIXME(estebank): look for method calls that can be applied on the previous | ||
// element of the chain, that would make the next call to typeck. | ||
let mut receiver = expr; | ||
if let (Some(typeck_results), hir::ExprKind::MethodCall(..)) = | ||
(self.in_progress_typeck_results, &receiver.kind) | ||
{ | ||
let typeck_results = typeck_results.borrow(); | ||
while let hir::ExprKind::MethodCall(method_segment, args, _) = receiver.kind { | ||
if let Some(ty) = typeck_results.expr_ty_adjusted_opt(receiver) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if resolving |
||
diag.span_label( | ||
method_segment.ident.span, | ||
&format!("this call is of type `{ty}`"), | ||
); | ||
} | ||
receiver = &args[0]; | ||
} | ||
while let hir::ExprKind::Call(path, _args) = receiver.kind { | ||
if let Some(ty) = typeck_results.expr_ty_adjusted_opt(receiver) { | ||
diag.span_label(path.span, &format!("this call is of type `{ty}`")); | ||
} | ||
receiver = &path; | ||
} | ||
} | ||
} | ||
let secondary_span = match predicate.kind().skip_binder() { | ||
ty::PredicateKind::Projection(proj) => self | ||
.tcx | ||
|
@@ -2689,3 +2719,24 @@ impl<'tcx> ty::TypeVisitor<'tcx> for HasNumericInferVisitor { | |
} | ||
} | ||
} | ||
|
||
struct HirFinder<'tcx> { | ||
needle: Span, | ||
found: Option<&'tcx hir::Expr<'tcx>>, | ||
} | ||
|
||
impl<'v> Visitor<'v> for HirFinder<'v> { | ||
fn visit_expr(&mut self, expr: &'v hir::Expr<'v>) { | ||
if expr.span == self.needle { | ||
self.found = Some(expr); | ||
return; | ||
} | ||
if let hir::ExprKind::MethodCall(segment, _, _) = expr.kind { | ||
if segment.ident.span == self.needle { | ||
self.found = Some(expr); | ||
return; | ||
} | ||
} | ||
hir::intravisit::walk_expr(self, expr); | ||
} | ||
} |
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
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.
@compiler-errors I think that this is the part that would be useful from this PR, that this PR didn't handle: enabling rustc to look for method calls that could be added to make the code compile would be the biggest benefit of this change, but this PR on its own might not be as useful as I envisioned.
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.
At least in the case of #33941, suggesting other methods that are compatible with the real
Item
type doesn't seem like the right direction for a suggestion to go imo.I think the compiler should be trying to stress that that
(&K, &V)
is not&T
instead. I'm not sure how to make the connection more clear that if they want(&K, &V) -> (K, V)
they've gotta do the map themselves... now I don't think this needs special-casing per se, but conversely, I think suggesting other methods is probably going to lead them further astray?