diff options
author | Manoj Palat | 2019-07-15 09:53:21 +0000 |
---|---|---|
committer | Manoj Palat | 2019-07-15 09:53:21 +0000 |
commit | dca146a6ff9a1138801577c665e5e29b71cbf90e (patch) | |
tree | 1123c0e07d6a3e577ee795381c3bc69bbd03987e | |
parent | 42b2896c6fdc8bfbaa0fb529ac8d63ad6238eebd (diff) | |
download | eclipse.jdt.core-dca146a6ff9a1138801577c665e5e29b71cbf90e.tar.gz eclipse.jdt.core-dca146a6ff9a1138801577c665e5e29b71cbf90e.tar.xz eclipse.jdt.core-dca146a6ff9a1138801577c665e5e29b71cbf90e.zip |
and yield type - yield yield processing
Change-Id: Ie5df9fb9d6e71a120cf9beffbf0121f762f25830
2 files changed, 67 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java index 90818e436d..17bff8ecd2 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java @@ -3047,4 +3047,57 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest { new String[] {""}, options); } + public void testBug547891_15() { + if (this.complianceLevel < ClassFileConstants.JDK12) + return; + Map<String, String> options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + options.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.WARNING); + String message = + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " case 1 -> yield();\n" + + " ^^^^^^^\n" + + "restricted identifier yield not allowed here - method calls need to be qualified\n" + + "----------\n" + + "2. ERROR in X.java (at line 8)\n" + + " case 3 -> {yield yield();}\n" + + " ^^^^^^^\n" + + "restricted identifier yield not allowed here - method calls need to be qualified\n" + + "----------\n" + + "3. ERROR in X.java (at line 10)\n" + + " default -> { yield yield();}\n" + + " ^^^^^^^\n" + + "restricted identifier yield not allowed here - method calls need to be qualified\n" + + "----------\n"; + + this.runNegativeTest(new String[] { + "X.java", + "public class X {\n"+ + "\n"+ + " @SuppressWarnings(\"preview\")\n"+ + " public static int foo(int i) {\n"+ + " int r = switch(i) {\n"+ + " case 1 -> yield();\n"+ + " case 2 -> X.yield();\n"+ + " case 3 -> {yield yield();}\n"+ + " case 4 -> {yield X.yield();}\n"+ + " default -> { yield yield();}\n"+ + " };\n"+ + " return r;\n"+ + " }\n"+ + " public static int yield() {\n"+ + " return 0;\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " System.out.println(X.foo(0));\n"+ + " }\n"+ + "}\n" + }, + message, + null, + true, + new String[] { "--enable-preview"}, + options); + } }
\ No newline at end of file diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java index 19c177b018..3645096bc0 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java @@ -125,6 +125,10 @@ public class Scanner implements TerminalTokens { protected ScanContext scanContext = null; protected boolean insideModuleInfo = false; + enum ScanYieldContext { + EXPECTING_YIELD, FLAGGED_YIELD, INACTIVE + } + protected ScanYieldContext scanYieldContext = null; public static final String END_OF_SOURCE = "End_Of_Source"; //$NON-NLS-1$ public static final String INVALID_HEXA = "Invalid_Hexa_Literal"; //$NON-NLS-1$ @@ -2743,8 +2747,7 @@ public boolean isInModuleDeclaration() { (this.activeParser != null ? this.activeParser.isParsingModuleDeclaration() : false); } protected boolean areRestrictedModuleKeywordsActive() { - return this.scanContext != null && this.scanContext != ScanContext.INACTIVE && - this.scanContext != ScanContext.EXPECTING_YIELD; + return this.scanContext != null && this.scanContext != ScanContext.INACTIVE; } void updateScanContext(int token) { switch (token) { @@ -5007,20 +5010,27 @@ private boolean mayBeAtAnYieldStatement() { } } int disambiguatedRestrictedIdentifier(int restrictedKeywordToken) { + if (restrictedKeywordToken != TokenNameRestrictedIdentifierYield) + return restrictedKeywordToken; // safety net - only yield processed here! if (!this.previewEnabled) return TokenNameIdentifier; - if (this.scanContext == ScanContext.EXPECTING_YIELD) + if (this.scanYieldContext == ScanYieldContext.FLAGGED_YIELD) // nested yield implies identifier. + return TokenNameIdentifier; + if (this.scanYieldContext == ScanYieldContext.EXPECTING_YIELD) { + this.scanYieldContext = ScanYieldContext.FLAGGED_YIELD; return TokenNameRestrictedIdentifierYield; + } if (this.sourceLevel < ClassFileConstants.JDK13) return TokenNameIdentifier; int token = TokenNameIdentifier; + // not working - check intermittent parser rule definition possibility. final VanguardParser parser = getVanguardParser(); if (restrictedKeywordToken == TokenNameRestrictedIdentifierYield && mayBeAtAnYieldStatement()) { parser.scanner.resetTo(this.startPosition, this.eofPosition - 1); - parser.scanner.scanContext = ScanContext.EXPECTING_YIELD; + parser.scanner.scanYieldContext = ScanYieldContext.EXPECTING_YIELD; if (parser.parse(Goal.YieldStatementGoal) == VanguardParser.SUCCESS) { token = TokenNameRestrictedIdentifierYield; } |