Skip to content

Commit 625e5f3

Browse files
committed
[GR-18449] Assigning to a literal should raise error.
1 parent cb2403e commit 625e5f3

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/AssignmentTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,33 @@ public void augassign14() throws Exception {
167167
" cls_or_self, *rest = args");
168168
}
169169

170+
@Test
171+
public void assignToLiteral01() throws Exception {
172+
checkSyntaxErrorMessage("1 = 1", "SyntaxError: can't assign to literal");
173+
}
174+
175+
@Test
176+
public void assignToLiteral02() throws Exception {
177+
checkSyntaxErrorMessage("1.1 = 1", "SyntaxError: can't assign to literal");
178+
}
179+
180+
@Test
181+
public void assignToLiteral03() throws Exception {
182+
checkSyntaxErrorMessage("'' = 1", "SyntaxError: can't assign to literal");
183+
}
184+
185+
@Test
186+
public void assignToLiteral04() throws Exception {
187+
checkSyntaxErrorMessage("f'' = 1", "SyntaxError: can't assign to literal");
188+
}
189+
190+
@Test
191+
public void assignToLiteral05() throws Exception {
192+
checkSyntaxErrorMessage("'' f'' = 1", "SyntaxError: can't assign to literal");
193+
}
194+
195+
@Test
196+
public void assignToKeyword01() throws Exception {
197+
checkSyntaxErrorMessage("True = 1", "SyntaxError: can't assign to keyword");
198+
}
170199
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ParserTestBase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.nio.CharBuffer;
6060
import java.util.Arrays;
6161
import java.util.List;
62+
import org.junit.Assert;
6263
import static org.junit.Assert.assertTrue;
6364
import org.junit.Rule;
6465
import org.junit.rules.TestName;
@@ -146,6 +147,18 @@ public void checkSyntaxError(String source) throws Exception {
146147
assertTrue("Expected SyntaxError was not thrown.", thrown);
147148
}
148149

150+
public void checkSyntaxErrorMessage(String source, String expectedMessage) throws Exception {
151+
boolean thrown = false;
152+
try {
153+
parseNew(source, name.getMethodName(), PythonParser.ParserMode.File);
154+
} catch (PException e) {
155+
thrown = e.isSyntaxError();
156+
Assert.assertEquals(expectedMessage, e.getMessage());
157+
}
158+
159+
assertTrue("Expected SyntaxError was not thrown.", thrown);
160+
}
161+
149162
public void saveNewTreeResult(File testFile, boolean goldenFileNextToTestFile) throws Exception {
150163
assertTrue("The test files " + testFile.getAbsolutePath() + " was not found.", testFile.exists());
151164
TruffleFile src = context.getEnv().getInternalTruffleFile(testFile.getAbsolutePath());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ public SourceSection getSourceSection() {
671671
instance.setAttribute("text", section.isAvailable() ? source.getCharacters(section.getStartLine()) : "");
672672
instance.setAttribute("lineno", section.getStartLine());
673673
instance.setAttribute("offset", section.getStartColumn());
674-
instance.setAttribute("msg", section.getCharIndex() == source.getLength() ? "unexpected EOF while parsing" : "invalid syntax");
674+
instance.setAttribute("msg", section.getCharIndex() == source.getLength() ? "unexpected EOF while parsing" : message != null ? message : "invalid syntax");
675675
throw PException.fromObject(instance, location);
676676
}
677677
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ public PNode visit(AssertSSTNode node) {
218218
public PNode visit(AssignmentSSTNode node) {
219219
ExpressionNode[] lhs = new ExpressionNode[node.lhs.length];
220220
for (int i = 0; i < node.lhs.length; i++) {
221-
lhs[i] = (ExpressionNode) node.lhs[i].accept(this);
221+
SSTNode sstLhs = node.lhs[i];
222+
if (sstLhs instanceof StringLiteralSSTNode || sstLhs instanceof NumberLiteralSSTNode || sstLhs instanceof FloatLiteralSSTNode) {
223+
errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "can't assign to literal");
224+
} else if (sstLhs instanceof BooleanLiteralSSTNode) {
225+
errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "can't assign to keyword");
226+
}
227+
lhs[i] = (ExpressionNode) sstLhs.accept(this);
222228
}
223229
ExpressionNode rhs = (ExpressionNode) node.rhs.accept(this);
224230

0 commit comments

Comments
 (0)