Skip to content

Commit 0db3155

Browse files
committed
Improve Constant Not Found Error
1 parent 905cc4f commit 0db3155

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

liquidjava-verifier/src/main/java/liquidjava/diagnostics/LJDiagnostic.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class LJDiagnostic extends RuntimeException {
1414
private final String customMessage;
1515
private String file;
1616
private SourcePosition position;
17+
private String hint;
1718
private static final String PIPE = " | ";
1819

1920
public LJDiagnostic(String title, String message, SourcePosition pos, String accentColor, String customMessage) {
@@ -38,7 +39,7 @@ public String getCustomMessage() {
3839
}
3940

4041
public String getDetails() {
41-
return ""; // to be overridden by subclasses
42+
return hint != null ? hint : "";
4243
}
4344

4445
public SourcePosition getPosition() {
@@ -60,6 +61,10 @@ public String getAccentColor() {
6061
return accentColor;
6162
}
6263

64+
public void setHint(String hint) {
65+
this.hint = hint;
66+
}
67+
6368
@Override
6469
public String toString() {
6570
StringBuilder sb = new StringBuilder();

liquidjava-verifier/src/main/java/liquidjava/diagnostics/errors/NotFoundError.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public NotFoundError(String name, String kind) {
1818
this(null, name, kind, null);
1919
}
2020

21+
public NotFoundError(SourcePosition position, String name, String kind) {
22+
this(position, name, kind, null);
23+
}
24+
2125
public NotFoundError(SourcePosition position, String name, String kind, TranslationTable translationTable) {
2226
super("Not Found Error", String.format("%s '%s' not found", kind, name), position, translationTable);
2327
this.name = Utils.getSimpleName(name);

liquidjava-verifier/src/main/java/liquidjava/rj_language/Predicate.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import liquidjava.api.CommandLineLauncher;
1010
import liquidjava.diagnostics.errors.LJError;
11+
import liquidjava.diagnostics.errors.NotFoundError;
1112
import liquidjava.processor.context.AliasWrapper;
1213
import liquidjava.processor.context.Context;
1314
import liquidjava.processor.context.GhostFunction;
@@ -103,12 +104,12 @@ private static Expression resolveStaticFinalConstants(Expression root, CtElement
103104
// unresolvable reference — throw an error with a helpful message and import suggestion if possible
104105
SourcePosition pos = context == null ? null : Utils.getLJAnnotationPosition(context, en.toString());
105106
String suggested = StaticConstants.findImportCandidate(en.getTypeName(), en.getConstName(), context);
106-
String hint = suggested != null ? "add: import " + suggested + ";"
107-
: "add an import for '" + en.getTypeName() + "' if it is a Java class with a static final field.";
108-
throw new liquidjava.diagnostics.errors.CustomError(
109-
String.format("Could not resolve '%s.%s' in refinement. If you meant the static final constant, %s",
110-
en.getTypeName(), en.getConstName(), hint),
111-
pos);
107+
String hint = suggested != null ? "Add: import " + suggested + ";"
108+
: "Add an import for '" + en.getTypeName() + "' if it is a Java class with a static final field";
109+
String name = en.getTypeName() + "." + en.getConstName();
110+
NotFoundError error = new NotFoundError(pos, name, "Constant");
111+
error.setHint(hint);
112+
throw error;
112113
}
113114
return root;
114115
}

0 commit comments

Comments
 (0)