Comments (3)
.. sounds like the current bug according to the spec is in the CFE not the Analyzer, but we may change the spec to agree with the CFE?
The specification does not specify that it is allowed to invoke an extension method whose on
type is nullable on a potentially nullable receiver, and this means that we need to adjust the specification in any case. This behavior has been implemented for several years, and it would just be a matter of removing this unwanted and unimplemented error from the specification, such that it is aligned with the implementations.
extension on int? {
void foo() => print('Fooing!');
}
void main() {
int? iq;
iq.foo(); // OK!
}
Similarly, we just need to say in the specification that it is not an error to invoke an extension type member m
on a receiver whose type is said extension type, even in the case where it is potentially nullable.
extension type E(int i) {
void foo() => print('Fooing!');
}
void main() {
E(1).foo(); // OK.
}
Again, this is already the implemented behavior. However, the difference comes up when we consider related types, in particular when the receiver type is a type variable whose bound is such an extension type:
extension type E(int i) {
void foo() => print('Fooing!');
}
void f<X extends E>(X x, X? xq) {
x.foo(); // Analyzer: Error.
if (xq == null) return;
xq.foo(); // Analyzer: Error.
}
void main() {
E(1).foo(); // OK.
f(E(2), E(3));
}
In this case it is the implemented behavior of the CFE, but it is not the implemented behavior of the analyzer.
So I'd recommend that we discuss the changes to the specification in dart-lang/language#3549 (hopefully we can agree on allowing all of the above examples), and then adjust implementations in those (small) ways that are needed.
So perhaps this issue should be blocked on dart-lang/language#3549?
from sdk.
E
is a potentially nullable type (extension types do not have Object
as a superinterface unless they are specified to have it). This will suffice to establish a situation where the invocation is accepted (we've eliminated the tricky part):
extension type E(C c) implements Object {
void e() {}
}
So the question is whether it is correct for the static analysis to report the unconditional invocation of e
on a receiver of type T
when E
doesn't have implements Object
. I created dart-lang/language#3549 in order to have this discussion.
The rule in the null-safety feature specification says that it is an error to have an unconditional member access (invocation or tear-off) on a receiver whose static type is potentially nullable and not dynamic
. I argue that we must change this: (1) it should be OK to access an extension member of an extension whose on
type is nullable (and this is indeed allowed by our tool chain), and (2) it should be allowed to access extension type members when the receiver static type is or is bounded by said extension type even when that extension type is potentially nullable.
This issue could then be considered to be a duplicate of dart-lang/language#3549.
Interestingly, the extension type itself gets the treatment that I'm asking for:
class B<T extends E> {
void b(T? t) {
if (t == null) return;
// `t` is promoted to have type `T`.
E t2 = t; // OK, `T <: E`.
t2.e(); // Sure, no problem! ;-)
}
}
from sdk.
This issue could then be considered to be a duplicate
I think resolving that issue would resolve this one too. I do think we should separately track the fact that CFE and Analyzer don't agree about this code currently. It sounds like the current bug according to the spec is in the CFE not the Analyzer, but we may change the spec to agree with the CFE?
from sdk.
Related Issues (20)
- `dart analyze` cannot analyze file without `.dart` extension. HOT 1
- Hover for docs in `analysis_options`
- Autocomplete in `analysis_options` analyzer -> errors/language
- Analysis server fails to initialize/create contexts with an invalid entry in lint rules
- vm crash on dart fuzzer test - SelectRepresentations at CalculateElementAddressInstr HOT 1
- Dart analyzer crashes reliably working with macros. HOT 16
- TSAN: data race between mutator and background compiler
- Dart 3.3.4 docker error HOT 2
- easy way to customize the diagnostic severity of all lint rules HOT 1
- TSAN: data race in reaching reload safepoint with another isolate starting
- TSAN: data race setting growth policy after loading
- Failures on [dds/dap] Handle Sentinels in invoke toString()/getObject and return error to clients HOT 7
- Dart_SendPortGetId and Dart_NewSendPort lose information about origin_id for the port
- Type check failure in _IOServicePorts._newServicePort HOT 1
- A strange and easily reproducible problem in the AOT-compiled code. HOT 1
- Make `RegExp` and `RegExpMatch` be `final` classes.
- Macro does not give details when run in command line. HOT 12
- Analyzer doesn't allow type parameter declarations as diagnostic targets in macros
- flaky vm/dart/use_dwarf_stack_traces_flag_deferred_test turns RuntimeError on vm-aot-mac-release-x64 HOT 1
- flaky ffi/callback_unwind_error_test turned Crash on vm-aot-linux-release-arm-qemu HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.