Skip to content

Turn 2 type flags into properties#11212

Merged
sandersn merged 9 commits into
masterfrom
cleanup-TypeFlags
Oct 3, 2016
Merged

Turn 2 type flags into properties#11212
sandersn merged 9 commits into
masterfrom
cleanup-TypeFlags

Conversation

@sandersn
Copy link
Copy Markdown
Member

This is needed for object spread and rest, which will each need a type
flag.

There are 4-5 other likely targets for removal, and I may remove those
later.

1. Instantiated (only modifies anonymous types)
2. ObjectLiteralWithComputedProperties (only modifies [resolved] object types)
3. ThisType (only modifies type parameters)

This is needed for object spread and rest, which will each need a type
flag.

There are 4-5 other likely targets for removal, and I may remove those
later.
@sandersn
Copy link
Copy Markdown
Member Author

@ahejlsberg and @mhegazy can you take a look at this?

Comment thread src/compiler/types.ts
Union = 1 << 19, // Union (T | U)
Intersection = 1 << 20, // Intersection (T & U)
Anonymous = 1 << 21, // Anonymous
Instantiated = 1 << 22, // Instantiated anonymous type
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would preserve this flag unless you really need to free it up. In generic code we create lots of instantiated anonymous types and it would be nice not to incur the overhead of another property.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I only need one flag right now (Spread), but I'll need another for Rest and Ryan will need one for Subset. Maybe ObjectLiteral is a better target?

FreshLiteral seems pretty easy to excise as well, but there are a lot of literals in any given program.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instantiated is back in. I'm looking at ObjectLiteral instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd leave ObjectLiteral alone. Just free up the other two for now and then we can do more if and when it is needed. I'd prefer not incurring overhead unless we have to.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/compiler/checker.ts Outdated
if (hasComputedProperties) {
result.flags |= TypeFlags.ObjectLiteralPatternWithComputedProperties;
}
result.inObjectLiteralPatternWithComputedProperties = hasComputedProperties;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only create this property if hasComputedProperties is true.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, turns out I was wrong when I thought that x.prop = undefined is a no-op. Fixed.

Comment thread src/compiler/checker.ts Outdated
* type itself. Note that the apparent type of a union type is the union type itself.
*/
function getApparentType(type: Type): Type {
function getApparentType(type: Type): ObjectType {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why this change? This function isn't guaranteed to return an ObjectType.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All the branches do return ObjectType, but I forgot about the else branch. Reverted.

Comment thread src/compiler/checker.ts Outdated

function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean {
if (!(target.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties) && maybeTypeOfKind(target, TypeFlags.ObjectType)) {
if (maybeTypeOfKind(target, TypeFlags.ObjectType) && !(target as ObjectType).inObjectLiteralPatternWithComputedProperties) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The maybeTypeOfKind function could return true when target is a union or intersection containing an object type, so the second check needs to first ensure target is actually an object type.

Copy link
Copy Markdown
Member Author

@sandersn sandersn Sep 29, 2016

Choose a reason for hiding this comment

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

I was relying on unions and intersections not having inObjectLiteralPatternWithComputedProperties and having the check come back with undefined. Adding the check is the right thing, though.

Comment thread src/compiler/checker.ts Outdated
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
// be "pushed" onto a node using the contextualType property.
function getApparentTypeOfContextualType(node: Expression): Type {
function getApparentTypeOfContextualType(node: Expression): ObjectType {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The returned type could be something other than an ObjectType so this change isn't correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed

Comment thread src/compiler/checker.ts Outdated
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : TypeFlags.FreshLiteral;
result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags) | (patternWithComputedProperties ? TypeFlags.ObjectLiteralPatternWithComputedProperties : 0);
result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags);
result.inObjectLiteralPatternWithComputedProperties = patternWithComputedProperties;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only set the property if patternWithComputedProperties is true.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@sandersn
Copy link
Copy Markdown
Member Author

I put Instantiated back in and removed ObjectLiteral instead. This keeps 3 flags open for use with Spread, Rest and Subset.

@sandersn
Copy link
Copy Markdown
Member Author

Oops, I didn't see your new comments, @ahejlsberg. I put ObjectLiteral back as well.

@Igorbek
Copy link
Copy Markdown
Contributor

Igorbek commented Sep 29, 2016

Why didn't you just introduce a new flags2 ?

@sandersn sandersn changed the title Turn 3 type flags into properties Turn 2 type flags into properties Oct 3, 2016
Comment thread src/compiler/checker.ts Outdated
}
if (hasComputedProperties) {
result.flags |= TypeFlags.ObjectLiteralPatternWithComputedProperties;
result.inObjectLiteralPatternWithComputedProperties = hasComputedProperties;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just say = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, shouldn't it be isObjectBlaBlaBla...? In other words, "is" instead of "in".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/compiler/checker.ts Outdated
(contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression);
let typeFlags: TypeFlags = 0;
let patternWithComputedProperties = false;
let patternWithComputedProperties: boolean | undefined;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need for this change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/compiler/checker.ts Outdated
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): ResolvedType {
const members = createMap<Symbol>();
let hasComputedProperties = false;
let hasComputedProperties: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need for this change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/compiler/checker.ts Outdated

// Return the type implied by an object binding pattern
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): ResolvedType {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One more change that isn't necessary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

reverted to Type

@sandersn sandersn merged commit 4b8e8b7 into master Oct 3, 2016
@sandersn sandersn deleted the cleanup-TypeFlags branch October 3, 2016 23:25
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants