-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Sema: Don't construct naked ErrorTypes in valid code #8642
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a08c1a0
Sema: Don't construct naked ErrorTypes in valid code
slavapestov 5f75ac4
Adjust capture typechecking to allow null type
beccadax 9e50c5f
Allow null types in switch typechecking
beccadax 252fa1b
Make ErrorType DeclRefExprs for ErrorType Decls
beccadax a3bda75
Mark failed initializers as errors
beccadax 3084d09
Ignore missing types on for statement expressions
beccadax 1795471
Use type map call instead of setting type directly
beccadax 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
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 |
---|---|---|
|
@@ -1763,11 +1763,12 @@ void ConstraintSystem::shrink(Expr *expr) { | |
/// | ||
/// \param collection The type of the collection container. | ||
/// | ||
/// \returns ErrorType on failure, properly constructed type otherwise. | ||
/// \returns Null type, ErrorType or UnresolvedType on failure, | ||
/// properly constructed type otherwise. | ||
Type extractElementType(Type collection) { | ||
auto &ctx = CS.getASTContext(); | ||
if (collection.isNull() || collection->hasError()) | ||
return ErrorType::get(ctx); | ||
if (!collection || collection->hasError()) | ||
return collection; | ||
|
||
auto base = collection.getPointer(); | ||
auto isInvalidType = [](Type type) -> bool { | ||
|
@@ -1779,19 +1780,19 @@ void ConstraintSystem::shrink(Expr *expr) { | |
if (auto array = dyn_cast<ArraySliceType>(base)) { | ||
auto elementType = array->getBaseType(); | ||
// If base type is invalid let's return error type. | ||
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. Should this be asserted as an invariant @slavapestov? 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. Why would it be an invariant? An array slice type can end up with an error type inside of it |
||
return isInvalidType(elementType) ? ErrorType::get(ctx) : elementType; | ||
return elementType; | ||
} | ||
|
||
// Map or Set or any other associated collection type. | ||
if (auto boundGeneric = dyn_cast<BoundGenericType>(base)) { | ||
if (boundGeneric->hasUnresolvedType()) | ||
return ErrorType::get(ctx); | ||
return boundGeneric; | ||
|
||
llvm::SmallVector<TupleTypeElt, 2> params; | ||
for (auto &type : boundGeneric->getGenericArgs()) { | ||
// One of the generic arguments in invalid or unresolved. | ||
if (isInvalidType(type)) | ||
return ErrorType::get(ctx); | ||
return type; | ||
|
||
params.push_back(type); | ||
} | ||
|
@@ -1803,7 +1804,7 @@ void ConstraintSystem::shrink(Expr *expr) { | |
return TupleType::get(params, ctx); | ||
} | ||
|
||
return ErrorType::get(ctx); | ||
return Type(); | ||
} | ||
|
||
bool isSuitableCollection(TypeRepr *collectionTypeRepr) { | ||
|
@@ -1884,7 +1885,9 @@ void ConstraintSystem::shrink(Expr *expr) { | |
auto elementType = extractElementType(contextualType); | ||
// If we couldn't deduce element type for the collection, let's | ||
// not attempt to solve it. | ||
if (elementType->hasError()) | ||
if (!elementType || | ||
elementType->hasError() || | ||
elementType->hasUnresolvedType()) | ||
return; | ||
|
||
contextualType = elementType; | ||
|
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
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
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.
The fixme notes spawning and solving a type variable here would potentially provide better diagnostics instead of no diagnostics.
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.
If I understand the code correctly, this will only happen if the variable's declaration is erroneous, and the idea is that we might be able to guess the type here so we can add it to the diagnosis there. But that seems really difficult to accomplish—by this point, the error at the declaration site should already have been diagnosed. Even if we did decide to do it, I think that's probably better done as a separate patch; I'm just trying to maintain the status quo in this PR.