From 44eafb29537d5c3d786915a17f0c05dddec17afb Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Tue, 26 May 2026 13:32:12 -0400 Subject: [PATCH] lib.types: remove throwIf usage throwIf sends our error message through a function call, even if the error condition doesn't trigger. This requires a lot of thunk allocation that can be easily avoided. --- lib/types.nix | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 41acfd907854..e7a8763983a1 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -21,7 +21,6 @@ let isString substring sort - throwIf toDerivation toList types @@ -660,10 +659,10 @@ rec { inStore ? null, absolute ? null, }: - throwIf (inStore != null && absolute != null && inStore && !absolute) - "In pathWith, inStore means the path must be absolute" - mkOptionType - { + if inStore != null && absolute != null && inStore && !absolute then + throw "In pathWith, inStore means the path must be absolute" + else + mkOptionType { name = "path"; description = ( (if absolute == null then "" else (if absolute then "absolute " else "relative ")) @@ -1079,8 +1078,8 @@ rec { builtins.addErrorContext "while checking that attrTag tag ${lib.strings.escapeNixIdentifier n} is an option with a type${inAttrPosSuffix tags_ n}" ( - throwIf (opt._type or null != "option") - "In attrTag, each tag value must be an option, but tag ${lib.strings.escapeNixIdentifier n} ${ + if opt._type or null != "option" then + throw "In attrTag, each tag value must be an option, but tag ${lib.strings.escapeNixIdentifier n} ${ if opt ? _type then if opt._type == "option-type" then "was a bare type, not wrapped in mkOption." @@ -1089,23 +1088,24 @@ rec { else "was not." }" + else opt - // { - declarations = - opt.declarations or ( - let - pos = builtins.unsafeGetAttrPos n tags_; - in - if pos == null then [ ] else [ pos.file ] - ); - declarationPositions = - opt.declarationPositions or ( - let - pos = builtins.unsafeGetAttrPos n tags_; - in - if pos == null then [ ] else [ pos ] - ); - } + // { + declarations = + opt.declarations or ( + let + pos = builtins.unsafeGetAttrPos n tags_; + in + if pos == null then [ ] else [ pos.file ] + ); + declarationPositions = + opt.declarationPositions or ( + let + pos = builtins.unsafeGetAttrPos n tags_; + in + if pos == null then [ ] else [ pos ] + ); + } ) ) tags_; choicesStr = concatMapStringsSep ", " lib.strings.escapeNixIdentifier (attrNames tags);