Skip to content

Commit f8e2525

Browse files
author
dlsmith
committed
DynamicJava: Nested interfaces are always static. Fixed ambiguity error occurring when the same field is inherited from two different supertypes. Improved "ambiguous name" error messages.
git-svn-id: file:///tmp/test-svn/trunk@5085 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 8c59b6d commit f8e2525

File tree

8 files changed

+73
-18
lines changed

8 files changed

+73
-18
lines changed

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/ExpressionChecker.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,17 @@ else if (context.fieldExists(first.image(), ts)) {
401401
}
402402
}
403403
}
404-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
404+
catch (AmbiguousNameException e) {
405+
setErrorStrings(node, className);
406+
throw new ExecutionError("ambiguous.name", node);
407+
}
405408
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument.arity", node); }
406409
catch (UnmatchedLookupException e) {
407410
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
408-
else { throw new ExecutionError("ambiguous.name", node); }
411+
else {
412+
setErrorStrings(node, className);
413+
throw new ExecutionError("ambiguous.name", node);
414+
}
409415
}
410416

411417
// Append member names until a field is encountered (or until all names are used up)
@@ -427,7 +433,10 @@ else if (ts.containsClass(classType, memberName.image(), context.accessModule())
427433
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument.arity", node); }
428434
catch (UnmatchedLookupException e) {
429435
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
430-
else { throw new ExecutionError("ambiguous.name", node); }
436+
else {
437+
setErrorStrings(node, memberName.image());
438+
throw new ExecutionError("ambiguous.name", node);
439+
}
431440
}
432441
// TODO: Improve error when memberName is a non-static class
433442
}
@@ -530,10 +539,16 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
530539
addRuntimeCheck(node, result, ref.field().type());
531540
return setType(node, result);
532541
}
533-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
542+
catch (AmbiguousNameException e) {
543+
setErrorStrings(node, node.getFieldName());
544+
throw new ExecutionError("ambiguous.name", node);
545+
}
534546
catch (UnmatchedLookupException e) {
535547
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
536-
else { throw new ExecutionError("ambiguous.name", node); }
548+
else {
549+
setErrorStrings(node, node.getFieldName());
550+
throw new ExecutionError("ambiguous.name", node);
551+
}
537552
}
538553
}
539554

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/StatementChecker.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ private Type checkTypeName(TypeName t) {
227227
}
228228
else { return context.importTopLevelClass(c); }
229229
}
230-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
230+
catch (AmbiguousNameException e) {
231+
setErrorStrings(node, node.getName());
232+
throw new ExecutionError("ambiguous.name", node);
233+
}
231234
}
232235

233236
}
@@ -246,7 +249,10 @@ private ClassType resolveClassName(String name, Node node) {
246249
DJClass c = context.getTopLevelClass(topLevelName, ts);
247250
result = (c == null) ? null : ts.makeClassType(c);
248251
}
249-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
252+
catch (AmbiguousNameException e) {
253+
setErrorStrings(node, topLevelName);
254+
throw new ExecutionError("ambiguous.name", node);
255+
}
250256
}
251257
else {
252258
try { result = ts.lookupClass(result, piece, IterUtil.<Type>empty(), context.accessModule()); }

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/TypeNameChecker.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,32 @@ private class TypeNameVisitor extends AbstractVisitor<Type> implements Lambda<Ty
277277
}
278278
}
279279
}
280-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
280+
catch (AmbiguousNameException e) {
281+
setErrorStrings(node, name);
282+
throw new ExecutionError("ambiguous.name", node);
283+
}
281284
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument.arity", node); }
282285
catch (UnmatchedLookupException e) {
283286
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
284-
else { throw new ExecutionError("ambiguous.name", node); }
287+
else {
288+
setErrorStrings(node, name);
289+
throw new ExecutionError("ambiguous.name", node);
290+
}
285291
}
286292
}
287293
while (ids.hasNext()) {
294+
String nextId = ids.next().image();
288295
try {
289-
ClassType memberType = ts.lookupClass(t, ids.next().image(), IterUtil.<Type>empty(), context.accessModule());
296+
ClassType memberType = ts.lookupClass(t, nextId, IterUtil.<Type>empty(), context.accessModule());
290297
t = memberType;
291298
}
292299
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument.arity", node); }
293300
catch (UnmatchedLookupException e) {
294301
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
295-
else { throw new ExecutionError("ambiguous.name", node); }
302+
else {
303+
setErrorStrings(node, nextId);
304+
throw new ExecutionError("ambiguous.name", node);
305+
}
296306
}
297307
}
298308

