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

Don't ascend into parent bodies when collecting stmts for possible return suggestion #124037

Merged
merged 1 commit into from
Apr 17, 2024
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
8 changes: 6 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,12 +2016,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
for (span, code) in errors_causecode {
self.dcx().try_steal_modify_and_emit_err(span, StashKey::MaybeForgetReturn, |err| {
if let Some(fn_sig) = self.body_fn_sig()
&& let ExprBindingObligation(_, _, hir_id, ..) = code
&& let ExprBindingObligation(_, _, binding_hir_id, ..) = code
&& !fn_sig.output().is_unit()
{
let mut block_num = 0;
let mut found_semi = false;
for (_, node) in self.tcx.hir().parent_iter(hir_id) {
for (hir_id, node) in self.tcx.hir().parent_iter(binding_hir_id) {
// Don't proceed into parent bodies
if hir_id.owner != binding_hir_id.owner {
break;
}
match node {
hir::Node::Stmt(stmt) => {
if let hir::StmtKind::Semi(expr) = stmt.kind {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/inference/dont-collect-stmts-from-parent-body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// issue: rust-lang/rust#124022

struct Type<T>;
//~^ ERROR type parameter `T` is never used

fn main() {
{
impl<T> Type<T> {
fn new() -> Type<T> {
Type
//~^ ERROR type annotations needed
}
}
};
}
24 changes: 24 additions & 0 deletions tests/ui/inference/dont-collect-stmts-from-parent-body.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0392]: type parameter `T` is never used
--> $DIR/dont-collect-stmts-from-parent-body.rs:3:13
|
LL | struct Type<T>;
| ^ unused type parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead

error[E0282]: type annotations needed
--> $DIR/dont-collect-stmts-from-parent-body.rs:10:17
|
LL | Type
| ^^^^ cannot infer type of the type parameter `T` declared on the struct `Type`
|
help: consider specifying the generic argument
|
LL | Type::<T>
| +++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0282, E0392.
For more information about an error, try `rustc --explain E0282`.
Loading