From 6bb8cd8943850ffc8ea17299d210baee4ed0db49 Mon Sep 17 00:00:00 2001 From: Johnni Winther Date: Tue, 15 Dec 2015 10:17:00 +0100 Subject: [PATCH] Introduce benign errors. Closes #25204 R=sigmund@google.com Review URL: https://codereview.chromium.org/1525593002. --- pkg/compiler/lib/src/compiler.dart | 30 +++++++++++++- tests/compiler/dart2js/benign_error_test.dart | 40 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tests/compiler/dart2js/benign_error_test.dart diff --git a/pkg/compiler/lib/src/compiler.dart b/pkg/compiler/lib/src/compiler.dart index 3b98b92e050..4a43f6b59fe 100644 --- a/pkg/compiler/lib/src/compiler.dart +++ b/pkg/compiler/lib/src/compiler.dart @@ -1250,6 +1250,32 @@ abstract class Compiler { _reporter.onCrashInUserCode(message, exception, stackTrace); } + /// Messages for which compile-time errors are reported but compilation + /// continues regardless. + static const List BENIGN_ERRORS = const [ + MessageKind.INVALID_METADATA, + MessageKind.INVALID_METADATA_GENERIC, + ]; + + bool markCompilationAsFailed(DiagnosticMessage message, api.Diagnostic kind) { + if (testMode) { + // When in test mode, i.e. on the build-bot, we always stop compilation. + return true; + } + if (reporter.options.fatalWarnings) { + return true; + } + return !BENIGN_ERRORS.contains(message.message.kind); + } + + void fatalDiagnosticReported(DiagnosticMessage message, + List infos, + api.Diagnostic kind) { + if (markCompilationAsFailed(message, kind)) { + compilationFailed = true; + } + } + /** * Translates the [resolvedUri] into a readable URI. * @@ -1701,13 +1727,13 @@ class _CompilerDiagnosticReporter extends DiagnosticReporter { void reportDiagnostic(DiagnosticMessage message, List infos, api.Diagnostic kind) { + compiler.reportDiagnostic(message, infos, kind); if (kind == api.Diagnostic.ERROR || kind == api.Diagnostic.CRASH || (options.fatalWarnings && kind == api.Diagnostic.WARNING)) { - compiler.compilationFailed = true; + compiler.fatalDiagnosticReported(message, infos, kind); } - compiler.reportDiagnostic(message, infos, kind); } /** diff --git a/tests/compiler/dart2js/benign_error_test.dart b/tests/compiler/dart2js/benign_error_test.dart new file mode 100644 index 00000000000..28c84dd658c --- /dev/null +++ b/tests/compiler/dart2js/benign_error_test.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test that benign error do not prevent compilation. + +import 'memory_compiler.dart'; + +import 'package:async_helper/async_helper.dart'; +import 'package:compiler/src/compiler.dart'; +import 'package:compiler/src/diagnostics/messages.dart'; +import 'package:compiler/src/js_backend/js_backend.dart'; +import 'package:expect/expect.dart'; + +main() { + asyncTest(() async { + for (MessageKind kind in Compiler.BENIGN_ERRORS) { + await testExamples(kind); + } + }); +} + +testExamples(MessageKind kind) async { + MessageTemplate template = MessageTemplate.TEMPLATES[kind]; + for (var example in template.examples) { + if (example is! Map) { + example = {'main.dart': example}; + } + DiagnosticCollector collector = new DiagnosticCollector(); + CompilationResult result = await runCompiler( + memorySourceFiles: example, + diagnosticHandler: collector); + Expect.isTrue(result.isSuccess); + Expect.isTrue( + collector.errors.any((message) => message.messageKind == kind)); + Compiler compiler = result.compiler; + JavaScriptBackend backend = compiler.backend; + Expect.isNotNull(backend.generatedCode[compiler.mainFunction]); + } +} \ No newline at end of file