From 43bd0e0ceef70a657fbd932e8d9d5343c0d6f866 Mon Sep 17 00:00:00 2001 From: zerbina <100542850+zerbina@users.noreply.github.com> Date: Sat, 17 Aug 2024 02:17:23 +0200 Subject: [PATCH] reject overloads that require conversion in `var` position (#1420) ## Summary * overloads where an argument passed to a `var` parameter requires an implicit conversion are now rejected * this results in clearer error messages ## Details The `checkConstraint` template is only called with `operand`, which is the operand before fixup (e.g., introduction of implicit conversions), hence the `isLValue` test not failing. `arg` (the operand after fixup) is now passed to `isLValue`, so that implicit conversions are taken into account. --- compiler/sem/sigmatch.nim | 2 +- .../timplicit_conversion_var_parameter.nim | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/errmsgs/timplicit_conversion_var_parameter.nim diff --git a/compiler/sem/sigmatch.nim b/compiler/sem/sigmatch.nim index e14d99681bf..89e3d6d792f 100644 --- a/compiler/sem/sigmatch.nim +++ b/compiler/sem/sigmatch.nim @@ -2818,7 +2818,7 @@ proc matchesAux(c: PContext, n, nOrig: PNode, m: var TCandidate, marker: var Int if argConverter.typ.kind notin {tyVar}: m.error.firstMismatch.kind = kVarNeeded noMatch() - elif not isLValue(c, n): + elif not isLValue(c, arg): m.error.firstMismatch.kind = kVarNeeded noMatch() diff --git a/tests/errmsgs/timplicit_conversion_var_parameter.nim b/tests/errmsgs/timplicit_conversion_var_parameter.nim new file mode 100644 index 00000000000..4061dc35966 --- /dev/null +++ b/tests/errmsgs/timplicit_conversion_var_parameter.nim @@ -0,0 +1,18 @@ +discard """ + action: reject + nimout: ''' +timplicit_conversion_var_parameter.nim(18, 2) Error: type mismatch: got +but expected one of: +proc p(x: var uint32) + first type mismatch at position: 1 + required type for x: var uint32 + but expression 'arg' is immutable, not 'var' + +expression: p(arg) +''' +""" + +proc p(x: var uint32) = discard + +var arg: uint16 # requires widening conversion +p(arg)