dart-sdk/pkg/analyzer
Paul Berry 130d6199c3 Field promotion: make the core promotability algorithm sharable; fix bugs
In the following code, it's not safe for the field `C._f` to undergo
type promotion, because a variable with static type `C` might have
type `D` at runtime, in which case `C._f` will get dispatched to
`noSuchMethod`, which is not guaranteed to return a stable result.

    class C {
      final int? _f;
    }
    class D implements C {
      noSuchMethod(_) => ...;
    }
    foo(C c) {
      if (c._f != null) {
        print(c._f + 1); // UNSAFE!
      }
    }

Therefore, in order to determine which fields are promotable, the
implementations need to analyze enough of the class hierarchy to
figure out which field accesses might get dispatched to
`noSuchMethod`.

Currently, the CFE does this by following its usual algorithm for
generating `noSuchMethod` forwarders before trying to determine which
fields are promotable. The analyzer, on the other hand, doesn't have
an algorithm for generating `noSuchMethod` forwarders (since it
doesn't implement execution semantics); so instead it has its own
logic to figure out when a `noSuchMethod` forwarder is needed for a
field, and disable promotion for that field.

But there's a chicken-and-egg problem in the CFE: the CFE needs to
determine which fields are promotable before doing top-level inference
(since the initializers of top-level fields might make use of field
promotion, affecting their inferred types--see #50522). But it doesn't
decide where `noSuchMethod` forwarders are needed until after
top-level inference (because the same phase that generates
`noSuchMethod` forwarders also generates forwarders that do runtime
covariant type-checking, and so it has to run after all top level
types have been inferred).

To fix the chicken-and-egg problem, I plan to rework the CFE so that
it uses the same algorithm as the analyzer to determine which fields
are promotable. This CL makes a first step towards that goal, by
reworking the analyzer's field promotability algorithm into a form
where it can be shared with the CFE, and moving it to
`package:_fe_analyzer_shared`.  Since this required a fairly
substantial rewrite, I went ahead and fixed #52938 in the process.

Fixes #52938.

Change-Id: I9e68f51b3ea9a967f55f15bdc445cc1c0efdabdd
Bug: https://github.com/dart-lang/sdk/issues/52938
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313293
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-07-18 18:54:26 +00:00
..
doc Capture the agreed on style for import prefix names 2023-07-18 04:09:58 +00:00
example Rename AnalysisSession.getXyz2() into getXyz(). 2021-07-12 22:42:58 +00:00
lib Field promotion: make the core promotability algorithm sharable; fix bugs 2023-07-18 18:54:26 +00:00
test Field promotion: make the core promotability algorithm sharable; fix bugs 2023-07-18 18:54:26 +00:00
tool Update glossary links and remove content from generated page. 2023-07-17 21:22:42 +00:00
.gitignore
analysis_options.yaml Ignore TODO in pkg/analyzer and pkg/analysis_server. 2023-04-21 19:16:04 +00:00
CHANGELOG.md Deprecate ExecutableElement.returnType, use returnType2 instead. 2023-06-29 23:16:59 +00:00
LICENSE Update LICENSE 2021-04-07 10:28:38 +00:00
messages.yaml Update glossary links and remove content from generated page. 2023-07-17 21:22:42 +00:00
OWNERS [infra] Add OWNERS to the Dart SDK 2022-02-14 14:06:34 +00:00
pubspec.yaml Add InstanceElement as a super-interface for InterfaceElement and InlineClassElement. 2023-06-28 00:56:05 +00:00
README.md Link to dart.dev for linter rules 2023-06-02 18:26:13 +00:00
TRIAGE.md [analyzer] Remove many instances of the word 'hint' 2023-03-23 03:59:55 +00:00

pub package package publisher

This package provides a library that performs static analysis of Dart code. It is useful for tool integration and embedding.

End-users should use the dart analyze command-line tool to analyze their Dart code.

Integrators that want to add Dart support to their editor should use the Dart Analysis Server. The Analysis Server API Specification is available. If you are adding Dart support to an editor or IDE, please let us know by emailing our list.

Configuring the analyzer

Both dart analyze and Dart Analysis Server can be configured with an analysis_options.yaml file (using an .analysis_options file is deprecated). This YAML file can control which files and paths are analyzed, which lints are applied, and more.

If you are embedding the analyzer library in your project, you are responsible for finding the analysis options file, parsing it, and configuring the analyzer.

The analysis options file should live at the root of your project (for example, next to your pubspec.yaml). Different embedders of analyzer, such as dart analyze or Dart Analysis Server, may choose to find the file in various different ways. Consult their documentation to learn more.

Here is an example file that instructs the analyzer to ignore two files:

analyzer:
  exclude:
    - test/_data/p4/lib/lib1.dart
    - test/_data/p5/p5.dart
    - test/_data/bad*.dart
    - test/_brokendata/**

Note that you can use globs, as defined by the glob package.

Here is an example file that enables two lint rules:

linter:
  rules:
    - camel_case_types
    - empty_constructor_bodies

Check out all the available Dart lint rules.

You can combine the analyzer section and the linter section into a single configuration. Here is an example:

analyzer:
  exclude:
    - test/_data/p4/lib/lib1.dart
linter:
  rules:
    - camel_case_types

For more information, see the docs for customizing static analysis.

Who uses this library?

Many tools embed this library, such as:

Support

Post issues and feature requests at https://github.com/dart-lang/sdk/issues. These will be triaged according to the analyzer triage priorities.

Questions and discussions are welcome at the Dart Analyzer Discussion Group.

Background

The APIs in this package were originally machine generated by a translator and were based on an earlier Java implementation. Several of the API's still look like their Java predecessors rather than clean Dart APIs.

In addition, there is currently no clean distinction between public and internal APIs. We plan to address this issue but doing so will, unfortunately, require a large number of breaking changes. We will try to minimize the pain this causes for our clients, but some pain is inevitable.

License

See the LICENSE file.