diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java index db675e3d0b3..d799975a76a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java @@ -389,6 +389,10 @@ public class JavaTokenizer extends UnicodeReader { break; } } else { + if (!isString && !Character.isBmpCodePoint(getCodepoint())) { + lexError(pos, Errors.IllegalCharLiteralMultipleSurrogates); + } + putThenNext(); } } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties index 79d15a96a6e..46d0438f27d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties @@ -694,6 +694,9 @@ compiler.err.illegal.combination.of.modifiers=\ compiler.err.illegal.enum.static.ref=\ illegal reference to static field from initializer +compiler.err.illegal.char.literal.multiple.surrogates=\ + character literal contains more than one UTF-16 code unit + compiler.err.illegal.esc.char=\ illegal escape character diff --git a/test/langtools/tools/javac/diags/examples/IllegalCharLiteralMultipleSurrogates.java b/test/langtools/tools/javac/diags/examples/IllegalCharLiteralMultipleSurrogates.java new file mode 100644 index 00000000000..5c7f7c41475 --- /dev/null +++ b/test/langtools/tools/javac/diags/examples/IllegalCharLiteralMultipleSurrogates.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// key: compiler.err.illegal.char.literal.multiple.surrogates + +class IllegalCharLiteralMultipleSurrogates { + char c = '\uD83D\uDE0A'; +} diff --git a/test/langtools/tools/javac/lexer/JavaLexerTest.java b/test/langtools/tools/javac/lexer/JavaLexerTest.java index 6b9c789087c..b2362801f84 100644 --- a/test/langtools/tools/javac/lexer/JavaLexerTest.java +++ b/test/langtools/tools/javac/lexer/JavaLexerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,8 +30,11 @@ */ import java.net.URI; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; +import javax.tools.DiagnosticListener; import javax.tools.SimpleJavaFileObject; import com.sun.tools.javac.parser.JavaTokenizer; @@ -101,17 +104,29 @@ public class JavaLexerTest { new TestTuple(ERROR, "\'\'"), new TestTuple(ERROR, "\'\\q\'", "\'\\q\'"), new TestTuple(ERROR, "\'\\{1+2}\'", "\'\\{1+2}\'"), + new TestTuple(ERROR, "'\uD83D\uDE0A'", + List.of("compiler.err.illegal.char.literal.multiple.surrogates")), }; static class TestTuple { String input; TokenKind kind; String expected; + List expectedErrors; - TestTuple(TokenKind kind, String input, String expected) { + TestTuple(TokenKind kind, String input, String expected, List expectedErrors) { this.input = input; this.kind = kind; this.expected = expected; + this.expectedErrors = expectedErrors; + } + + TestTuple(TokenKind kind, String input, List expectedErrors) { + this(kind, input, input, expectedErrors); + } + + TestTuple(TokenKind kind, String input, String expected) { + this(kind, input, expected, null); } TestTuple(TokenKind kind, String input) { @@ -121,6 +136,12 @@ public class JavaLexerTest { void test(TestTuple test, boolean willFail) throws Exception { Context ctx = new Context(); + List errors = new ArrayList(); + + if (test.expectedErrors != null) { + ctx.put(DiagnosticListener.class, (DiagnosticListener) d -> errors.add(d.getCode())); + } + Log log = Log.instance(ctx); log.useSource(SimpleJavaFileObject.forSource(URI.create("mem://Test.java"), @@ -149,6 +170,10 @@ public class JavaLexerTest { System.err.println("input: " + test.input); throw new AssertionError("Unexpected token content: " + actual); } + + if (test.expectedErrors != null && !test.expectedErrors.equals(errors)) { + throw new AssertionError("Unexpected errors: " + errors); + } } void run() throws Exception {