Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ static private class OverrideCheckResult {
// inherited functions used for override checks only, skip reporting
boolean skipErrorReporting = false;

// errors for functions with same name that it does not override
io.vavr.collection.List<String> overrideErrors = io.vavr.collection.List.empty();
// functions with same name that it does not override, latest first; why it does not is worked out only
// where it is reported, for a function marked override which overrides nothing
io.vavr.collection.List<FailedOverride> failedOverrides = io.vavr.collection.List.empty();

public void addError(String error) {
this.overrideErrors = overrideErrors.prepend(error);
public void addFailedOverride(FuncLink overridden, boolean allowStaticOverride) {
this.failedOverrides = failedOverrides.prepend(new FailedOverride(overridden, allowStaticOverride));
}
}

private record FailedOverride(FuncLink overridden, boolean allowStaticOverride) {
}

/**
* The links of one name come out in the order they were added: the structure's own names, then
* those of its module instantiations, its superclass and its interfaces. Callers which find
Expand Down Expand Up @@ -94,9 +98,10 @@ private static void reportOverrideErrors(Map<String, Map<FuncLink, OverrideCheck
}
if (f.attrIsOverride() && !check.doesOverride) {
StringBuilder msg = new StringBuilder("Function " + f.getName() + " does not override anything.");
for (String overrideError : check.overrideErrors) {
for (FailedOverride failed : check.failedOverrides) {
msg.append("\n ");
msg.append(overrideError);
msg.append(WurstValidator.checkOverride(e.getKey(), failed.overridden(),
failed.allowStaticOverride()));
}
f.addError(msg.toString());
} else if (!f.attrIsOverride() && check.doesOverride) {
Expand Down Expand Up @@ -173,15 +178,14 @@ private static void addNewNameLinks(Multimap<String, DefLink> result, Map<String
FuncLink otherFunc = e2.getKey();
OverrideCheckResult checkResult = e2.getValue();

String error = WurstValidator.checkOverride(otherFunc, func, allowStaticOverride);
if (error == null) {
if (WurstValidator.canOverride(otherFunc, func, allowStaticOverride)) {
checkResult.doesOverride = true;
if (!(func.getReceiverType() instanceof WurstTypeInterface)) {
checkResult.requiresOverrideMod = true;
}
isOverridden = true;
} else {
checkResult.addError(error);
checkResult.addFailedOverride(func, allowStaticOverride);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ public static <T> T getLast(List<T> ts) {

public static <T extends Element> String printElement(Optional<T> el) {
return el.map(e -> {
String type = makeReadableTypeName(e);
// The readable type name costs three regular expressions and most kinds of element are
// printed without it, so it is made only for those which print it.
String type = null;
String name = "";
if (e instanceof ExprFunctionCall fc) {
return "function call " + fc.getFuncName() + "()";
Expand Down Expand Up @@ -285,7 +287,7 @@ public static <T extends Element> String printElement(Optional<T> el) {
}
type = "type";
}
return type + " " + name;
return (type != null ? type : makeReadableTypeName(e)) + " " + name;
}).orElse("null");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4052,60 +4052,77 @@ private void checkCodeArrays(TypeExprArray e) {
* checks if func1 can override func2
*/
public static boolean canOverride(FuncLink func1, FuncLink func2, boolean allowStaticOverride) {
return checkOverride(func1, func2, allowStaticOverride) == null;
return overrideError(func1, func2, allowStaticOverride, false) == null;
}

/**
* checks if func1 can override func2
* <p>
* Returns null if yes and an error message if not.
*/
public static String checkOverride(FuncLink func1, FuncLink func2, boolean allowStaticOverride) {
public static @Nullable String checkOverride(FuncLink func1, FuncLink func2, boolean allowStaticOverride) {
return overrideError(func1, func2, allowStaticOverride, true);
}

/** What {@link #overrideError} returns in place of a message which nobody reads. */
private static final String CANNOT_OVERRIDE = "cannot override";

/**
* Null if func1 can override func2, else why not; with {@code explain} false only {@link #CANNOT_OVERRIDE}.
* Most pairs a lookup compares are overloads which cannot override each other, and the message names the
* overridden function with its position, so a check which only needs the answer does not build it.
*/
private static @Nullable String overrideError(FuncLink func1, FuncLink func2, boolean allowStaticOverride,
boolean explain) {
if (!allowStaticOverride) {
if (func1.isStatic()) {
return "Static method " + func1.getName() + " cannot override other methods.";
return explain
? "Static method " + func1.getName() + " cannot override other methods."
: CANNOT_OVERRIDE;
}
if (func2.isStatic()) {
return "Static " + Utils.printElementWithSource(Optional.of(func2.getDef())) + " cannot be overridden.";
return explain
? "Static " + Utils.printElementWithSource(Optional.of(func2.getDef())) + " cannot be overridden."
: CANNOT_OVERRIDE;
}
} else {
if (func1.isStatic() && !func2.isStatic()) {
return "Static method "
return explain ? "Static method "
+ func1.getName()
+ " cannot override dynamic "
+ Utils.printElementWithSource(Optional.of(func2.getDef()))
+ ".";
+ "." : CANNOT_OVERRIDE;
} else if (!func1.isStatic() && func2.isStatic()) {
return "Method "
return explain ? "Method "
+ func1.getName()
+ " cannot override static "
+ Utils.printElementWithSource(Optional.of(func2.getDef()))
+ ".";
+ "." : CANNOT_OVERRIDE;
}
}
if (func1.isVarargMethod() && !func2.isVarargMethod()) {
return "Vararg method "
return explain ? "Vararg method "
+ func1.getName()
+ " cannot override non-vararg method "
+ Utils.printElementWithSource(Optional.of(func2.getDef()))
+ ".";
+ "." : CANNOT_OVERRIDE;
}
if (!func1.isVarargMethod() && func2.isVarargMethod()) {
return "Non-vararg method "
return explain ? "Non-vararg method "
+ func1.getName()
+ " cannot override vararg method "
+ Utils.printElementWithSource(Optional.of(func2.getDef()))
+ ".";
+ "." : CANNOT_OVERRIDE;
}
int paramCount2 = func2.getParameterTypes().size();
int paramCount1 = func1.getParameterTypes().size();
if (paramCount1 != paramCount2) {
return Utils.printElement(func2.getDef()) + " takes " + paramCount2
+ " parameters, but there are only " + paramCount1 + " parameters here.";
return explain ? Utils.printElement(func2.getDef()) + " takes " + paramCount2
+ " parameters, but there are only " + paramCount1 + " parameters here." : CANNOT_OVERRIDE;
}

// the functions' own type parameters are equal up to renaming
String typeParameterError = checkOwnTypeParameters(func1.getDef(), func2.getDef());
String typeParameterError = checkOwnTypeParameters(func1.getDef(), func2.getDef(), explain);
if (typeParameterError != null) {
return typeParameterError;
}
Expand All @@ -4116,18 +4133,18 @@ public static String checkOverride(FuncLink func1, FuncLink func2, boolean allow
WurstType type1 = func1.getParameterType(i);
WurstType type2 = renaming.isEmpty() ? func2.getParameterType(i) : func2.getParameterType(i).setTypeArgs(renaming);
if (!type1.isSupertypeOf(type2, func1.getDef())) {
return "Parameter " + type1 + " " + func1.getParameterName(i) + " should have type " + type2
+ " to override " + Utils.printElementWithSource(Optional.of(func2.getDef())) + ".";
return explain ? "Parameter " + type1 + " " + func1.getParameterName(i) + " should have type " + type2
+ " to override " + Utils.printElementWithSource(Optional.of(func2.getDef())) + "." : CANNOT_OVERRIDE;
}
}
// covariant return types
WurstType returnType2 = renaming.isEmpty() ? func2.getReturnType() : func2.getReturnType().setTypeArgs(renaming);
if (!func1.getReturnType().isSubtypeOf(returnType2, func1.getDef())) {
return "Return type should be "
return explain ? "Return type should be "
+ func2.getReturnType()
+ " to override "
+ Utils.printElementWithSource(Optional.of(func2.getDef()))
+ ".";
+ "." : CANNOT_OVERRIDE;
}
// no error
return null;
Expand All @@ -4136,22 +4153,25 @@ public static String checkOverride(FuncLink func1, FuncLink func2, boolean allow
/**
* Checks that the type parameters {@code overriding} declares itself are those {@code overridden} declares, up to
* renaming: as many, each of the same kind of generic with the same bounds. Returns null if they are, and otherwise
* the error which says why {@code overriding} cannot override {@code overridden}.
* the error which says why {@code overriding} cannot override {@code overridden} (with {@code explain} false only
* {@link #CANNOT_OVERRIDE}).
*/
private static @Nullable String checkOwnTypeParameters(FunctionDefinition overriding, FunctionDefinition overridden) {
private static @Nullable String checkOwnTypeParameters(FunctionDefinition overriding, FunctionDefinition overridden,
boolean explain) {
List<TypeParamDef> ownTypeParams1 = ownTypeParameters(overriding);
List<TypeParamDef> ownTypeParams2 = ownTypeParameters(overridden);
if (ownTypeParams1.size() != ownTypeParams2.size()) {
return Utils.printElement(overridden) + " takes " + ownTypeParams2.size()
+ " type parameters, but there are " + ownTypeParams1.size() + " type parameters here.";
return explain ? Utils.printElement(overridden) + " takes " + ownTypeParams2.size()
+ " type parameters, but there are " + ownTypeParams1.size() + " type parameters here."
: CANNOT_OVERRIDE;
}
VariableBinding renaming = ownTypeParameterRenaming(overriding, overridden);
for (int i = 0; i < ownTypeParams1.size(); i++) {
TypeParamDef tp1 = ownTypeParams1.get(i);
TypeParamDef tp2 = ownTypeParams2.get(i);
if (!sameTypeParamConstraints(tp1, tp2, renaming)) {
return "Type parameter " + printTypeParam(tp1) + " should be " + printTypeParam(tp2)
+ " to override " + Utils.printElementWithSource(Optional.of(overridden)) + ".";
return explain ? "Type parameter " + printTypeParam(tp1) + " should be " + printTypeParam(tp2)
+ " to override " + Utils.printElementWithSource(Optional.of(overridden)) + "." : CANNOT_OVERRIDE;
}
}
return null;
Expand Down
Loading