Skip to content

Constrain directly returned locals from block tail type - #161705

Open
chenyukang wants to merge 6 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-139168-return-local-constraint
Open

chenyukang wants to merge 6 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-139168-return-local-constraint

Conversation

@chenyukang

@chenyukang chenyukang commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

View all comments

Fixes #139168

This change records locals used as a function's direct tail expression and, after their initializer and pattern have been checked, constrains any remaining inference variables using the declared return type.

This allows earlier field, method, and index operations to use information from the return type without changing locals whose initializers already inferred an independent concrete type.

Also
closes #37164
closes #86562
closes #72323

Improve error diagnostic for #146399
r? @ghost

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 24, 2026
@chenyukang

Copy link
Copy Markdown
Member Author

Let's do a perf run:

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 24, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 24, 2026
…straint, r=<try>

Constrain directly returned locals from return types
@fmease fmease added T-types Relevant to the types team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Aug 24, 2026
@rust-bors

rust-bors Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 99899fe (99899fe8e8133196db8ddb939433e11afb618f21)
Base parent: 0a3fa2a (0a3fa2af35783dcd1b4206a85fe7811297eea0bf)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (99899fe): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.4%] 6
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.4%, -0.2%] 10
All ❌✅ (primary) 0.3% [0.1%, 0.4%] 6

Max RSS (memory usage)

