Repository navigation
Constrain directly returned locals from block tail type - #161705
chenyukang wants to merge 6 commits into
Conversation
|
Let's do a perf run: @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…straint, r=<try> Constrain directly returned locals from return types
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -2.3%, secondary -3.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 469.763s -> 471.393s (0.35%) |
|
621b7b6 make the solution more principled to support block, for issue #72323 Let's do a perf run: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…straint, r=<try> Constrain directly returned locals from return types
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (secondary 2.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 476.471s -> 471.019s (-1.14%) |
|
Removed the new added field @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…straint, r=<try> Constrain directly returned locals from return types
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -0.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 476.343s -> 476.463s (0.03%) |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…straint, r=<try> Constrain directly returned locals from return types
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -2.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 476.343s -> 477.655s (0.28%) |
|
seems the performance regression is tolerable? |
|
☔ The latest upstream changes (presumably #162827) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
There was a problem hiding this comment.
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?
| .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(); |
There was a problem hiding this comment.
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.
| 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 | ||
| } |
There was a problem hiding this comment.
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
}There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| if !local_ty.has_non_region_infer() { | ||
| return; | ||
| } |
There was a problem hiding this comment.
So - in theory this shouldn't matter, right? We will eventually relate these anyways. I expect this is maybe for performance? Or diagnostics?
| 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) | ||
| }); |
There was a problem hiding this comment.
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:
-
Keep the status quo, where we don't constrain from the return ty. But, we could provide better (
cargo fixable) diagnostics. -
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,
Defaultnot implemented, etc.) -
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
'staticreference). 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> { |
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