Skip to content

Add support for @Unsafe to analyzer #40431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/analyzer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.39.6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be 0.39.5?

/cc @stereotype441

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am not sure what -dev means....

* Add support for `@Unsafe` annotation as `isUnsafe` and `hasUnsafe`.

## 0.39.5-dev
* Deprecated `ClassElement.instantiateToBounds()` and
`FunctionTypeAliasElement.instantiateToBounds()`. With the null-safety
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ abstract class Element implements AnalysisTarget {
/// Return `true` if this element has an annotation of the form `@sealed`.
bool get hasSealed;

/// Return `true` if this element has an annotation of the form `@Unsafe`.
bool get hasUnsafe;

/// Return `true` if this element has an annotation of the form
/// `@visibleForTemplate`.
bool get hasVisibleForTemplate;
Expand Down Expand Up @@ -782,6 +785,9 @@ abstract class ElementAnnotation implements ConstantEvaluationTarget {
/// sealed.
bool get isSealed;

/// Return `true` if this annotation marks a method or field as unsafe.
bool get isUnsafe;

/// Return `true` if this annotation marks the associated member as being
/// visible for template files.
bool get isVisibleForTemplate;
Expand Down
21 changes: 21 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,9 @@ class ElementAnnotationImpl implements ElementAnnotation {
/// The name of the top-level variable used to mark a class as being sealed.
static const String _SEALED_VARIABLE_NAME = "sealed";

/// The name of the top-level class used to mark an API as unsafe.
static const String _UNSAFE_CLASS_NAME = "Unsafe";

/// The name of the top-level variable used to mark a method as being
/// visible for templates.
static const String _VISIBLE_FOR_TEMPLATE_VARIABLE_NAME =
Expand Down Expand Up @@ -2540,6 +2543,12 @@ class ElementAnnotationImpl implements ElementAnnotation {
element.name == _SEALED_VARIABLE_NAME &&
element.library?.name == _META_LIB_NAME;

@override
bool get isUnsafe =>
element is ClassElement &&
element.name == _UNSAFE_CLASS_NAME &&
element.library?.name == _META_LIB_NAME;

@override
bool get isVisibleForTemplate =>
element is PropertyAccessorElement &&
Expand Down Expand Up @@ -2864,6 +2873,18 @@ abstract class ElementImpl implements Element {
return false;
}

@override
bool get hasUnsafe {
var metadata = this.metadata;
for (var i = 0; i < metadata.length; i++) {
var annotation = metadata[i];
if (annotation.isUnsafe) {
return true;
}
}
return false;
}

@override
bool get hasVisibleForTemplate {
var metadata = this.metadata;
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ abstract class Member implements Element {
@override
bool get hasSealed => _declaration.hasSealed;

@override
bool get hasUnsafe => _declaration.hasUnsafe;

@override
bool get hasVisibleForTemplate => _declaration.hasVisibleForTemplate;

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: analyzer
version: 0.39.4
version: 0.39.6
description: This package provides a library that performs static analysis of Dart code.
homepage: https://github.com/dart-lang/sdk/tree/master/pkg/analyzer

Expand Down