Results (primary 4.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.1% [4.1%, 4.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.1% [4.1%, 4.1%] 1

Cycles

Results (primary -2.3%, secondary -3.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.9% [3.9%, 3.9%] 1
Improvements ✅
(primary)
-2.3% [-2.5%, -2.0%] 4
Improvements ✅
(secondary)
-5.5% [-6.2%, -4.9%] 3
All ❌✅ (primary) -2.3% [-2.5%, -2.0%] 4

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 469.763s -> 471.393s (0.35%)
Artifact size: 400.31 MiB -> 400.34 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Aug 24, 2026
@chenyukang

chenyukang commented Aug 25, 2026 •

Copy link
Copy Markdown
Member Author

621b7b6 make the solution more principled to support block, for issue #72323
but there maybe a performance regression.

Let's do a perf run:
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 25, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
…straint, r=<try>

Constrain directly returned locals from return types
@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 93e9fb8 (93e9fb8384aed543dd76d0828ca517f7d3edff69)
Base parent: 9bb55c8 (9bb55c8c865411b7d9dea6ff743e583d510d89f5)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (93e9fb8): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.2%] 6
Regressions ❌
(secondary)
0.3% [0.2%, 0.4%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 2
All ❌✅ (primary) 0.2% [0.1%, 0.2%] 6

Max RSS (memory usage)

Results (primary -3.2%, secondary -2.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
-5.9% [-9.4%, -2.5%] 2
Improvements ✅
(secondary)
-3.3% [-3.7%, -2.1%] 4
All ❌✅ (primary) -3.2% [-9.4%, 2.2%] 3

Cycles

Results (secondary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.6% [2.6%, 2.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.471s -> 471.019s (-1.14%)
Artifact size: 400.96 MiB -> 400.26 MiB (-0.18%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 25, 2026
@chenyukang

Copy link
Copy Markdown
Member Author

Removed the new added field direct_tail_local_constraint in FnCtxt, should don't have much effect on performance, but I also extend the scope to primitive type and tuple type, so let's take another perf check.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 27, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 27, 2026
…straint, r=<try>

Constrain directly returned locals from return types
@rust-bors

rust-bors Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: d546d8b (d546d8b7005bef7fa747bb5a1179f5ca1c9078f3)
Base parent: e457a7b (e457a7b0d326d67b4322ef0d11bd715cfaeda48f)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (d546d8b): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 4
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.2%, 0.2%] 4

Max RSS (memory usage)

Results (primary -2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Cycles

Results (primary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-2.2%, -2.1%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-2.2%, 2.6%] 3

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.343s -> 476.463s (0.03%)
Artifact size: 402.74 MiB -> 402.78 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 27, 2026
@chenyukang

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 27, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 27, 2026
…straint, r=<try>

Constrain directly returned locals from return types
@rust-bors

rust-bors Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: f579a16 (f579a16fee31bf092d06261b9d47e4eb55d16005)
Base parent: e457a7b (e457a7b0d326d67b4322ef0d11bd715cfaeda48f)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (f579a16): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.2%, 0.2%] 4

Max RSS (memory usage)

Results (primary -1.6%, secondary -2.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.6% [-2.4%, -0.8%] 2
Improvements ✅
(secondary)
-2.3% [-2.7%, -2.0%] 2
All ❌✅ (primary) -1.6% [-2.4%, -0.8%] 2

Cycles

Results (primary -2.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-2.3%, -2.2%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.2%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.343s -> 477.655s (0.28%)
Artifact size: 402.74 MiB -> 402.74 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 28, 2026
@chenyukang
chenyukang marked this pull request as ready for review August 28, 2026 09:46
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 28, 2026
@chenyukang

Copy link
Copy Markdown
Member Author

seems the performance regression is tolerable?
r? types

@chenyukang chenyukang changed the title Constrain directly returned locals from return types Constrain directly returned locals from block tail type Aug 28, 2026
@rust-bors

rust-bors Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #162827) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@jackh726 jackh726 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some thoughts. Reading over this, it's feeling a tad "too clever" for me, in some sense. I think it might be good to get a vibe check from the lang team - but at least with my types hat on, I'd rather not commit to this at the moment (even though it's a fairly reasonable change).

As an aside: I sort of expect tests around RPITs?

View changes since this review

.only_has_type(self)
.filter(|expected_ty| expected_ty.is_known_rigid())
.and_then(|expected_ty| {
let tail = blk.expr?.peel_drop_temps().peel_blocks().peel_drop_temps();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, this series of method calls feels...strange. (Not necessarily, but at first glance I am like "what is this doing and why")

Probably deserves a comment at least.

Comment on lines +1391 to +1408
if let ExprKind::Path(QPath::Resolved(
None,
hir::Path { res: Res::Local(binding_hir_id), .. },
)) = tail.kind
&& let Some((_, Node::LetStmt(local))) = self
.tcx
.hir_parent_iter(*binding_hir_id)
.find(|(_, node)| matches!(node, Node::LetStmt(_)))
&& local.init.is_some()
{
Some(LocalConstraint {
binding_hir_id: *binding_hir_id,
declaration_hir_id: local.hir_id,
expected_ty,
})
} else {
None
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, what's the reasoning behind only limiting the extra constraint like this?

I would probably expect a tests of the form like this (I would expect variations with generics, etc.):

#[derive(Default)]
struct Something {
    value: i32,
}

fn later_known() -> Something {
    let (mut value1, mut value2) = Default::default();
    value1 = 100;
    value2.value = 100;
    value2
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, one thing I didn't quite appreciate until just now: we're basically constraining at the let site from information we don't have until the end of the function. Also, this doesn't cover early return statements. It also doesn't cover something like:

fn foo() -> (Something, i32) {
    let mut value1 = Default::default();
    let value2 = 100;
    value1.value = 100;
    (value1, value2)
}

I'm feeling increasingly like - although this solution is clever - but that we should just steer towards diagnostics for now. That seems like it could get us 70% of the way there to a better experience while also giving us some data on when and how we can/should constrain things based on the return type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My first several version for theo original issue is given fix suggestion, then I have found other issues with similar pattern, so I tried the direction of fixing compiling.

Yes, I also feel this fix maybe not suitable as normal type checking logic, it's indeed clever, but it should be ok for diagnostic.

I'd like to create another PR to do only diagnostic based on this.

Comment on lines +1194 to +1196
if !local_ty.has_non_region_infer() {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So - in theory this shouldn't matter, right? We will eventually relate these anyways. I expect this is maybe for performance? Or diagnostics?

Comment on lines +1201 to +1205
let contains_maybe_unsized_ty =
expected_ty.walk().filter_map(|arg| arg.as_type()).any(|ty| {
!ty.has_trivial_sizedness(self.tcx, SizedTraitKind::Sized)
&& !self.type_is_sized_modulo_regions(self.param_env, ty)
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think highlights the downside of this constraint: that it's possible the user intended some coercion from the local ty to the return ty. I think there are essentially three options:

  1. Keep the status quo, where we don't constrain from the return ty. But, we could provide better (cargo fixable) diagnostics.

  2. Always constrain to the return ty directly (this includes to unsized tys). Accept that this means that the user won't see "type annotations needed" errors, but rather other kinds of errors (e.g. field not found, Default not implemented, etc.)

  3. Find an appropriate middle ground. I suppose not constraining on unsized types is an okay heuristic, because I think deref coercion is going to be hard to hit (I think, you could do it behind a 'static reference). But certainly would want to try to find some tests showing where this heuristic fails. Another problem with this specific implementation is that you can have a type that where the unsized type is already constrained but a sized part is not. Here we would fail - but I think we could succeed. Tests here would be good.

pub(crate) struct GenericIdx {}
}

struct LocalConstraint<'tcx> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be Copy

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

5 participants