Skip to content

preserve overflow in builtin Field candidates - #162332

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
amirHdev:fix-field-projection-overflow
Oct 3, 2026
Merged

rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
amirHdev:fix-field-projection-overflow

Conversation

@amirHdev

@amirHdev amirHdev commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

preserve overflow from the builtin Field candidate instead of treating it as NoSolution
the Sized requirements are now evaluated inside the candidate probe and coherence correctly rejects overlapping impls when evaluation overflows.

fixes #162125

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Sep 5, 2026
@rust-log-analyzer

This comment has been minimized.

@amirHdev
amirHdev force-pushed the fix-field-projection-overflow branch from b1a0cdc to 907d8f7 Compare September 5, 2026 13:26
@amirHdev
amirHdev marked this pull request as ready for review September 14, 2026 09:00
@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 Sep 14, 2026
@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 76 candidates
  • Random selection from 18 candidates

@BennoLossin

Copy link
Copy Markdown
Contributor

Thanks for fixing this, I also figured out the solution in the zulip thread and I think our code looks pretty much the same, except for early return style vs bigger if.

my solution
    fn consider_builtin_field_candidate(
        ecx: &mut EvalCtxt<'_, D>,
        goal: Goal<I, Self>,
    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
        if goal.predicate.polarity != ty::ClausePolarity::Positive {
            return Err(NoSolution.into());
        }

        if let ty::Adt(def, args) = goal.predicate.self_ty().kind()
            && let Some(FieldInfo { base, ty, .. }) =
                def.field_representing_type_info(ecx.cx(), args)
            && match base.kind() {
                ty::Adt(def, _) => def.is_struct() && !def.is_packed(),
                ty::Tuple(..) => true,
                _ => false,
            }
        {
            ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
                let cx = ecx.cx();
                let sized_trait = ecx.cx().require_trait_lang_item(SolverTraitLangItem::Sized);
                // FIXME: add better support for builtin impls of traits that check for the bounds
                // on the trait definition in std.

                // NOTE: these bounds have to be kept in sync with the definition of the `Field`
                // trait in `library/core/src/field.rs` as well as the old trait solver `fn
                // assemble_candidates_for_field_trait` in
                // `compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs`.
                ecx.add_goal(
                    GoalSource::ImplWhereBound,
                    goal.with(cx, ty::TraitRef::new(ecx.cx(), sized_trait, [base])),
                )?;
                ecx.add_goal(
                    GoalSource::ImplWhereBound,
                    goal.with(cx, TraitRef::new(ecx.cx(), sized_trait, [ty])),
                )?;
                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
            })
        } else {
            Err(NoSolution.into())
        }
    }

Maybe @lcnr would make for a better reviewer?

@amirHdev

Copy link
Copy Markdown
Contributor Author

Closing this since I realized @BennoLossin had already worked it out :)

I missed that in the Zulip thread
Thanks for digging into it, Benno

@amirHdev amirHdev closed this Sep 17, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 17, 2026
@BennoLossin

Copy link
Copy Markdown
Contributor

Ah no worries, I was glad that I didn't have to spin up a PR :) so feel free to reopen this one

@BennoLossin

Copy link
Copy Markdown
Contributor

@amirHdev do you want to finish this work? if so, just reopen the PR

@amirHdev

Copy link
Copy Markdown
Contributor Author

Reopening as suggested thanks Benno :) I'll finish this up here

And thanks for the earlier Zulip investigation as well

@amirHdev amirHdev reopened this Sep 17, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 17, 2026
@JohnTitor

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned JonathanBrouwer and unassigned JohnTitor Sep 19, 2026
@BennoLossin

BennoLossin commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

let's see if I'm able to do this, if not, @amirHdev lcnr is probably the most appropriate reviewer for this.

r? @lcnr

edit: yay it worked :)

