Skip to content

Commit cade928

Browse files
committed
improving error feedback (though the info is not present anyway)
1 parent d35ef21 commit cade928

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

key.core/src/main/java/de/uka/ilkd/key/proof/init/FinalFieldCodeValidator.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import de.uka.ilkd.key.logic.op.IProgramMethod;
1919
import de.uka.ilkd.key.logic.op.ProgramMethod;
2020
import de.uka.ilkd.key.logic.op.ProgramVariable;
21+
import de.uka.ilkd.key.parser.Location;
22+
import de.uka.ilkd.key.util.parsing.LocatableException;
2123

2224
import org.key_project.logic.SyntaxElement;
2325
import org.key_project.util.collection.IdentityHashSet;
@@ -177,7 +179,8 @@ private void validateNew(New _new) {
177179
// This also disallows things like "a.new B()" which would not like this. However,
178180
// KeY cannot deal with this anyway, so we can do the easy check here.
179181
throw new FinalViolationException(
180-
"Call to non-static inner class " + classType + " leaks 'this' to the constructor",
182+
"Constructor call to non-static inner class " + classType.getFullName() +
183+
" leaks 'this' to the constructor",
181184
_new);
182185
}
183186

@@ -204,12 +207,14 @@ private void validateMethodReference(MethodReference methodReference) {
204207

205208
if (hasThisArgument) {
206209
throw new FinalViolationException(
207-
"Method call " + methodReference + " leaks 'this' to called method.",
210+
"Method call to " + methodReference.getName() + " leaks 'this' to called method.",
208211
methodReference);
209212
}
210213

211214
if (calledOnThis) {
212215
IProgramMethod method = findMethod(methodReference);
216+
assert !method.isConstructor()
217+
: "Constructor calls should have been handled by the New handler above.";
213218
if (method.isStatic() || method.isConstructor()) {
214219
// local static methods are acutally fine ...
215220
// constructor calls are also fine
@@ -219,7 +224,9 @@ private void validateMethodReference(MethodReference methodReference) {
219224
if (!method.isFinal() && !method.isPrivate()
220225
&& !((ClassType) enclosingClass.getJavaType()).isFinal()) {
221226
throw new FinalViolationException(
222-
"Method called on 'this' that is not effectively final.", methodReference);
227+
"Method to " + method.getFullName() +
228+
" called on 'this' that is not effectively final.",
229+
methodReference);
223230
}
224231
validate(method);
225232
}
@@ -266,25 +273,24 @@ private void validateFieldReference(FieldReference fieldReference) {
266273
validateChildren(fieldReference);
267274
}
268275

269-
static class FinalViolationException extends RuntimeException {
270-
271-
private final PositionInfo position;
276+
static class FinalViolationException extends LocatableException {
272277

273278
public FinalViolationException(String message) {
274279
this(message, null);
275280
}
276281

277282
public FinalViolationException(String message, SyntaxElement syntaxElement) {
278-
super(message);
279-
if (syntaxElement instanceof SourceElement sourceElement) {
280-
this.position = sourceElement.getPositionInfo();
281-
} else {
282-
this.position = null;
283-
}
283+
super(message, computeLocation(syntaxElement));
284284
}
285285

286-
public PositionInfo getPositionInfo() {
287-
return position;
286+
private static Location computeLocation(SyntaxElement syntaxElement) {
287+
if (syntaxElement instanceof SourceElement sourceElement) {
288+
PositionInfo posInfo = sourceElement.getPositionInfo();
289+
var uri = posInfo.getURI().orElse(null);
290+
var pos = posInfo.getStartPosition();
291+
return new Location(uri, pos);
292+
}
293+
return Location.UNDEFINED;
288294
}
289295
}
290296
}

key.core/src/main/java/de/uka/ilkd/key/proof/init/FinalFieldsPOExtension.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ public Term modifyPostTerm(AbstractOperationPO abstractPO, InitConfig proofConfi
6161
: "Expected a ProgramMethod not a schema variable, since we need the actual implementation";
6262
ProgramMethod constructor = (ProgramMethod) iconstructor;
6363

64+
List<JFunction> finalFields = findFinalFields(iconstructor, services);
65+
if (finalFields.isEmpty()) {
66+
// If there are no final fields, we do not need to do anything
67+
return postTerm;
68+
}
69+
6470
FinalFieldCodeValidator.validateFinalFields(constructor, proofConfig);
6571

6672
TermBuilder tb = services.getTermBuilder();
67-
List<JFunction> finalFields = findFinalFields(iconstructor, services);
6873
Term self = tb.var(selfVar);
6974
for (JFunction finalField : finalFields) {
7075
Term fieldRef = tb.tf().createTerm(finalField);

0 commit comments

Comments
 (0)