-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Better errors for types with same name #1575
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
DanielRosenwasser
merged 11 commits into
microsoft:master
from
chrisbubernak:betterErrorsForTypesWithSameName
Jan 31, 2015
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fddc225
fixed checker and added a test case
chrisbubernak 82fcaa8
added baselines for new test case
chrisbubernak 07d3c20
updating baseines
chrisbubernak 5838900
remove commented out code
chrisbubernak 3b55a0c
removed errant text
chrisbubernak 8808a69
rewrote the fix to use a new type format flag and fixed the baselines…
chrisbubernak f39a25b
made some changes based cr feedback
chrisbubernak 52a6ba6
changed logic based on CR feedback to not get fully qualified for typ…
chrisbubernak ce794e8
more cr feedback: rename undefined flags param to None
chrisbubernak d35757d
fixing error in last checkin
chrisbubernak afc9f45
more changes based on CR feedback
chrisbubernak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1033,7 +1033,7 @@ module ts { | |
* Enclosing declaration is optional when we don't want to get qualified name in the enclosing declaration scope | ||
* Meaning needs to be specified if the enclosing declaration is given | ||
*/ | ||
function buildSymbolDisplay(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags): void { | ||
function buildSymbolDisplay(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags, typeFlags?: TypeFormatFlags): void { | ||
var parentSymbol: Symbol; | ||
function appendParentTypeArgumentsAndSymbolName(symbol: Symbol): void { | ||
if (parentSymbol) { | ||
|
@@ -1095,11 +1095,12 @@ module ts { | |
} | ||
} | ||
|
||
// Get qualified name | ||
if (enclosingDeclaration && | ||
// TypeParameters do not need qualification | ||
!(symbol.flags & SymbolFlags.TypeParameter)) { | ||
|
||
// Get qualified name if the symbol is not a type parameter | ||
// and there is an enclosing declaration or we specifically | ||
// asked for it | ||
var isTypeParameter = symbol.flags & SymbolFlags.TypeParameter; | ||
var typeFormatFlag = TypeFormatFlags.UseFullyQualifiedType & typeFlags; | ||
if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) { | ||
walkSymbol(symbol, meaning); | ||
return; | ||
} | ||
|
@@ -1122,7 +1123,8 @@ module ts { | |
writeTypeReference(<TypeReference>type, flags); | ||
} | ||
else if (type.flags & (TypeFlags.Class | TypeFlags.Interface | TypeFlags.Enum | TypeFlags.TypeParameter)) { | ||
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type); | ||
// The specified symbol flags need to be reinterpreted as type flags | ||
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, flags); | ||
} | ||
else if (type.flags & TypeFlags.Tuple) { | ||
writeTupleType(<TupleType>type); | ||
|
@@ -1193,17 +1195,18 @@ module ts { | |
function writeAnonymousType(type: ObjectType, flags: TypeFormatFlags) { | ||
// Always use 'typeof T' for type of class, enum, and module objects | ||
if (type.symbol && type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) { | ||
writeTypeofSymbol(type); | ||
writeTypeofSymbol(type, flags); | ||
} | ||
// Use 'typeof T' for types of functions and methods that circularly reference themselves | ||
else if (shouldWriteTypeOfFunctionSymbol()) { | ||
writeTypeofSymbol(type); | ||
writeTypeofSymbol(type, flags); | ||
} | ||
else if (typeStack && contains(typeStack, type)) { | ||
// If type is an anonymous type literal in a type alias declaration, use type alias name | ||
var typeAlias = getTypeAliasForTypeLiteral(type); | ||
if (typeAlias) { | ||
buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, SymbolFlags.Type); | ||
// The specified symbol flags need to be reinterpreted as type flags | ||
buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, flags); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as for |
||
} | ||
else { | ||
// Recursive usage, use any | ||
|
@@ -1237,10 +1240,10 @@ module ts { | |
} | ||
} | ||
|
||
function writeTypeofSymbol(type: ObjectType) { | ||
function writeTypeofSymbol(type: ObjectType, typeFormatFlags?: TypeFormatFlags) { | ||
writeKeyword(writer, SyntaxKind.TypeOfKeyword); | ||
writeSpace(writer); | ||
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Value); | ||
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Value, SymbolFormatFlags.None, typeFormatFlags); | ||
} | ||
|
||
function getIndexerParameterName(type: ObjectType, indexKind: IndexKind, fallbackName: string): string { | ||
|
@@ -3508,7 +3511,13 @@ module ts { | |
} | ||
if (reportErrors) { | ||
headMessage = headMessage || Diagnostics.Type_0_is_not_assignable_to_type_1; | ||
reportError(headMessage, typeToString(source), typeToString(target)); | ||
var sourceType = typeToString(source); | ||
var targetType = typeToString(target); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would write this as: if (sourceType === targetType) {
sourceType = typeToString(...);
targetType = typeToString(...);
}
reportError(headMessage, sourceType, targetType) |
||
if (sourceType === targetType) { | ||
sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType); | ||
targetType = typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType); | ||
} | ||
reportError(headMessage, sourceType, targetType); | ||
} | ||
return Ternary.False; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/baselines/reference/differentTypesWithSameName.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
tests/cases/compiler/differentTypesWithSameName.ts(16,15): error TS2345: Argument of type 'variable' is not assignable to parameter of type 'm.variable'. | ||
|
||
|
||
==== tests/cases/compiler/differentTypesWithSameName.ts (1 errors) ==== | ||
module m { | ||
export class variable{ | ||
s: string; | ||
} | ||
export function doSomething(v: m.variable) { | ||
|
||
} | ||
} | ||
|
||
class variable { | ||
t: number; | ||
} | ||
|
||
|
||
var v: variable = new variable(); | ||
m.doSomething(v); | ||
~ | ||
!!! error TS2345: Argument of type 'variable' is not assignable to parameter of type 'm.variable'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//// [differentTypesWithSameName.ts] | ||
module m { | ||
export class variable{ | ||
s: string; | ||
} | ||
export function doSomething(v: m.variable) { | ||
|
||
} | ||
} | ||
|
||
class variable { | ||
t: number; | ||
} | ||
|
||
|
||
var v: variable = new variable(); | ||
m.doSomething(v); | ||
|
||
//// [differentTypesWithSameName.js] | ||
var m; | ||
(function (m) { | ||
var variable = (function () { | ||
function variable() { | ||
} | ||
return variable; | ||
})(); | ||
m.variable = variable; | ||
function doSomething(v) { | ||
} | ||
m.doSomething = doSomething; | ||
})(m || (m = {})); | ||
var variable = (function () { | ||
function variable() { | ||
} | ||
return variable; | ||
})(); | ||
var v = new variable(); | ||
m.doSomething(v); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module m { | ||
export class variable{ | ||
s: string; | ||
} | ||
export function doSomething(v: m.variable) { | ||
|
||
} | ||
} | ||
|
||
class variable { | ||
t: number; | ||
} | ||
|
||
|
||
var v: variable = new variable(); | ||
m.doSomething(v); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is subtle - leave a comment saying that in these cases, the specified symbol flags need to be reinterpreted as type flags.