@rustbot rustbot assigned lcnr and unassigned JonathanBrouwer Sep 19, 2026
match base.kind() {
ty::Adt(def, _) if def.is_struct() && !def.is_packed() => {}
ty::Tuple(..) => {}
_ => return Err(NoSolution.into()),

@lcnr lcnr Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what if that's an infer var @BennoLossin, is that possible?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

base comes from args.type_at(0)
it can still be an infer var here.

I'll move this check back inside the probe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yeah that sounds correct

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

actually, if the field representing type info exists, then base is always an adt or tuple, so I don't think htis needs to be moved in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yeah, this should handle infer vars then 😁 and we should get a test where that happens, maybe by makig this match ICE on ty::Infer and then getting a test to panic.

also, what are other allowed base types here. Could we make this match exhaustive/unreachable! in the catch all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't understand why we need to handle infer vars here. the type should be concrete... what would an example of ty::Infer be? something like this?

#![feature(field_projections)]
use std::field::field_of;
fn main() {
    let _: field_of!(_, field);
}
error: cannot use `_` in this position
 --> src/main.rs:4:22
  |
4 |     let _: field_of!(_, field);
  |   

@lcnr lcnr Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

didn't read your second message in depth 😅

okay, then this should be an unreachable!. That's also fine with me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

as field_representing_type_info only gives us an ADT or tuple here we can keep the other ADT cases as NoSolution and made everything else unreachable!()

@lcnr lcnr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me on the refactor 👍

View changes since this review

@lcnr

lcnr commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 1, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 2, 2026
@amirHdev
amirHdev requested a review from BennoLossin October 2, 2026 13:42
@lcnr

lcnr commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 8bd4b8a has been tentatively approved by lcnr

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 2, 2026
@rust-bors

rust-bors Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

❌ Commit 8bd4b8a has been unapproved due to PR CI failure. Reapprove it with @bors r+ force if you want to ignore the failure.

@rust-log-analyzer

This comment has been minimized.

Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
@amirHdev
amirHdev force-pushed the fix-field-projection-overflow branch from 8bd4b8a to 01ad9b7 Compare October 2, 2026 15:15
@rustbot

rustbot commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@amirHdev

amirHdev commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@lcnr lol. the CI failure was stale stderr after the recent diagnostic span change
fixed

@lcnr

lcnr commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 01ad9b7 has been tentatively approved by lcnr

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 2, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 2, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #163655 (explicitly handle tests that pass with -Znext-solver)
 - #159924 (Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc)
 - #163572 (Update the minimum external LLVM to 22)
 - #129822 (Docs - type guarantees update)
 - #157973 (Distinguish `repr(C)` ZSTs from others in ABI compatibility rules)
 - #162332 (preserve overflow in builtin Field candidates)
 - #163574 (intrinsics: Rename `abort` to `abort_immediate`)
 - #163638 (avoid trivial `fn map_bound` validations)
 - #163660 (yeet compare-mode-coherence)
rust-bors Bot pushed a commit that referenced this pull request Oct 3, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #163655 (explicitly handle tests that pass with -Znext-solver)
 - #159924 (Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc)
 - #163572 (Update the minimum external LLVM to 22)
 - #129822 (Docs - type guarantees update)
 - #157973 (Distinguish `repr(C)` ZSTs from others in ABI compatibility rules)
 - #162332 (preserve overflow in builtin Field candidates)
 - #163574 (intrinsics: Rename `abort` to `abort_immediate`)
 - #163638 (avoid trivial `fn map_bound` validations)
 - #163660 (yeet compare-mode-coherence)
@rust-bors
rust-bors Bot merged commit bb9cac2 into rust-lang:main Oct 3, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 3, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 3, 2026
Rollup merge of #162332 - amirHdev:fix-field-projection-overflow, r=lcnr

preserve overflow in builtin Field candidates

preserve overflow from the builtin Field candidate instead of treating it as `NoSolution`
the Sized requirements are now evaluated inside the candidate probe and coherence correctly rejects overlapping impls when evaluation overflows.

fixes #162125
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Oct 5, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#163655 (explicitly handle tests that pass with -Znext-solver)
 - rust-lang/rust#159924 (Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc)
 - rust-lang/rust#163572 (Update the minimum external LLVM to 22)
 - rust-lang/rust#129822 (Docs - type guarantees update)
 - rust-lang/rust#157973 (Distinguish `repr(C)` ZSTs from others in ABI compatibility rules)
 - rust-lang/rust#162332 (preserve overflow in builtin Field candidates)
 - rust-lang/rust#163574 (intrinsics: Rename `abort` to `abort_immediate`)
 - rust-lang/rust#163638 (avoid trivial `fn map_bound` validations)
 - rust-lang/rust#163660 (yeet compare-mode-coherence)
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Oct 5, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#163655 (explicitly handle tests that pass with -Znext-solver)
 - rust-lang/rust#159924 (Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc)
 - rust-lang/rust#163572 (Update the minimum external LLVM to 22)
 - rust-lang/rust#129822 (Docs - type guarantees update)
 - rust-lang/rust#157973 (Distinguish `repr(C)` ZSTs from others in ABI compatibility rules)
 - rust-lang/rust#162332 (preserve overflow in builtin Field candidates)
 - rust-lang/rust#163574 (intrinsics: Rename `abort` to `abort_immediate`)
 - rust-lang/rust#163638 (avoid trivial `fn map_bound` validations)
 - rust-lang/rust#163660 (yeet compare-mode-coherence)
programskillforverification pushed a commit to programskillforverification/miri that referenced this pull request Oct 5, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#163655 (explicitly handle tests that pass with -Znext-solver)
 - rust-lang/rust#159924 (Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc)
 - rust-lang/rust#163572 (Update the minimum external LLVM to 22)
 - rust-lang/rust#129822 (Docs - type guarantees update)
 - rust-lang/rust#157973 (Distinguish `repr(C)` ZSTs from others in ABI compatibility rules)
 - rust-lang/rust#162332 (preserve overflow in builtin Field candidates)
 - rust-lang/rust#163574 (intrinsics: Rename `abort` to `abort_immediate`)
 - rust-lang/rust#163638 (avoid trivial `fn map_bound` validations)
 - rust-lang/rust#163660 (yeet compare-mode-coherence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

field_projections incorrectly handles recursion limits

7 participants