lib/modules: Move the isDefined check into mergedValue

Without this change, accessing `mergedValue` from `mergeDefinitions` in
case there are no definitions will throw an error like

  error: evaluation aborted with the following error message: 'This case should never happen.'

This change makes it throw the appropriate error

  error: The option `foo' is used but not defined.

This is fully backwards compatible.
This commit is contained in:
Silvan Mosberger 2019-10-12 19:32:59 +02:00 committed by Silvan Mosberger
parent 112eea6b90
commit 130a0c9878
No known key found for this signature in database
GPG Key ID: E8F1E9EAD284E17D

View File

@ -365,16 +365,9 @@ rec {
else else
mergeDefinitions loc opt.type defs'; mergeDefinitions loc opt.type defs';
# The value with a check that it is defined
valueDefined = if res.isDefined then res.mergedValue else
# (nixos-option detects this specific error message and gives it special
# handling. If changed here, please change it there too.)
throw "The option `${showOption loc}' is used but not defined.";
# Apply the 'apply' function to the merged value. This allows options to # Apply the 'apply' function to the merged value. This allows options to
# yield a value computed from the definitions # yield a value computed from the definitions
value = if opt ? apply then opt.apply valueDefined else valueDefined; value = if opt ? apply then opt.apply res.mergedValue else res.mergedValue;
in opt // in opt //
{ value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value; { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
@ -408,11 +401,17 @@ rec {
}; };
defsFinal = defsFinal'.values; defsFinal = defsFinal'.values;
# Type-check the remaining definitions, and merge them. # Type-check the remaining definitions, and merge them. Or throw if no definitions.
mergedValue = foldl' (res: def: mergedValue =
if type.check def.value then res if isDefined then
else throw "The option value `${showOption loc}' in `${def.file}' is not of type `${type.description}'.") foldl' (res: def:
(type.merge loc defsFinal) defsFinal; if type.check def.value then res
else throw "The option value `${showOption loc}' in `${def.file}' is not of type `${type.description}'."
) (type.merge loc defsFinal) defsFinal
else
# (nixos-option detects this specific error message and gives it special
# handling. If changed here, please change it there too.)
throw "The option `${showOption loc}' is used but not defined.";
isDefined = defsFinal != []; isDefined = defsFinal != [];