-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[breaking change] Change the context for the operand of throw to Object. #56065
Copy link
Copy link
Closed
Labels
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).Dart language related items (some items might be better tracked at github.com/dart-lang/language).breaking-change-approvedbreaking-change-requestThis tracks requests for feedback on breaking changesThis tracks requests for feedback on breaking changestriage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-enhancementA request for a change that isn't a bugA request for a change that isn't a bug
Metadata
Metadata
Assignees
Labels
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).Dart language related items (some items might be better tracked at github.com/dart-lang/language).breaking-change-approvedbreaking-change-requestThis tracks requests for feedback on breaking changesThis tracks requests for feedback on breaking changestriage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-enhancementA request for a change that isn't a bugA request for a change that isn't a bug
Type
Projects
Status
Complete
Intended Change
The type schema for type inference of the operand of a
throwexpression will be changed from_(the unknown type) to non-nullableObject.Rationale
The type schema for an expression is intended to capture information from the surrounding context about what static type the expression is expected or intended to have. Since the operand of a
throwexpression is required by the compiler to be assignable toObject, it makes sense for this type to also be the context. Making this change furthers a longstanding goal of removing inconsistencies and unexpected behaviors from the Dart type inference process.Expected Impact
The impact of this change is expected to be very low, since the operands of most
throwexpressions do not require any type inference. A trial run of this change over Google's internal Dart code base caused zero breakages.However, it's theoretically possible that some code could change behavior as a result of this change. Here is an example of a program whose behavior would change:
Today this program prints
Bad state: f<dynamic> was called. With the change, it will printBad state: f<Object> was called.Mitigation
In the unlikely event that some code is affected by this change, the old behavior may be restored by supplying explicit types inside the operand of the
throwexpression, so that no type inference is needed. For example, in the code above, the old behavior may be restored by changingthrow f()tothrow f<dynamic>().