More shared messages.

R=brianwilkerson@google.com, sigmund@google.com

Review URL: https://codereview.chromium.org/1706033002 .
This commit is contained in:
Florian Loitsch 2016-02-18 17:21:04 +01:00
parent 0be651ba94
commit 4d26e4ec13
9 changed files with 109 additions and 40 deletions

View file

@ -12,6 +12,8 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/source/error_processor.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart' show ScannerErrorCode;
import 'package:analyzer/src/generated/generated/shared_messages.dart'
as shared_messages;
import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
import 'package:analyzer/src/generated/source.dart';
@ -2228,16 +2230,14 @@ class CompileTimeErrorCode extends ErrorCode {
* <i>return e;</i> appears in a generative constructor.
*/
static const CompileTimeErrorCode RETURN_IN_GENERATIVE_CONSTRUCTOR =
const CompileTimeErrorCode('RETURN_IN_GENERATIVE_CONSTRUCTOR',
"Constructors cannot return a value");
shared_messages.RETURN_IN_GENERATIVE_CONSTRUCTOR;
/**
* 13.12 Return: It is a compile-time error if a return statement of the form
* <i>return e;</i> appears in a generator function.
*/
static const CompileTimeErrorCode RETURN_IN_GENERATOR =
const CompileTimeErrorCode('RETURN_IN_GENERATOR',
"Cannot return a value from a generator function (one marked with either 'async*' or 'sync*')");
shared_messages.RETURN_IN_GENERATOR;
/**
* 14.1 Imports: It is a compile-time error if a prefix used in a deferred

View file

@ -1928,7 +1928,9 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
} else if (_inGenerator) {
// RETURN_IN_GENERATOR
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.RETURN_IN_GENERATOR, statement);
CompileTimeErrorCode.RETURN_IN_GENERATOR,
statement,
[_inAsync ? "async*" : "sync*"]);
}
// RETURN_OF_INVALID_TYPE
return _checkForReturnOfInvalidType(returnExpression, expectedReturnType);

View file

@ -61,3 +61,13 @@ const ParserErrorCode CONSTRUCTOR_WITH_RETURN_TYPE = const ParserErrorCode(
'CONSTRUCTOR_WITH_RETURN_TYPE',
"Constructors can't have a return type",
"Try removing the return type."); // Generated. Don't edit.
const CompileTimeErrorCode RETURN_IN_GENERATIVE_CONSTRUCTOR = const CompileTimeErrorCode(
'RETURN_IN_GENERATIVE_CONSTRUCTOR',
"Constructors can't return values.",
"Try removing the return statement or using a factory constructor."); // Generated. Don't edit.
const CompileTimeErrorCode RETURN_IN_GENERATOR = const CompileTimeErrorCode(
'RETURN_IN_GENERATOR',
"Can't return a value from a generator function (using the '{0}' modifier).",
"Try removing the value, replacing 'return' with 'yield' or changing the method body modifier"); // Generated. Don't edit.

View file

@ -73,4 +73,34 @@ const Map<MessageKind, MessageTemplate> TEMPLATES = const <MessageKind, MessageT
"class A { int A() {} } main() { new A(); }",
]
), // Generated. Don't edit.
MessageKind.RETURN_IN_GENERATIVE_CONSTRUCTOR: const MessageTemplate(
MessageKind.RETURN_IN_GENERATIVE_CONSTRUCTOR,
"Constructors can't return values.",
howToFix: "Try removing the return statement or using a factory constructor.",
examples: const [
r"""
class C {
C() {
return 1;
}
}
main() => new C();""",
]
), // Generated. Don't edit.
MessageKind.RETURN_IN_GENERATOR: const MessageTemplate(
MessageKind.RETURN_IN_GENERATOR,
"Can't return a value from a generator function (using the '#{modifier}' modifier).",
howToFix: "Try removing the value, replacing 'return' with 'yield' or changing the method body modifier",
examples: const [
r"""
foo() async* { return 0; }
main() => foo();
""",
r"""
foo() sync* { return 0; }
main() => foo();
""",
]
), // Generated. Don't edit.
};

View file

@ -136,7 +136,7 @@ enum MessageKind {
CANNOT_RESOLVE_IN_INITIALIZER,
CANNOT_RESOLVE_SETTER,
CANNOT_RESOLVE_TYPE,
CANNOT_RETURN_FROM_CONSTRUCTOR,
RETURN_IN_GENERATIVE_CONSTRUCTOR,
CLASS_NAME_EXPECTED,
COMPILER_CRASHED,
COMPLEX_RETURNING_NSM,
@ -1847,19 +1847,6 @@ main() {
const MessageTemplate(MessageKind.OPERATOR_NAMED_PARAMETERS,
"Operator '#{operatorName}' cannot have named parameters."),
MessageKind.CANNOT_RETURN_FROM_CONSTRUCTOR:
const MessageTemplate(MessageKind.CANNOT_RETURN_FROM_CONSTRUCTOR,
"Constructors can't return values.",
howToFix: "Remove the return statement or use a factory constructor.",
examples: const ["""
class C {
C() {
return 1;
}
}
main() => new C();"""]),
MessageKind.ILLEGAL_FINAL_METHOD_MODIFIER:
const MessageTemplate(MessageKind.ILLEGAL_FINAL_METHOD_MODIFIER,
"Cannot have final modifier on method."),
@ -3314,23 +3301,6 @@ main() sync* {
var yield;
}"""]),
MessageKind.RETURN_IN_GENERATOR:
const MessageTemplate(MessageKind.RETURN_IN_GENERATOR,
"'return' with a value is not allowed in a method body using the "
"'#{modifier}' modifier.",
howToFix: "Try removing the value, replacing 'return' with 'yield' "
"or changing the method body modifier.",
examples: const [
"""
foo() async* { return 0; }
main() => foo();
""",
"""
foo() sync* { return 0; }
main() => foo();
"""]),
MessageKind.NATIVE_NOT_SUPPORTED:
const MessageTemplate(MessageKind.NATIVE_NOT_SUPPORTED,
"'native' modifier is not supported.",

View file

@ -3674,7 +3674,7 @@ class ResolverVisitor extends MappingVisitor<ResolutionResult> {
// Specification 13.12.)
reporter.reportErrorMessage(
expression,
MessageKind.CANNOT_RETURN_FROM_CONSTRUCTOR);
MessageKind.RETURN_IN_GENERATIVE_CONSTRUCTOR);
} else if (!node.isArrowBody && currentAsyncMarker.isYielding) {
reporter.reportErrorMessage(
node,

View file

@ -155,9 +155,12 @@ String convertToAnalyzerTemplate(String template, holeOrder) {
}
}
int seenHoles = 0;
return template.replaceAllMapped(new RegExp(r"#\w+"), (Match match) {
return template.replaceAllMapped(new RegExp(r"#\w+|#{\w+}"), (Match match) {
if (holeMap != null) {
String holeName = match[0].substring(1);
String matchedString = match[0];
String holeName = matchedString.startsWith("#{")
? matchedString.substring(2, matchedString.length - 1)
: matchedString.substring(1);
int index = holeMap[holeName];
if (index == null) {
throw "Couldn't find hole-position for $holeName $holeMap";

File diff suppressed because one or more lines are too long

View file

@ -75,6 +75,8 @@ class Category {
static final parserError = new Category("ParserError");
static final compileTimeError = new Category("CompileTimeError");
final String name;
Category(this.name);
@ -401,4 +403,56 @@ final Map<String, Message> MESSAGES = {
howToFix: "Try removing the return type.",
usedBy: [analyzer, dart2js],
examples: const ["class A { int A() {} } main() { new A(); }",]),
/**
* 13.12 Return: It is a compile-time error if a return statement of the form
* <i>return e;</i> appears in a generative constructor.
*/
'RETURN_IN_GENERATIVE_CONSTRUCTOR': new Message(
id: 'UOTDQH',
category: Category.compileTimeError,
template: "Constructors can't return values.",
howToFix:
"Try removing the return statement or using a factory constructor.",
usedBy: [
analyzer,
dart2js
],
examples: const [
"""
class C {
C() {
return 1;
}
}
main() => new C();"""
]),
/**
* 13.12 Return: It is a compile-time error if a return statement of the form
* <i>return e;</i> appears in a generator function.
*/
'RETURN_IN_GENERATOR': new Message(
id: 'JRUTUQ',
subId: 0,
category: Category.compileTimeError,
template: "Can't return a value from a generator function "
"(using the '#{modifier}' modifier).",
howToFix: "Try removing the value, replacing 'return' with 'yield' or"
" changing the method body modifier",
usedBy: [
analyzer,
dart2js
],
examples: const [
"""
foo() async* { return 0; }
main() => foo();
""",
"""
foo() sync* { return 0; }
main() => foo();
"""
]),
};