You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2019/11/29 21:40:00 UTC
[jira] [Comment Edited] (GROOVY-8996) String literal unexpectedly
resolving to a local variable
[ https://issues.apache.org/jira/browse/GROOVY-8996?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16985195#comment-16985195 ]
Eric Milles edited comment on GROOVY-8996 at 11/29/19 9:39 PM:
---------------------------------------------------------------
The variable expressions "toUpperCase" and "'toUpperCase'" are resolved within {{VariableScopeVisitor}}:
{code:java}
public void visitMethodCallExpression(MethodCallExpression call) {
if (call.isImplicitThis() && call.getMethod() instanceof ConstantExpression) {
...
Variable v = checkVariableNameForDeclaration(methodName, call); // local variable found here
if (v != null && !(v instanceof DynamicVariable)) {
checkVariableContextAccess(v, call);
}
if (v instanceof VariableExpression || v instanceof Parameter) { // constant expression transformed to variable expression here
VariableExpression object = new VariableExpression(v);
object.setSourcePosition(methodNameConstant);
call.setObjectExpression(object);
ConstantExpression method = new ConstantExpression("call");
method.setSourcePosition(methodNameConstant); // important for GROOVY-4344
call.setImplicitThis(false);
call.setMethod(method);
}
private Variable checkVariableNameForDeclaration(String name, Expression expression) {
if ("super".equals(name) || "this".equals(name)) return null;
VariableScope scope = currentScope;
Variable var = new DynamicVariable(name, currentScope.isInStaticContext());
Variable orig = var;
// try to find a declaration of a variable
boolean crossingStaticContext = false;
while (true) {
crossingStaticContext = crossingStaticContext || scope.isInStaticContext();
Variable var1 = scope.getDeclaredVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedLocalVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedClassVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
ClassNode classScope = scope.getClassScope();
if (classScope != null) {
Variable member = findClassMember(classScope, var.getName());
if (member != null) {
boolean staticScope = crossingStaticContext || isSpecialConstructorCall;
boolean staticMember = member.isInStaticContext();
// We don't allow a static context (e.g. a static method) to access
// a non-static variable (e.g. a non-static field).
if (!(staticScope && !staticMember))
var = member;
}
// GROOVY-5961
if (!isAnonymous(classScope))
break;
}
scope = scope.getParent();
}
if (var == orig && crossingStaticContext) {
var = new DynamicVariable(var.getName(), true);
}
VariableScope end = scope;
scope = currentScope;
while (scope != end) {
if (end.isClassScope() ||
(end.isReferencedClassVariable(name) && end.getDeclaredVariable(name) == null)) {
scope.putReferencedClassVariable(var);
} else {
scope.putReferencedLocalVariable(var);
}
scope = scope.getParent();
}
return var;
}
{code}
was (Author: emilles):
The variable expressions "toUpperCase" and "'toUpperCase'" are resolved within {{VariableScopeVisitor}}:
{code:java}
public void visitMethodCallExpression(MethodCallExpression call) {
if (call.isImplicitThis() && call.getMethod() instanceof ConstantExpression) {
ConstantExpression methodNameConstant = (ConstantExpression) call.getMethod();
Object value = methodNameConstant.getText();
if (!(value instanceof String)) {
throw new GroovyBugError("tried to make a method call with a non-String constant method name.");
}
String methodName = (String) value;
Variable v = checkVariableNameForDeclaration(methodName, call);
if (v != null && !(v instanceof DynamicVariable)) {
checkVariableContextAccess(v, call);
}
private Variable checkVariableNameForDeclaration(String name, Expression expression) {
if ("super".equals(name) || "this".equals(name)) return null;
VariableScope scope = currentScope;
Variable var = new DynamicVariable(name, currentScope.isInStaticContext());
Variable orig = var;
// try to find a declaration of a variable
boolean crossingStaticContext = false;
while (true) {
crossingStaticContext = crossingStaticContext || scope.isInStaticContext();
Variable var1 = scope.getDeclaredVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedLocalVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedClassVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
ClassNode classScope = scope.getClassScope();
if (classScope != null) {
Variable member = findClassMember(classScope, var.getName());
if (member != null) {
boolean staticScope = crossingStaticContext || isSpecialConstructorCall;
boolean staticMember = member.isInStaticContext();
// We don't allow a static context (e.g. a static method) to access
// a non-static variable (e.g. a non-static field).
if (!(staticScope && !staticMember))
var = member;
}
// GROOVY-5961
if (!isAnonymous(classScope))
break;
}
scope = scope.getParent();
}
if (var == orig && crossingStaticContext) {
var = new DynamicVariable(var.getName(), true);
}
VariableScope end = scope;
scope = currentScope;
while (scope != end) {
if (end.isClassScope() ||
(end.isReferencedClassVariable(name) && end.getDeclaredVariable(name) == null)) {
scope.putReferencedClassVariable(var);
} else {
scope.putReferencedLocalVariable(var);
}
scope = scope.getParent();
}
return var;
}
{code}
> String literal unexpectedly resolving to a local variable
> ---------------------------------------------------------
>
> Key: GROOVY-8996
> URL: https://issues.apache.org/jira/browse/GROOVY-8996
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 3.0.0-alpha-4
> Reporter: Роман Донченко
> Priority: Major
>
> Consider this code:
> {code:groovy}
> def toUpperCase = { 'ONE' }
> 'two'.with {
> println toUpperCase()
> println 'toUpperCase'()
> println "${'toUpperCase'}"()
> }
> {code}
> It outputs this:
> {code}
> ONE
> ONE
> TWO
> {code}
> In other words, the argument to the second {{println}} is evaluated like the argument to the first one and not like the argument to the third one.
> This seems very surprising and wrong:
> # {{'toUpperCase'}} is equal to {{"${'toUpperCase'}"}}, so how can {{'toUpperCase'()}} and {{"${'toUpperCase'}"()}} be evaluated differently?
> # How can the string literal {{'toUpperCase'}} evaluate to the local variable {{toUpperCase}}? The documentation mentions using string literals for property and method names, but not for local variable names.
> In my opinion, the output should be:
> {code}
> ONE
> TWO
> TWO
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)