Skip to content

[ClangImporter] C static-qualified array params are non-nullable. #5617

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 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/ClangImporter/ClangAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,19 +695,28 @@ bool importer::isUnavailableInSwift(
return false;
}

OptionalTypeKind importer::getParamOptionality(const clang::ParmVarDecl *param,
OptionalTypeKind importer::getParamOptionality(version::Version swiftVersion,
const clang::ParmVarDecl *param,
bool knownNonNull) {
auto &clangCtx = param->getASTContext();

// If nullability is available on the type, use it.
if (auto nullability = param->getType()->getNullability(clangCtx)) {
clang::QualType paramTy = param->getType();
if (auto nullability = paramTy->getNullability(clangCtx)) {
return translateNullability(*nullability);
}

// If it's known non-null, use that.
if (knownNonNull || param->hasAttr<clang::NonNullAttr>())
return OTK_None;

// Check for the 'static' annotation on C arrays.
if (!swiftVersion.isVersion3())
if (const auto *DT = dyn_cast<clang::DecayedType>(paramTy))
Copy link
Member

@milseman milseman Nov 8, 2016

Choose a reason for hiding this comment

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

Is the intent here for 4 and later, or this a problem only in swift 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"4 and later". I'm honestly not sure what our idiom should be here (why do we have a particular accessor at all?). @graydon?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a convenience accessor, I figured people sprinkling explicit version comparisons in their code would be worse.

(nb: there's no support for setting --swift-version earlier than 3 so "not 3" means >= 3 in this case, but probably a more explicit convenience accessor would be better.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess in the C world you don't say "C > 99" or something. It's only because we have sequential version numbers that we even have this option.

In Clang we check for the new version of the language (if (LangOpts.C99), if (LangOpts.CXX14)), but those checks are designed to stay around. Checking isVersion3 feels like a reminder that the check will go away if/when we drop support for Swift 3 mode.

if (const auto *AT = DT->getOriginalType()->getAsArrayTypeUnsafe())
if (AT->getSizeModifier() == clang::ArrayType::Static)
return OTK_None;

// Default to implicitly unwrapped optionals.
return OTK_ImplicitlyUnwrappedOptional;
}
6 changes: 5 additions & 1 deletion lib/ClangImporter/ClangAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ bool isUnavailableInSwift(const clang::Decl *decl, const PlatformAvailability &,

/// Determine the optionality of the given Clang parameter.
///
/// \param swiftLanguageVersion What version of Swift we're using, which affects
/// how optionality is inferred.
///
/// \param param The Clang parameter.
///
/// \param knownNonNull Whether a function- or method-level "nonnull" attribute
/// applies to this parameter.
OptionalTypeKind getParamOptionality(const clang::ParmVarDecl *param,
OptionalTypeKind getParamOptionality(version::Version swiftLanguageVersion,
const clang::ParmVarDecl *param,
bool knownNonNull);
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ static bool omitNeedlessWordsInFunctionName(
Optional<unsigned> errorParamIndex, bool returnsSelf, bool isInstanceMethod,
NameImporter &nameImporter) {
clang::ASTContext &clangCtx = nameImporter.getClangContext();
const version::Version &swiftLanguageVersion =
nameImporter.getLangOpts().EffectiveLanguageVersion;

// Collect the parameter type names.
StringRef firstParamName;
Expand Down Expand Up @@ -741,7 +743,8 @@ static bool omitNeedlessWordsInFunctionName(
bool hasDefaultArg =
ClangImporter::Implementation::inferDefaultArgument(
param->getType(),
getParamOptionality(param, !nonNullArgs.empty() && nonNullArgs[i]),
getParamOptionality(swiftLanguageVersion, param,
!nonNullArgs.empty() && nonNullArgs[i]),
nameImporter.getIdentifier(baseName), numParams, argumentName,
i == 0, isLastParameter, nameImporter) != DefaultArgumentKind::None;

Expand Down
8 changes: 5 additions & 3 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(

// Check nullability of the parameter.
OptionalTypeKind OptionalityOfParam =
getParamOptionality(param, !nonNullArgs.empty() && nonNullArgs[index]);
getParamOptionality(SwiftContext.LangOpts.EffectiveLanguageVersion,
param, !nonNullArgs.empty() && nonNullArgs[index]);

ImportTypeKind importKind = ImportTypeKind::Parameter;
if (param->hasAttr<clang::CFReturnsRetainedAttr>())
Expand Down Expand Up @@ -1933,8 +1934,9 @@ Type ClangImporter::Implementation::importMethodType(

// Check nullability of the parameter.
OptionalTypeKind optionalityOfParam
= getParamOptionality(param,
!nonNullArgs.empty() && nonNullArgs[paramIndex]);
= getParamOptionality(SwiftContext.LangOpts.EffectiveLanguageVersion,
param,
!nonNullArgs.empty() && nonNullArgs[paramIndex]);

bool allowNSUIntegerAsIntInParam = isFromSystemModule;
if (allowNSUIntegerAsIntInParam) {
Expand Down
5 changes: 5 additions & 0 deletions test/ClangImporter/ctypes_parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ func testArrays() {

nullableArrayParameters([], [], [])
nullableArrayParameters(nil, nil, nil)

// It would also be nice to warn here about the arrays being too short, but
// that's probably beyond us for a while.
staticBoundsArray([])
staticBoundsArray(nil) // no-error
}
10 changes: 10 additions & 0 deletions test/ClangImporter/ctypes_parse_swift4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-parse-verify-swift %clang-importer-sdk -swift-version 4

import ctypes

func testArrays() {
// It would also be nice to warn here about the arrays being too short, but
// that's probably beyond us for a while.
staticBoundsArray([])
staticBoundsArray(nil) // expected-error {{nil is not compatible with expected argument type 'UnsafePointer<Int8>'}}
}
1 change: 1 addition & 0 deletions test/Inputs/clang-importer-sdk/usr/include/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ typedef struct ModRM {
// Arrays
//===---
void useArray(char x[4], char y[], char z[][8]);
void staticBoundsArray(const char x[static 4]);

typedef const int FourConstInts[4];
void nonnullArrayParameters(const char x[_Nonnull], void * const _Nullable y[_Nonnull], _Nonnull FourConstInts z);
Expand Down