Skip to content

Commit

Permalink
GROOVY-6072: "String | []" is converted to a ClassExpression at seman…
Browse files Browse the repository at this point in the history
…tic analysis
  • Loading branch information
melix committed Apr 3, 2013
1 parent 39e06bf commit ad4434a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/org/codehaus/groovy/control/ResolveVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,13 @@ private boolean testVanillaNameForClass(String name) {
return !Character.isLowerCase(name.charAt(0));
}

private boolean isLeftSquareBracket(int op) {
return op == Types.ARRAY_EXPRESSION
|| op == Types.LEFT_SQUARE_BRACKET
|| op == Types.SYNTH_LIST
|| op == Types.SYNTH_MAP;
}

protected Expression transformBinaryExpression(BinaryExpression be) {
Expression left = transform(be.getLeftExpression());
int type = be.getOperation().getType();
Expand All @@ -885,7 +892,7 @@ protected Expression transformBinaryExpression(BinaryExpression be) {
addError(error, be.getLeftExpression());
return be;
}
if (left instanceof ClassExpression) {
if (left instanceof ClassExpression && isLeftSquareBracket(type)) {
if (be.getRightExpression() instanceof ListExpression) {
ListExpression list = (ListExpression) be.getRightExpression();
if (list.getExpressions().isEmpty()) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/groovy/bugs/Groovy6072Bug.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package groovy.bugs

class Groovy6072Bug extends GroovyTestCase {
void testShouldNotChangeBinExpToClassExp() {
assertScript '''import groovy.transform.ASTTest
import org.codehaus.groovy.ast.expr.BinaryExpression
class OhNo {}
try {
@ASTTest(phase=CANONICALIZATION, value={
def right = node.rightExpression
assert right instanceof BinaryExpression
})
def expr1 = OhNo | []
@ASTTest(phase=SEMANTIC_ANALYSIS, value={
def right = node.rightExpression
assert right instanceof BinaryExpression
})
def expr2 = OhNo | []
} catch (MissingMethodException ex) {
assert ex.message.contains('or()')
// alright, what we wanted to test has gone
}
'''
}
}

0 comments on commit ad4434a

Please sign in to comment.