Skip to content

Commit 4b7fab4

Browse files
committed
GROOVY-11688: Potential minor improvement to STC error message
1 parent e42a2a7 commit 4b7fab4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,13 +3137,13 @@ private static LambdaExpression constructLambdaExpressionForMethodReference(fina
31373137
private void checkNamedParamsAnnotation(final Parameter param, final MapExpression args) {
31383138
if (!isOrImplements(param.getType(), MAP_TYPE)) return;
31393139
List<MapEntryExpression> entryExpressions = args.getMapEntryExpressions();
3140-
Map<Object, Expression> entries = new LinkedHashMap<>();
3140+
Map<Object, MapEntryExpression> entries = new LinkedHashMap<>();
31413141
for (MapEntryExpression entry : entryExpressions) {
31423142
Object key = entry.getKeyExpression();
31433143
if (key instanceof ConstantExpression) {
31443144
key = ((ConstantExpression) key).getValue();
31453145
}
3146-
entries.put(key, entry.getValueExpression());
3146+
entries.put(key, entry);
31473147
}
31483148
List<String> collectedNames = new ArrayList<>();
31493149
List<AnnotationNode> annotations = param.getAnnotations(NAMED_PARAMS_CLASSNODE);
@@ -3177,21 +3177,22 @@ private void checkNamedParamsAnnotation(final Parameter param, final MapExpressi
31773177
}
31783178
}
31793179
if (!collectedNames.isEmpty()) {
3180-
for (Map.Entry<Object, Expression> entry : entries.entrySet()) {
3181-
if (!collectedNames.contains(entry.getKey())) {
3182-
addStaticTypeError("unexpected named arg: " + entry.getKey(), args);
3180+
for (Map.Entry<Object, MapEntryExpression> entry : entries.entrySet()) {
3181+
Object name = entry.getKey();
3182+
if (!collectedNames.contains(name)) {
3183+
addStaticTypeError("unexpected named arg: " + name, entry.getValue());
31833184
}
31843185
}
31853186
}
31863187
}
31873188

3188-
private void processNamedParam(final AnnotationConstantExpression value, final Map<Object, Expression> entries, final Expression expression, final List<String> collectedNames) {
3189+
private void processNamedParam(final AnnotationConstantExpression value, final Map<Object, MapEntryExpression> entries, final MapExpression args, final List<String> collectedNames) {
31893190
AnnotationNode namedParam = (AnnotationNode) value.getValue();
31903191
if (!namedParam.getClassNode().getName().equals(NamedParam.class.getName())) return;
3191-
processNamedParam(namedParam, entries, expression, collectedNames);
3192+
processNamedParam(namedParam, entries, args, collectedNames);
31923193
}
31933194

3194-
private void processNamedParam(final AnnotationNode namedParam, final Map<Object, Expression> entries, final Expression expression, final List<String> collectedNames) {
3195+
private void processNamedParam(final AnnotationNode namedParam, final Map<Object, MapEntryExpression> entries, final MapExpression args, final List<String> collectedNames) {
31953196
String name = null;
31963197
boolean required = false;
31973198
ClassNode expectedType = null;
@@ -3210,12 +3211,13 @@ private void processNamedParam(final AnnotationNode namedParam, final Map
32103211
}
32113212
if (!entries.containsKey(name)) {
32123213
if (required) {
3213-
addStaticTypeError("required named param '" + name + "' not found.", expression);
3214+
addStaticTypeError("required named param '" + name + "' not found.", args);
32143215
}
32153216
} else if (expectedType != null) {
3216-
ClassNode argumentType = getDeclaredOrInferredType(entries.get(name));
3217+
MapEntryExpression entry = entries.get(name);
3218+
ClassNode argumentType = getDeclaredOrInferredType(entry.getValueExpression());
32173219
if (!isAssignableTo(argumentType, expectedType)) {
3218-
addStaticTypeError("argument for named param '" + name + "' has type '" + prettyPrintType(argumentType) + "' but expected '" + prettyPrintType(expectedType) + "'.", expression);
3220+
addStaticTypeError("argument for named param '" + name + "' has type '" + prettyPrintType(argumentType) + "' but expected '" + prettyPrintType(expectedType) + "'.", entry);
32193221
}
32203222
}
32213223
}

0 commit comments

Comments
 (0)