Skip to content

split try trait in twain - #154715

Open
bend-n wants to merge 3 commits into
rust-lang:mainfrom
bend-n:the-try.-split-it
Open

split try trait in twain#154715
bend-n wants to merge 3 commits into
rust-lang:mainfrom
bend-n:the-try.-split-it

Conversation

@bend-n

@bend-n bend-n commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

View all comments

https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/on.20.60Try.60.20for.20.60.26Option.60/with/582974682

currently Try for &Option is impossible, because from_output would need to return &Option from &Self::Output. but i think there should be some way to do Option::as_ref in a generic setting, and i think this is a good cause to split the traits up a bit, like so:

pub trait Branch {
    type Output;
    type Residual;
    fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
}
pub trait FromOutput<O = <Self as Branch>::Output>: Branch {
    fn from_output(output: O) -> Self;
}
pub trait FromResidual<R = <Self as Branch>::Residual>: Branch {
    fn from_residual(residual: R) -> Self;
}
pub trait Try = Branch + FromResidual + FromOutput;
pub trait Residual<O>: Sized {
    type TryType: Try<Output = O, Residual = Self>;
}

which then allows (this is not implemented in this PR.)

impl<'a, T, E> Branch for &'a Result<T, E> {
    type Output = &'a T;
    type Residual = Result<Infallible, &'a E>;

    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
}

which then allows

fn as_ref<R>(x: R) -> <R::Residual as Residual<<R as Branch>::Output>>::TryType
where
    R: Branch<Residual: Residual<<R as Branch>::Output>>
 {}

which then allows

let x: Result<&i32, &()> = as_ref(&Ok::<i32, ()>(1));

which i think is a great boon to Try.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Apr 4, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bend-n
bend-n force-pushed the the-try.-split-it branch from 709df2d to cb71243 Compare April 18, 2026 16:37
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 19, 2026
@bend-n
bend-n marked this pull request as ready for review April 19, 2026 14:11
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 19, 2026
@rustbot

rustbot commented Apr 19, 2026

Copy link
Copy Markdown
Collaborator

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot

rustbot commented Apr 19, 2026

Copy link
Copy Markdown
Collaborator

r? @jhpratt

rustbot has assigned @jhpratt.
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: @scottmcm, libs
  • @scottmcm, libs expanded to 7 candidates
  • Random selection from Mark-Simulacrum, jhpratt

Comment thread src/tools/rust-analyzer/crates/hir-def/src/expr_store/lower.rs
@jhpratt

jhpratt commented Apr 21, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned scottmcm and unassigned jhpratt Apr 21, 2026
@rust-bors

This comment has been minimized.

@bend-n
bend-n force-pushed the the-try.-split-it branch from 9e7b895 to 7491a82 Compare May 6, 2026 11:07
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bend-n
bend-n force-pushed the the-try.-split-it branch from 7491a82 to 57073d2 Compare May 25, 2026 15:56
@rustbot

This comment has been minimized.

@bend-n

bend-n commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@rustbot rustbot assigned Mark-Simulacrum and unassigned scottmcm May 25, 2026
@rust-log-analyzer

This comment has been minimized.

@bend-n
bend-n force-pushed the the-try.-split-it branch from 57073d2 to 368771f Compare May 25, 2026 16:37
@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum added the I-libs-nominated Nominated for discussion during a libs team meeting. label May 31, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

I know there has been a bunch of discussion about improvements to Try (e.g., something about GATs making the API friendlier), but it's not obvious to me that we want to try to further generalize it. I think it would make sense for T-libs to discuss this and figure out what the right path forward is before we spend more time on review / iteration on the specifics of the PR.

cc @scottmcm @Amanieu

@rust-bors

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-t-libs Status: Awaiting decision from T-libs and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. I-libs-nominated Nominated for discussion during a libs team meeting. labels Jun 6, 2026
@bend-n
bend-n force-pushed the the-try.-split-it branch from 368771f to d217c9a Compare August 4, 2026 13:20
@rustbot

rustbot commented Aug 4, 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.

@theemathas

Copy link
Copy Markdown
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 4, 2026
@rust-bors

rust-bors Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 65b20a9 (65b20a97de33e9e59992793a144aef2950c48c76)
Base parent: 0b63def (0b63defc8fd68957c635fe61f097b84df8fd9611)

@theemathas

Copy link
Copy Markdown
Contributor

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-154715 created and queued.
🤖 Automatically detected try build 65b20a9
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-t-libs Status: Awaiting decision from T-libs labels Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-crater Status: Waiting on a crater run to be completed. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants