Skip to content

A trait impl method that omits its return type is checked against the trait's as a written one is; one report per return mismatch - #3598

Merged
ehartford merged 1 commit into
mainfrom
trait-impl-ret
Nov 24, 2026
Merged

ehartford merged 1 commit into
mainfrom
trait-impl-ret

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Found while sizing item 1 of #3061 (part of #3061 and #2519: both want to remove a return type the trait already declares, and that form was broken).

The bug. impl Size for i64: fn size(): self under fn size(self: &Self) -> i64 was an internal compiler error (invalid MIR: a &i64 assigned to an i64). With the return written, it compiled.

Root cause. In check_fn_decl's body check (src/SemaCheck.with), a method with no written return and a trait contract set its signature from the contract and stopped: check_annotated_body_result ran only on the written branch. So the tail was never checked or adjusted against the declared return (the copy out of a &Self tail, the implicit Ok), and check_body_result was told the function had no declared return, which also skipped the owned-returned-as-view check.

The fix. The trait's return is the declared return for the whole body check: it is passed to check_body_under_return as one, and check_annotated_body_result runs on the contract branch.

Also. A written return that the tail did not meet was reported twice (a bare return type mismatch, then the one naming both types). check_body_result now leaves a declared return's mismatch to the check that names the types and reports only what that check lets by.

Tests: behav_trait_impl_omitted_return_is_the_traits (a &Self tail, an integer literal under f64, a bare value under Result and Option, Self), err_trait_impl_omitted_return_mismatch. Both fail on main.

Evidence at 7227f2e: both compilers check the source; release build, :unit-return-review, :ceremony-census, :self-audit, :native-compile-error-tests, :behavior-tests green; battery green; :release-checks survey: all targets green. Sema only: no platform leg.

… trait's as a written one is; one report per return mismatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant