Skip to content

Commit

Permalink
Add an alwaysThrows annotation to indicate that a function always thr…
Browse files Browse the repository at this point in the history
…ows.

BUG=#17999
R=brianwilkerson@google.com

Review-Url: https://codereview.chromium.org/2170643003 .
  • Loading branch information
srawlins committed Jun 13, 2017
1 parent 4da8380 commit c6d6a69
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/meta/lib/meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@
/// in the language tour.
library meta;

/// Used to annotate a function `f`. Indicates that `f` always throws an
/// exception. Any functions that override `f`, in class inheritence, are also
/// expected to conform to this contract.
///
/// Tools, such as the analyzer, can use this to understand whether a block of
/// code "exits". For example:
///
/// ```dart
/// @alwaysThrows toss() { throw 'Thrown'; }
///
/// int fn(bool b) {
/// if (b) {
/// return 0;
/// } else {
/// toss();
/// print("Hello.");
/// }
/// }
/// ```
///
/// Without the annotation on `toss`, it would look as though `fn` doesn't
/// always return a value. The annotation shows that `fn` does always exit. In
/// addition, the annotation reveals that any statements following a call to
/// `toss` (like the `print` call) are dead code.
///
/// Tools, such as the analyzer, can also expect this contract to be enforced;
/// that is, tools may emit warnings if a function with this annotation
/// _doesn't_ always throw.
const _AlwaysThrows alwaysThrows = const _AlwaysThrows();

/// Used to annotate a parameter of an instance method that overrides another
/// method.
///
Expand Down Expand Up @@ -195,6 +225,10 @@ class Required {
const Required([this.reason]);
}

class _AlwaysThrows {
const _AlwaysThrows();
}

class _Checked {
const _Checked();
}
Expand Down

2 comments on commit c6d6a69

@nex3
Copy link
Member

@nex3 nex3 commented on c6d6a69 Jul 7, 2017

Choose a reason for hiding this comment

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

Was this ever released?

@srawlins
Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, releasing now, meta 1.1.0

Please sign in to comment.