Skip to content

Integer min and max are library methods: the MinMax trait's default bodies (part of #3483) - #3613

Merged
ehartford merged 1 commit into
mainfrom
builtins-minmax
Nov 25, 2026
Merged

ehartford merged 1 commit into
mainfrom
builtins-minmax

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Part of #3483; the issue stays open.

a.min(b) and a.max(b) on an integer were a compiler intrinsic with a row in BuiltinSigs. They are now two default methods of a trait in lib/std/traits.with, implemented for the twelve integer types by one bodiless impl line each:

pub trait MinMax:
    fn min(self: &Self, other: Self) -> Self: if other < *self: other else: *self
    fn max(self: &Self, other: Self) -> Self: if other > *self: other else: *self

Deleted: the integer dispatch and the name-based return type in SemaCheck, the two BuiltinSigs rows, and the integer branch of the LLVM arm. Float min / max stay the built-in (llvm.minnum / maxnum: one instruction, with its NaN rule), and the C backend's arm is shared with floats and stays.

No slowdown (the issue's condition): a 400-million-iteration loop of .min(i).max(…) runs in 0.40 s built with main and 0.40 s built with this, same result.

Test: behav_3483_int_min_max_in_library: unsigned operands compare unsigned (#511), a literal takes the receiver's width, the type constants i64.max / i8.min are still the constants, float min is unchanged.

Not gained: min and max inside a comptime fn. They were refused before ("not comptime-evaluable yet") and still are; a trait cannot hold a comptime fn today.

Evidence at 21f87da: 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.

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