Skip to content

delegation: support simplest output Self mapping - #158397

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
aerooneqq:delegation-self-type-mapping
Jul 1, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
aerooneqq:delegation-self-type-mapping

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This PR supports simplest output Self mapping for callee path if the following conditions are met (see should_wrap_return_value).

Example:

trait Trait {
    fn method(&self) -> Self;
    fn r#static() -> Self;
    fn raw_S(&self) -> S { S }
}

struct S;
impl Trait for S {
    fn method(&self) -> S { S }
    fn r#static() -> S { S }
}

struct W(S);
impl Trait for W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    //~^ WARN: function cannot return without recursing [unconditional_recursion]
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

impl W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

If the above conditions are met, there is no need to propagate generics of a newtype, as unused generics is an error, thus they should be used in a single field and it can be inferred from the return type of callee path.

Accessing signatures through queries produced query cycles in one test, so with_no_trimmed_paths was used to prevent it, though it can cause other cycles in other situations maybe.

Part of #118212.
r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 25, 2026
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Jun 25, 2026
@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Jun 29, 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.

Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
@petrochenkov petrochenkov 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 Jun 29, 2026
@aerooneqq

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Jul 1, 2026
Comment thread tests/ui/delegation/self-mapping-output.rs
Comment thread compiler/rustc_hir/src/hir.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
@petrochenkov petrochenkov 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 Jul 1, 2026
@aerooneqq

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Jul 1, 2026
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after addressing #158397 (comment) and squashing commits.
@rustbot author
@bors delegate+

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 1, 2026
@rust-bors

rust-bors Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

✌️ @aerooneqq, you can now approve this pull request!

If @petrochenkov told you to "r=me" after making some further change, then please make that change and post @bors r=petrochenkov.

View changes since this delegation.

@aerooneqq aerooneqq changed the title delegation: support simplest newtype wrapping delegation: support simplest output Self mapping Jul 1, 2026
@aerooneqq
aerooneqq force-pushed the delegation-self-type-mapping branch from ee6cfed to 92c10f9 Compare July 1, 2026 12:37
@aerooneqq

Copy link
Copy Markdown
Contributor Author

@bors r=petrochenkov

@rust-bors

rust-bors Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 92c10f9 has been approved by petrochenkov

It is now in the queue for this repository.

@rust-bors rust-bors Bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jul 1, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 1, 2026
…ping, r=petrochenkov

delegation: support simplest output `Self` mapping

This PR supports simplest output `Self` mapping for callee path if the following conditions are met (see `should_wrap_return_value`).

Example:
```rust
trait Trait {
    fn method(&self) -> Self;
    fn r#static() -> Self;
    fn raw_S(&self) -> S { S }
}

struct S;
impl Trait for S {
    fn method(&self) -> S { S }
    fn r#static() -> S { S }
}

struct W(S);
impl Trait for W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    //~^ WARN: function cannot return without recursing [unconditional_recursion]
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

impl W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

```

~If the above conditions are met, there is no need to propagate generics of a newtype, as unused generics is an error, thus they should be used in a single field and it can be inferred from the return type of callee path.~

~Accessing signatures through queries produced query cycles in one test, so `with_no_trimmed_paths` was used to prevent it, though it can cause other cycles in other situations maybe.~

Part of rust-lang#118212.
r? @petrochenkov
rust-bors Bot pushed a commit that referenced this pull request Jul 1, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #150075 (Implement clamp_to)
 - #158169 (Fix debuginfo compression in bootstrap)
 - #158397 (delegation: support simplest output `Self` mapping)
 - #158613 (Fix getrandom fallback test on platforms with `panic=abort`)
 - #158620 (Remove skip_norm_w/i/p().def_id with a helper)
 - #158633 (Remove unnecessary `Clone` derives on resolver types)
 - #158634 (Add missing `needs_drop` check to `DroplessArena`.)
 - #158647 (Document `strip_circumfix` behavior on overlapping prefix and suffix.)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 1, 2026
…ping, r=petrochenkov

delegation: support simplest output `Self` mapping

This PR supports simplest output `Self` mapping for callee path if the following conditions are met (see `should_wrap_return_value`).

Example:
```rust
trait Trait {
    fn method(&self) -> Self;
    fn r#static() -> Self;
    fn raw_S(&self) -> S { S }
}

struct S;
impl Trait for S {
    fn method(&self) -> S { S }
    fn r#static() -> S { S }
}

struct W(S);
impl Trait for W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    //~^ WARN: function cannot return without recursing [unconditional_recursion]
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

impl W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

```

~If the above conditions are met, there is no need to propagate generics of a newtype, as unused generics is an error, thus they should be used in a single field and it can be inferred from the return type of callee path.~

~Accessing signatures through queries produced query cycles in one test, so `with_no_trimmed_paths` was used to prevent it, though it can cause other cycles in other situations maybe.~

Part of rust-lang#118212.
r? @petrochenkov
rust-bors Bot pushed a commit that referenced this pull request Jul 1, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #150075 (Implement clamp_to)
 - #156716 (tests: fix: parallel frontend test failures: different alloc ids)
 - #158397 (delegation: support simplest output `Self` mapping)
 - #158613 (Fix getrandom fallback test on platforms with `panic=abort`)
 - #158620 (Remove skip_norm_w/i/p().def_id with a helper)
 - #158633 (Remove unnecessary `Clone` derives on resolver types)
 - #158634 (Add missing `needs_drop` check to `DroplessArena`.)
 - #158647 (Document `strip_circumfix` behavior on overlapping prefix and suffix.)
rust-bors Bot pushed a commit that referenced this pull request Jul 1, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #158294 (Use .drectve for MSVC DLL exports)
 - #156716 (tests: fix: parallel frontend test failures: different alloc ids)
 - #158397 (delegation: support simplest output `Self` mapping)
 - #158613 (Fix getrandom fallback test on platforms with `panic=abort`)
 - #158620 (Remove skip_norm_w/i/p().def_id with a helper)
 - #158633 (Remove unnecessary `Clone` derives on resolver types)
 - #158634 (Add missing `needs_drop` check to `DroplessArena`.)
 - #158647 (Document `strip_circumfix` behavior on overlapping prefix and suffix.)
@rust-bors
rust-bors Bot merged commit aad6334 into rust-lang:main Jul 1, 2026
13 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jul 1, 2026
rust-timer added a commit that referenced this pull request Jul 1, 2026
Rollup merge of #158397 - aerooneqq:delegation-self-type-mapping, r=petrochenkov

delegation: support simplest output `Self` mapping

This PR supports simplest output `Self` mapping for callee path if the following conditions are met (see `should_wrap_return_value`).

Example:
```rust
trait Trait {
    fn method(&self) -> Self;
    fn r#static() -> Self;
    fn raw_S(&self) -> S { S }
}

struct S;
impl Trait for S {
    fn method(&self) -> S { S }
    fn r#static() -> S { S }
}

struct W(S);
impl Trait for W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    //~^ WARN: function cannot return without recursing [unconditional_recursion]
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

impl W {
    #[attr = Inline(Hint)]
    fn method(self: _) -> _ { Self { 0: Trait::method(self.0) } }
    #[attr = Inline(Hint)]
    fn r#static() -> _ { Trait::r#static() }
    #[attr = Inline(Hint)]
    fn raw_S(self: _) -> _ { Trait::raw_S(self.0) }
}

```

~If the above conditions are met, there is no need to propagate generics of a newtype, as unused generics is an error, thus they should be used in a single field and it can be inferred from the return type of callee path.~

~Accessing signatures through queries produced query cycles in one test, so `with_no_trimmed_paths` was used to prevent it, though it can cause other cycles in other situations maybe.~

Part of #118212.
r? @petrochenkov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants