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

check_type_length_limit is broken #125460

Closed
lcnr opened this issue May 23, 2024 · 2 comments · Fixed by #125507
Closed

check_type_length_limit is broken #125460

lcnr opened this issue May 23, 2024 · 2 comments · Fixed by #125507
Labels
C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented May 23, 2024

The function was added to prevent hangs during codegen if there are infinite expansions which result in exponential blowup. It does that by computing an estimate of the complexity of the type and checking whether it's above a limit:

fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
let type_length = instance
.args
.iter()
.flat_map(|arg| arg.walk())
.filter(|arg| match arg.unpack() {
GenericArgKind::Type(_) | GenericArgKind::Const(_) => true,
GenericArgKind::Lifetime(_) => false,
})
.count();

This worked quite well, until we changed the walk to use caching in #72412. It looks like nobody noticed its effect on the type length limit in this PR.

With this the type length estimate grows linearly while the "true" size of the type is still exponential, causing the limit to never be reached in the cases it's intended for. The only remaining test which hits the limit arbitrarily sets it to 8 and uses a linearly recursive type:

#![type_length_limit="8"]
macro_rules! link {
($id:ident, $t:ty) => {
pub type $id = ($t, $t, $t);
}
}
link! { A1, B1 }
link! { B1, C1 }
link! { C1, D1 }
link! { D1, E1 }
link! { E1, A }
link! { A, B }
link! { B, C }
link! { C, D }
link! { D, E }
link! { E, F }
link! { F, G<Option<i32>, Option<i32>> }
pub struct G<T, K>(std::marker::PhantomData::<(T, K)>);

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 23, 2024
@lcnr
Copy link
Contributor Author

lcnr commented May 23, 2024

Only checking this for instances in monomorphization seems also insufficient to handle all cases where this causes issues. itertools ends up with enourmous instances during mir inlining, causing a hang in the new solver. These instances do not get detected evenafter fixing this check by using a type visitor

@lcnr lcnr added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label May 24, 2024
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 24, 2024
@lcnr
Copy link
Contributor Author

lcnr commented May 24, 2024

This regressed in 1.47

@lcnr lcnr added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels May 24, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue May 24, 2024
…=<try>

Re-implement a type-size based limit

r? lcnr

Fixes rust-lang#125460
bors added a commit to rust-lang-ci/rust that referenced this issue May 24, 2024
…=<try>

Re-implement a type-size based limit

r? lcnr

Fixes rust-lang#125460
@saethlin saethlin added T-types Relevant to the types team, which will review and decide on the PR/issue. C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 24, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue May 31, 2024
…=<try>

Re-implement a type-size based limit

r? lcnr

Fixes rust-lang#125460
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 12, 2024
…=<try>

Re-implement a type-size based limit

r? lcnr

Fixes rust-lang#125460
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 12, 2024
…=<try>

Re-implement a type-size based limit

r? lcnr

Fixes rust-lang#125460
@bors bors closed this as completed in c872a14 Jul 3, 2024
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 11, 2024
…=lcnr

Re-implement a type-size based limit

r? lcnr

This PR reintroduces the type length limit added in rust-lang#37789, which was accidentally made practically useless by the caching changes to `Ty::walk` in rust-lang#72412, which caused the `walk` function to no longer walk over identical elements.

Hitting this length limit is not fatal unless we are in codegen -- so it shouldn't affect passes like the mir inliner which creates potentially very large types (which we observed, for example, when the new trait solver compiles `itertools` in `--release` mode).

This also increases the type length limit from `1048576 == 2 ** 20` to `2 ** 24`, which covers all of the code that can be reached with craterbot-check. Individual crates can increase the length limit further if desired.

Perf regression is mild and I think we should accept it -- reinstating this limit is important for the new trait solver and to make sure we don't accidentally hit more type-size related regressions in the future.

Fixes rust-lang#125460
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 14, 2024
…, r=jackh726

Gate the type length limit check behind a nightly flag

Effectively disables the type length limit by introducing a `-Zenforce-type-length-limit` which defaults to **`false`**, since making the length limit actually be enforced ended up having a worse fallout than expected. We still keep the code around, but the type length limit attr is now a noop (except for its usage in some diagnostics code?).

r? `@lcnr` -- up to you to decide what team consensus we need here since this reverses an FCP decision.

Reopens rust-lang#125460 (if we decide to reopen it or keep it closed)
Effectively reverses the decision FCP'd in rust-lang#125507
Closes rust-lang#127346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants