|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 |
|
24 | 24 | /* |
25 | 25 | * @test |
26 | | - * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 |
| 26 | + * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 |
27 | 27 | * @summary tests error and diagnostics positions |
28 | 28 | * @author Jan Lahoda |
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api |
@@ -2061,6 +2061,183 @@ public Void visitModule(ModuleTree node, Void p) { |
2061 | 2061 | }); |
2062 | 2062 | } |
2063 | 2063 |
|
| 2064 | + @Test //JDK-8304671 |
| 2065 | + void testEnumConstantUnderscore() throws IOException { |
| 2066 | + record TestCase(String code, String release, String ast, String errors) {} |
| 2067 | + TestCase[] testCases = new TestCase[] { |
| 2068 | + new TestCase(""" |
| 2069 | + package t; |
| 2070 | + enum Test { |
| 2071 | + _ |
| 2072 | + } |
| 2073 | + """, |
| 2074 | + "8", |
| 2075 | + """ |
| 2076 | + package t; |
| 2077 | + \n\ |
| 2078 | + enum Test { |
| 2079 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2080 | + } """, |
| 2081 | + """ |
| 2082 | + - compiler.warn.option.obsolete.source: 8 |
| 2083 | + - compiler.warn.option.obsolete.target: 8 |
| 2084 | + - compiler.warn.option.obsolete.suppression |
| 2085 | + Test.java:3:5: compiler.warn.underscore.as.identifier |
| 2086 | + """), |
| 2087 | + new TestCase(""" |
| 2088 | + package t; |
| 2089 | + enum Test { |
| 2090 | + _ |
| 2091 | + } |
| 2092 | + """, |
| 2093 | + System.getProperty("java.specification.version"), |
| 2094 | + """ |
| 2095 | + package t; |
| 2096 | + \n\ |
| 2097 | + enum Test { |
| 2098 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2099 | + } """, |
| 2100 | + """ |
| 2101 | + Test.java:3:5: compiler.err.underscore.as.identifier |
| 2102 | + """), |
| 2103 | + new TestCase(""" |
| 2104 | + package t; |
| 2105 | + enum Test { |
| 2106 | + _; |
| 2107 | + } |
| 2108 | + """, |
| 2109 | + "8", |
| 2110 | + """ |
| 2111 | + package t; |
| 2112 | + \n\ |
| 2113 | + enum Test { |
| 2114 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2115 | + } """, |
| 2116 | + """ |
| 2117 | + - compiler.warn.option.obsolete.source: 8 |
| 2118 | + - compiler.warn.option.obsolete.target: 8 |
| 2119 | + - compiler.warn.option.obsolete.suppression |
| 2120 | + Test.java:3:5: compiler.warn.underscore.as.identifier |
| 2121 | + """), |
| 2122 | + new TestCase(""" |
| 2123 | + package t; |
| 2124 | + enum Test { |
| 2125 | + _; |
| 2126 | + } |
| 2127 | + """, |
| 2128 | + System.getProperty("java.specification.version"), |
| 2129 | + """ |
| 2130 | + package t; |
| 2131 | + \n\ |
| 2132 | + enum Test { |
| 2133 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2134 | + } """, |
| 2135 | + """ |
| 2136 | + Test.java:3:5: compiler.err.underscore.as.identifier |
| 2137 | + """), |
| 2138 | + new TestCase(""" |
| 2139 | + package t; |
| 2140 | + enum Test { |
| 2141 | + A; |
| 2142 | + void t() {} |
| 2143 | + _; |
| 2144 | + } |
| 2145 | + """, |
| 2146 | + "8", |
| 2147 | + """ |
| 2148 | + package t; |
| 2149 | + \n\ |
| 2150 | + enum Test { |
| 2151 | + /*public static final*/ A /* = new Test() */ /*enum*/ , |
| 2152 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2153 | + \n\ |
| 2154 | + void t() { |
| 2155 | + } |
| 2156 | + } """, |
| 2157 | + """ |
| 2158 | + - compiler.warn.option.obsolete.source: 8 |
| 2159 | + - compiler.warn.option.obsolete.target: 8 |
| 2160 | + - compiler.warn.option.obsolete.suppression |
| 2161 | + Test.java:5:5: compiler.err.enum.constant.not.expected |
| 2162 | + Test.java:5:5: compiler.warn.underscore.as.identifier |
| 2163 | + """), |
| 2164 | + new TestCase(""" |
| 2165 | + package t; |
| 2166 | + enum Test { |
| 2167 | + A; |
| 2168 | + void t() {} |
| 2169 | + _; |
| 2170 | + } |
| 2171 | + """, |
| 2172 | + System.getProperty("java.specification.version"), |
| 2173 | + """ |
| 2174 | + package t; |
| 2175 | + \n\ |
| 2176 | + enum Test { |
| 2177 | + /*public static final*/ A /* = new Test() */ /*enum*/ , |
| 2178 | + /*public static final*/ _ /* = new Test() */ /*enum*/ ; |
| 2179 | + \n\ |
| 2180 | + void t() { |
| 2181 | + } |
| 2182 | + } """, |
| 2183 | + """ |
| 2184 | + Test.java:5:5: compiler.err.enum.constant.not.expected |
| 2185 | + """), |
| 2186 | + new TestCase(""" |
| 2187 | + package t; |
| 2188 | + enum Test { |
| 2189 | + _ {}, |
| 2190 | + A; |
| 2191 | + } |
| 2192 | + """, |
| 2193 | + "8", |
| 2194 | + """ |
| 2195 | + package t; |
| 2196 | + \n\ |
| 2197 | + enum Test { |
| 2198 | + /*public static final*/ _ /* = new Test() */ /*enum*/ { |
| 2199 | + }, |
| 2200 | + /*public static final*/ A /* = new Test() */ /*enum*/ ; |
| 2201 | + } """, |
| 2202 | + """ |
| 2203 | + - compiler.warn.option.obsolete.source: 8 |
| 2204 | + - compiler.warn.option.obsolete.target: 8 |
| 2205 | + - compiler.warn.option.obsolete.suppression |
| 2206 | + Test.java:3:5: compiler.warn.underscore.as.identifier |
| 2207 | + """), |
| 2208 | + new TestCase(""" |
| 2209 | + package t; |
| 2210 | + enum Test { |
| 2211 | + _ {}, |
| 2212 | + A; |
| 2213 | + } |
| 2214 | + """, |
| 2215 | + System.getProperty("java.specification.version"), |
| 2216 | + """ |
| 2217 | + package t; |
| 2218 | + \n\ |
| 2219 | + enum Test { |
| 2220 | + /*public static final*/ _ /* = new Test() */ /*enum*/ { |
| 2221 | + }, |
| 2222 | + /*public static final*/ A /* = new Test() */ /*enum*/ ; |
| 2223 | + } """, |
| 2224 | + """ |
| 2225 | + Test.java:3:5: compiler.err.underscore.as.identifier |
| 2226 | + """), |
| 2227 | + }; |
| 2228 | + for (TestCase testCase : testCases) { |
| 2229 | + StringWriter out = new StringWriter(); |
| 2230 | + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null, |
| 2231 | + List.of("-XDrawDiagnostics", "--release", testCase.release), |
| 2232 | + null, Arrays.asList(new MyFileObject(testCase.code))); |
| 2233 | + String ast = ct.parse().iterator().next().toString().replaceAll("\\R", "\n"); |
| 2234 | + assertEquals("Unexpected AST, got:\n" + ast, testCase.ast, ast); |
| 2235 | + assertEquals("Unexpected errors, got:\n" + out.toString(), |
| 2236 | + out.toString().replaceAll("\\R", "\n"), |
| 2237 | + testCase.errors); |
| 2238 | + } |
| 2239 | + } |
| 2240 | + |
2064 | 2241 | void run(String[] args) throws Exception { |
2065 | 2242 | int passed = 0, failed = 0; |
2066 | 2243 | final Pattern p = (args != null && args.length > 0) |
|
0 commit comments