@@ -336,24 +346,34 @@ private class TypeNameVisitor extends AbstractVisitor<Type> implements Lambda<Ty
336346
t = context.getTypeVariable(name, ts);
337347
}
338348
}
339-
catch (AmbiguousNameException e) { throw new ExecutionError("ambiguous.name", node); }
349+
catch (AmbiguousNameException e) {
350+
setErrorStrings(node, name);
351+
throw new ExecutionError("ambiguous.name", node);
352+
}
340353
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument.arity", node); }
341354
catch (UnmatchedLookupException e) {
342355
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
343-
else { throw new ExecutionError("ambiguous.name", node); }
356+
else {
357+
setErrorStrings(node, name);
358+
throw new ExecutionError("ambiguous.name", node);
359+
}
344360
}
345361
}
346362

347363
while (ids.hasNext()) {
364+
String nextId = ids.next().image();
348365
try {
349366
Iterable<Type> targs = checkStructureForList(allTargs.next());
350-
ClassType memberType = ts.lookupClass(t, ids.next().image(), targs, context.accessModule());
367+
ClassType memberType = ts.lookupClass(t, nextId, targs, context.accessModule());
351368
t = memberType;
352369
}
353370
catch (InvalidTypeArgumentException e) { throw new ExecutionError("type.argument", node); }
354371
catch (UnmatchedLookupException e) {
355372
if (e.matches() == 0) { throw new ExecutionError("undefined.name.noinfo", node); }
356-
else { throw new ExecutionError("ambiguous.name", node); }
373+
else {
374+
setErrorStrings(node, nextId);
375+
throw new ExecutionError("ambiguous.name", node);
376+
}
357377
}
358378
}
359379

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/TreeClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public String declaredName() {
194194

195195
public boolean isStatic() {
196196
if (_declaring == null) { return false; }
197-
else { return _declaring.isInterface() || _ast instanceof EnumDeclaration || _mods.isStatic(); }
197+
else { return _declaring.isInterface() || isInterface() || _ast instanceof EnumDeclaration || _mods.isStatic(); }
198198
}
199199
public boolean isAbstract() { return _mods.isAbstract(); }
200200
public boolean isFinal() { return _mods.isFinal(); }

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/TypeSystem.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import edu.rice.cs.plt.iter.IterUtil;
99
import edu.rice.cs.plt.lambda.Thunk;
1010
import edu.rice.cs.plt.lambda.Lambda;
11+
import edu.rice.cs.plt.object.ObjectUtil;
1112

1213
import java.io.Serializable;
1314

@@ -560,6 +561,18 @@ protected FieldReference(DJField field, Type type) {
560561

561562
/** @return The return type of the access (before capture) */
562563
public Type type() { return _type; }
564+
565+
public boolean equals(Object that) {
566+
if (this == that) { return true; }
567+
else if (!(that instanceof FieldReference)) { return false; }
568+
else {
569+
FieldReference r = (FieldReference) that;
570+
return _field.equals(r._field) && _type.equals(r._type);
571+
}
572+
}
573+
574+
public int hashCode() { return ObjectUtil.hash(FieldReference.class, _field, _type); }
575+
563576
}
564577

565578

dynamicjava/src/koala/dynamicjava/interpreter/error/ExecutionError.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class ExecutionError extends Error {
8383
public ExecutionError(String s, Node n) {
8484
rawMessage = s;
8585
node = n;
86+
getMessage(); // ensure that s is valid while we have a useful stack trace
8687
}
8788

8889
public ExecutionError(Throwable thrown) {

dynamicjava/src/koala/dynamicjava/interpreter/resources/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ No constructor in %0 matches this invocation\n\
144144
\ Expected return type: %4\n\
145145
\ Candidate signatures: %5
146146

147-
ambiguous.name = Name is ambiguous
147+
ambiguous.name = The name '%0' is ambiguous
148148
ambiguous.inner.class = Multiple classes in %0 have name '%1'
149149
ambiguous.field = Multiple fields in %0 have name '%1'
150150

dynamicjava/src/koala/dynamicjava/util/LocalizedMessageReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public String getMessage(String key, String[] strings) {
8585
numb += c;
8686
} while (++i < rawMessage.length());
8787
int n = Integer.parseInt(numb);
88-
result += (n < strings.length) ? strings[n] : "%"+n;
88+
result += strings[n];
8989
}
9090
} else {
9191
result += c;

0 commit comments

Comments
 (0)