Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Numeric literals assignable to enum literals only when values match #51561

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

Merged
merged 6 commits into from
Nov 17, 2022

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Nov 16, 2022

For purposes of representing collections of bit flags as enum types we have always permitted type number to be assigned to any numeric enum literal type (because number is the type that results when bit flags are or'd and and'ed using the | and & operators). However, we have also permitted any numeric literal type to be assigned to any numeric enum literal type, even when the underlying numeric values differ. This rule makes little sense and is revoked by this PR.

The general effect of this change is that when a numeric type value is known not to match any of the declared members of an enum type (because the numeric value has a literal type that doesn't have a match in the enum type), the numeric type value is not assignable to that enum type. This in particular means that the literal value 0 cannot be used to initialize a bit flags enum type unless the enum type declares a member with the value 0.

const enum Flags1 {
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2,
}

const enum Flags2 {
    None = 0,
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2,
}

let a: Flags1 = 0;  // Error
let b: Flags1 = 0 as Flags1;  // Ok
let c: Flags2 = Flags2.None;  // Ok

Note above that it is always possible to use an as assertion to coerce an arbitrary numeric value to an enum type.

Fixes #51466.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 16, 2022
@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at dadfbcb. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at dadfbcb. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at dadfbcb. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the extended test suite on this PR at dadfbcb. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at dadfbcb. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/51561/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@ahejlsberg
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..51561

Metric main 51561 Delta Best Worst
Angular - node (v16.17.1, x64)
Memory used 340,575k (± 0.02%) 340,598k (± 0.02%) +24k (+ 0.01%) 340,408k 340,719k
Parse Time 1.88s (± 0.66%) 1.90s (± 0.76%) +0.02s (+ 0.96%) 1.86s 1.93s
Bind Time 0.65s (± 0.90%) 0.65s (± 0.51%) +0.00s (+ 0.46%) 0.64s 0.66s
Check Time 5.15s (± 0.41%) 5.14s (± 0.42%) -0.01s (- 0.16%) 5.11s 5.20s
Emit Time 5.10s (± 0.93%) 5.11s (± 0.61%) +0.01s (+ 0.12%) 5.04s 5.18s
Total Time 12.78s (± 0.51%) 12.80s (± 0.40%) +0.02s (+ 0.13%) 12.69s 12.88s
Compiler-Unions - node (v16.17.1, x64)
Memory used 188,225k (± 0.65%) 186,636k (± 0.02%) -1,589k (- 0.84%) 186,557k 186,715k
Parse Time 0.80s (± 1.21%) 0.79s (± 0.89%) -0.01s (- 1.00%) 0.78s 0.81s
Bind Time 0.42s (± 1.06%) 0.42s (± 0.53%) -0.00s (- 0.71%) 0.41s 0.42s
Check Time 6.04s (± 0.70%) 6.07s (± 0.58%) +0.03s (+ 0.48%) 6.02s 6.16s
Emit Time 1.90s (± 0.71%) 1.92s (± 0.93%) +0.02s (+ 0.95%) 1.88s 1.96s
Total Time 9.16s (± 0.63%) 9.20s (± 0.54%) +0.04s (+ 0.45%) 9.10s 9.33s
Monaco - node (v16.17.1, x64)
Memory used 319,835k (± 0.01%) 319,825k (± 0.01%) -11k (- 0.00%) 319,765k 319,902k
Parse Time 1.42s (± 0.49%) 1.41s (± 0.67%) -0.01s (- 0.42%) 1.39s 1.44s
Bind Time 0.59s (± 1.09%) 0.59s (± 0.38%) -0.00s (- 0.51%) 0.58s 0.59s
Check Time 4.87s (± 0.42%) 4.87s (± 0.40%) +0.00s (+ 0.08%) 4.83s 4.92s
Emit Time 2.73s (± 0.74%) 2.71s (± 1.31%) -0.02s (- 0.81%) 2.65s 2.79s
Total Time 9.61s (± 0.41%) 9.58s (± 0.53%) -0.03s (- 0.30%) 9.45s 9.67s
TFS - node (v16.17.1, x64)
Memory used 282,296k (± 0.00%) 282,284k (± 0.02%) -11k (- 0.00%) 282,121k 282,344k
Parse Time 1.17s (± 0.71%) 1.17s (± 0.60%) +0.00s (+ 0.34%) 1.16s 1.18s
Bind Time 0.66s (± 3.75%) 0.66s (± 4.30%) -0.00s (- 0.46%) 0.60s 0.71s
Check Time 4.76s (± 0.54%) 4.77s (± 0.31%) +0.01s (+ 0.17%) 4.74s 4.81s
Emit Time 2.75s (± 2.13%) 2.77s (± 2.15%) +0.02s (+ 0.62%) 2.70s 2.94s
Total Time 9.34s (± 0.68%) 9.36s (± 0.56%) +0.02s (+ 0.27%) 9.27s 9.50s
material-ui - node (v16.17.1, x64)
Memory used 435,254k (± 0.00%) 435,241k (± 0.00%) -14k (- 0.00%) 435,197k 435,272k
Parse Time 1.64s (± 0.50%) 1.65s (± 0.28%) +0.01s (+ 0.79%) 1.64s 1.66s
Bind Time 0.50s (± 1.11%) 0.50s (± 0.59%) 0.00s ( 0.00%) 0.50s 0.51s
Check Time 11.83s (± 0.86%) 11.86s (± 1.12%) +0.03s (+ 0.27%) 11.71s 12.35s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 13.97s (± 0.80%) 14.01s (± 0.96%) +0.05s (+ 0.33%) 13.86s 14.51s
xstate - node (v16.17.1, x64)
Memory used 515,977k (± 0.01%) 516,001k (± 0.00%) +24k (+ 0.00%) 515,950k 516,074k
Parse Time 2.30s (± 0.79%) 2.31s (± 0.54%) +0.01s (+ 0.26%) 2.29s 2.35s
Bind Time 0.83s (± 0.93%) 0.83s (± 1.60%) +0.00s (+ 0.36%) 0.81s 0.88s
Check Time 1.34s (± 0.83%) 1.36s (± 0.41%) +0.02s (+ 1.26%) 1.35s 1.37s
Emit Time 0.06s (± 0.00%) 0.06s (± 0.00%) 0.00s ( 0.00%) 0.06s 0.06s
Total Time 4.55s (± 0.54%) 4.57s (± 0.44%) +0.03s (+ 0.57%) 4.54s 4.62s
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 51561 10
Baseline main 10

Developer Information:

Download Benchmark

@@ -8659,7 +8660,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function formatUnionTypes(types: readonly Type[]): Type[] {
const result: Type[] = [];
let flags: TypeFlags = 0;
let flags = 0 as TypeFlags;
Copy link
Member

@RyanCavanaugh RyanCavanaugh Nov 16, 2022

Choose a reason for hiding this comment

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

Consider adding TypeFlags.Uninitialized = 0 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would be an alternative. But I prefer 0 as TypeFlags as an indicator this isn't actually a permitted value.

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top-repos suite comparing main and refs/pull/51561/merge:

Something interesting changed - please have a look.

Details

microsoft/vscode

4 of 54 projects failed to build with the old tsc and were ignored

src/tsconfig.monaco.json

src/tsconfig.tsec.json

@ahejlsberg
Copy link
Member Author

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Nov 16, 2022

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 4220f58. You can monitor the build here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Breaking Change Would introduce errors in existing code For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior between assignment and "as" with object literals after enums unification
3 participants