lcnr
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # non-fatal overflow in the new solver *this is a collection of notes for [the final overflow doc](https://hackmd.io/ATf4hN0NRY-w2LIVgeFsVg)* --- The new solver should avoid hangs as much as possible. It must consider constraints from overflowing branches because of https://github.com/rust-lang/trait-system-refactor-initiative/issues/70. Overflow can happen in multiple places: - [evaluating a nested goal: stack depth](https://github.com/rust-lang/rust/blob/8abf920985368264ed4d46e62e1730232e161292/compiler/rustc_trait_selection/src/solve/search_graph.rs#L151-L158) - [normalizing the self type](https://github.com/rust-lang/rust/blob/8abf920985368264ed4d46e62e1730232e161292/compiler/rustc_trait_selection/src/solve/assembly/mod.rs#L360-L386) - [`try_normalize_ty`](https://github.com/rust-lang/rust/blob/8abf920985368264ed4d46e62e1730232e161292/compiler/rustc_trait_selection/src/solve/mod.rs#L313-L315) - [`try_evaluate_added_goals`](https://github.com/rust-lang/rust/blob/8abf920985368264ed4d46e62e1730232e161292/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs#L445-L460) - [solver cycle fixpoint](https://github.com/rust-lang/rust/blob/8abf920985368264ed4d46e62e1730232e161292/compiler/rustc_trait_selection/src/solve/search_graph.rs#L281-L307) The main concern with non-fatal overflow is how we should handle exponential blowup. This can be caused in multiple different ways yet again: - multiple candidates for a trait or project goals - multiple nested goals of a trait or project candidate - multiple nominal obligations from well-formed goals ## more complex overflow issues These blowup and overflow sources can be combined for even more fun. ### solver fixpoint + try_evaluate_added_goals https://github.com/rust-lang/rust/pull/118774 ```rust trait Trait {} struct W<T: ?Sized>(*const T); impl<T: ?Sized> Trait for W<W<T>> where W<T>: Trait, W<T>: Trait, {} fn impls_trait<T: Trait>() {} fn main() { impls_trait::<W<_>>(); //~^ ERROR overflow evaluating the requirement } ``` ### solver cycle fixpoint at each level + multiple nested goals ```rust #![feature(rustc_attrs)] #![allow(internal_features)] #[rustc_coinductive] trait Trait {} struct W<T: ?Sized>(T); impl<T: ?Sized> Trait for W<W<T>> where Self: Assistant, W<T>: Trait, { } #[rustc_coinductive] trait Assistant {} impl<T: ?Sized> Assistant for W<T> where T: Assistant, Self: Trait, { } fn impls_trait<T: Trait + ?Sized>() {} fn main() { impls_trait::<W<_>>(); } ``` ### solver cycle fixpoint at each level + multiple candidates ```rust // This has exponential growth because of the growing impl, // even it does not apply. trait Trait {} struct W<T>(T); struct U<T>(T); trait NotImplemented {} impl<T> Trait for W<T> where W<W<T>>: Trait, W<T>: NotImplemented, {} impl<T: Other> Trait for T {} trait Other {} impl<T: Other + Trait> Other for W<T> {} impl Other for () {} fn impls_trait<T: Trait + ?Sized>() {} fn main() { impls_trait::<W<_>>(); } ``` ### overflow in `try_evaluate_added_goals` and anything else Rerunning overflowing goals after applying their constraints very easily result in hangs, because we recompute the overflowing goal at each loop, increasing the size of the inferred type even more, e.g: `ui/traits/new-solver/overflow/exponential-trait-goals.rs` ```rust trait Trait {} struct W<T>(T); impl<T, U> Trait for W<(W<T>, W<U>)> where W<T>: Trait, W<U>: Trait, { } fn impls<T: Trait>() {} fn main() { impls::<W<_>>(); } ``` This also results in unstable results. Stopping to apply inference constraints because of overflow allows the solver to make additional progress the next time the goal is computed. ## `assemble_candidates_after_normalizing_self_ty` and `try_normalize_ty` blowup For cyclic projections, normalizing the self type results in `recursion_depth` nested `Projection(Alias, ?new_infer)` goals. `?new_infer` gets instantiated as `Alias` which (due to the way the current impl is set up, ends up resulting in a nested `AliasRelate(Alias, Alias)` goal, which again normalizes the alias resulting in `recursion_depth` many nested goals. ```rust trait Overflow<U: ?Sized> { type Assoc; } impl<U: ?Sized> Overflow<U> for () { type Assoc: = <() as Overflow<(U,)>>::Assoc; } fn main() {} ``` This results in nested alias relate goals because when generalizing, the generalized types has no unresolved inference variables while the original one does, preventing the structural eq fast path from firing when equating at the end of `CombineFields::instantiate`. The following diff prevents that overflow ```diff --- a/compiler/rustc_trait_selection/src/solve/project_goals/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals/mod.rs @@ -227,6 +227,7 @@ fn consider_impl_candidate( // // And then map these args to the args of the defining impl of `Assoc`, going // from `[u32, u64]` to `[u32, i32, u64]`. + let impl_args = ecx.resolve_vars_if_possible(impl_args); let impl_args_with_gat = goal.predicate.projection_ty.args.rebase_onto( tcx, goal_trait_ref.def_id, ``` ## random thoughts and summary - we need to apply inference constraints even if there's overflow for [backcompat](https://github.com/rust-lang/trait-system-refactor-initiative/issues/7) - `try_evaluate_added_goals` causes other overflow to very quickly result in hangs. Overflowing nested goals have to be heavily penalized or avoided. - changing the layout of the proof tree after stabilization is theoretically breaking, either because of hangs or because we stop visiting paths which are not visited anymore. - avoiding the recomputation of parts of the proof tree is fully backwards compatible and something we can do after stabilization - normalization needs the "full depth" - cycle handling must not allow the full depth as it [otherwise hangs](https://github.com/rust-lang/rust/blob/master/tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.rs) - we will probably readd a provisional cache at some point, this may reduce the cost of the cycle fixpoint step - ignoring constaints from overflow is very good for perf, may specialcase the constraints from where-clauses ## new idea - stash goals resulting in overflow in `try_evaluate_added_goals` and avoid evaluating them in following evaluations - do the same in fulfillment - maybe also try to prove them once more at the end ## cache usage of the new solver current implementation (without dependencies) as of 2023.12.04. | crate | overflow | global cache | cycle | compute | | ----- | -------- | ------------ | ----- | ------- | | syn :negative_squared_cross_mark: | 0 | 470628 | 0 | 49688 | | rand (slightly changed) :heavy_check_mark: | 0 | 118929 | 8586 | 29173 | | serde :negative_squared_cross_mark:| 0 | 4032167 | 0 | 122229 | | bitflags :heavy_check_mark: | 0 | 10901 | 0 | 3477 | | regex-syntax :heavy_check_mark: | 0 | 400924 | 28 | 51540 | | regex-automata :negative_squared_cross_mark: | 0 | 826261 | 4 | 139092 | | typenum :heavy_check_mark: | 76924 | 345245 | 4854 | 65233 | ## IDEA: Learning from CTFE Trait solving has similar constraints to CTFE. We can use a similar approach to CTFE to avoid hangs in a backwards compatible way. Have a simple counter in the trait solver, which is incremented whenever we evaluate a nested goal. If that counter hits some arbitrary limit, we emit a `deny` by default lint telling the user that the solver seems to be hanging due to their code. If that lint results in an error (i.e. has not been changed to allow/warn), we abort compilation. If it has been allowed or changed to warn, we repeatedly emit a warning with some exponential backoff. :warning: This has one significant issue: the number of evaluated nested goals is not a good approximation of the solver runtime: `typenum` has 95000 uncached goal evaluations in less than a second. `tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.rs` hangs with less than 700 uncached goal evaluations :warning: The time needed to evaluate a single goal can differ widely depending on the amount and size of inference constraints from nested goals. However, we can combine this counter with a size check of constraints, or include the size of constraints when incrementing the counter. ## IDEA: split "overflow" and "recursion limit based overflow" Only yeet constraints from recursion limit based overflow. This does not avoid the hang in `tests/ui/traits/new-solver/cycles/coinduction/fixpoint-exponential-growth.rs`. ## Many crates depend on the "overflow project where bounds" behavior https://github.com/rust-lang/trait-system-refactor-initiative/issues/70 may just be acceptable breakage. It may be very positive for perf, at least doing it for recursion limit based overflow. https://github.com/rust-lang/rust/issues/90662 was originally caused by only causing this pattern to error for global goals (if it otherwise cycles), this broke https://github.com/AzureMarker/shaku. It feels likely that always doing so is too impactful. ## IDEA: checking the size of the var_values constraints When canonicalizing a response, check the size of the `var_values` and discard them if they grow too large, resulting in overflow. This can result in bistable cycle fixpoint computations, but that seems alright. ## TODO: impl header eq constraints old solver Write tests where we rely on these constraints both for Projection and Trait goals, nested goals either resulting in inductive cycle or hitting the recurison limit (should also be fine if there's just a single candidate). also write tests where we rely on these constraints from a nested goal. So we need the impl header eq constraints of a goal from the where-bounds. ## QUESTION: Can we delay stabilizing our non-fatal overflow behavior I personally think it is acceptable to delay the stabilization of a "ready" implementation of `-Ztrait-solver=next-coherence` to collect more data while working on full `-Ztrait-solver=next`. We should still publish a blogpost asking for testing and stating that it is ready for stabilization. We cannot completly avoid non-fatal overflow in `Ztrait-solver=next-coherence` as `typenum` should continue to compile. We could emit a `deny`-by default lint when people depend on our current overflow handling. This lint would hit quite a few crates however, so it's not ideal. There's also the question of whether and where to drop constraints from overflow. If this behavior should affect non-recursion depth based overflow, e.g. inductive cycle fixpoints, then we either have to pretty much decide on that behavior already. ## Advantages of stabilizing `-Ztrait-solver=next-coherence` - remove the coherence support from the old solver, reducing complexity - get more testing of the new solver, at least for the behavior relied upon in coherence - milestone showing that the new solver is making progress, alleviate concerns about the type system being stuck - fix bugs in coherence and have a sensible behavior wrt to binders. Mostly negligable: https://hackmd.io/ABcskdRCRj6WuE3TeX9zEQ - the positive impact is overwhelmingly social. there are limited technical benefits from stabilizing it.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully