Report malformed URIs in library dependencies as errors.

For instance: "import '../../Udyn[mic ils/expect.dart';" used to cause an internal compiler error. The error is now caught and the compiler reports the invalid import.

BUG=http://dartbug.com/23228
R=floitsch@google.com

Review URL: https://codereview.chromium.org//1131593006

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@45580 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
het@google.com 2015-05-07 00:11:47 +00:00
parent 1124eaacd8
commit acfd54f53a
4 changed files with 61 additions and 8 deletions

View file

@ -418,7 +418,7 @@ abstract class Backend {
/// have special treatment, such as being allowed to extends blacklisted
/// classes or member being eagerly resolved.
bool isBackendLibrary(LibraryElement library) {
// TODO(johnnwinther): Remove this when patching is only done by the
// TODO(johnniwinther): Remove this when patching is only done by the
// JavaScript backend.
Uri canonicalUri = library.canonicalUri;
if (canonicalUri == js_backend.JavaScriptBackend.DART_JS_HELPER ||
@ -654,7 +654,7 @@ abstract class Compiler implements DiagnosticListener {
final CacheStrategy cacheStrategy;
/**
* Map from token to the first preceeding comment token.
* Map from token to the first preceding comment token.
*/
final TokenMap commentMap = new TokenMap();
@ -1053,7 +1053,7 @@ abstract class Compiler implements DiagnosticListener {
progress = new Stopwatch()..start();
}
// TODO(johnniwinther): Separate the dependency tracking from the enqueueing
// TODO(johnniwinther): Separate the dependency tracking from the enqueuing
// for global dependencies.
globalDependencies =
new CodegenRegistry(this, new TreeElementMapping(null));

View file

@ -499,13 +499,23 @@ class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask {
/**
* Handle an import/export tag by loading the referenced library and
* registering its dependency in [handler] for the computation of the import/
* export scope.
* export scope. If the tag does not contain a valid URI, then its dependency
* is not registered in [handler].
*/
Future registerLibraryFromTag(LibraryDependencyHandler handler,
LibraryElement library,
LibraryDependency tag) {
Future<Null> registerLibraryFromTag(LibraryDependencyHandler handler,
LibraryElement library,
LibraryDependency tag) {
Uri base = library.canonicalUri;
Uri resolvedUri = base.resolve(tag.uri.dartString.slowToString());
String tagUriString = tag.uri.dartString.slowToString();
Uri resolvedUri;
try {
resolvedUri = base.resolve(tagUriString);
} on FormatException {
compiler.reportError(
tag.uri, MessageKind.INVALID_URI, {'uri': tagUriString});
// 'reportError' does not stop necessarily stop compilation
return new Future.value();
}
return createLibrary(handler, library, resolvedUri, tag.uri)
.then((LibraryElement loadedLibrary) {
if (loadedLibrary == null) return;

View file

@ -1557,6 +1557,17 @@ main() {}
@MirrorsUsed(targets: 'dart.core.List.addAl')
import 'dart:mirrors';
main() {}
"""]);
static const MessageKind INVALID_URI = const MessageKind(
"'#{uri}' is not a valid URI.",
howToFix: DONT_KNOW_HOW_TO_FIX,
examples: const [
"""
// can't have a '[' in a URI
import '../../Udyn[mic ils/expect.dart';
main() {}
"""]);

View file

@ -0,0 +1,32 @@
// 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 the compiler can handle missing files used in imports, exports,
// part tags or as the main source file.
library dart2js.test.malformed_uri;
import 'package:expect/expect.dart';
import "package:async_helper/async_helper.dart";
import 'memory_compiler.dart';
const MEMORY_SOURCE_FILES = const {
'main.dart': '''
import '../../Udyn[mic ils/expect.dart';
main () { print("Hi"); }
''',
};
testMalformedUri() {
var collector = new DiagnosticCollector();
var compiler = compilerFor(MEMORY_SOURCE_FILES, diagnosticHandler: collector);
asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) {
Expect.equals(1, collector.errors.length);
}));
}
void main() {
testMalformedUri();
}