dart-lang / sdk

The Dart SDK, including the VM, dart2js, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
9.97k stars 1.53k forks source link

Configurable imports with prefixes doesn't work as expected #55899

Open matanlurey opened 1 month ago

matanlurey commented 1 month ago

I was surprised to see that if I do the following:

import 'impl.dart' as impl if (dart.library.io) 'impl_io.dart';

void main() {
  impl.run();
}

I expected this to, in environments where dart:io is available, call the run function in impl_io.dart.

However, it appears that it unconditionally calls impl.dart due to the prefix, and it's not valid to add the same prefix:

// An import directive can only have one prefix ('as' clause).
// Try removing all but one prefix.
import 'impl.dart' as impl if (dart.library.io) 'impl_io.dart' as impl;
lrhn commented 1 month ago

This should just e a compile-time error. The as clause goes after the if clauses.

If you write it as

import 'impl.dart' if (dart.library.io) 'impl_io.dart' as impl;

it should do what you intend.

Grammatically the 'impl.dart' if (dart.library.io) 'impl_io.dart' is a single production <configurableUri> which is either a URI or a URI followed by one or more conditional URIs.

Filing as parser bug, it shouldn't be allowed to parse with an if ... after the prefix.

userAdityaa commented 1 month ago

Hey, I'm new to contributing to dart. Can you just help with reproducing this error to me ? As per my knowledge @lrhn is right, and this is just an issue that should be solved by the following code written.