Skip to content

Commit be4df5a

Browse files
committed
[CSDiagnostics] Diagnose attempts to specialize with invalid number of generic arguments
```swift struct Test<T, U> {} _ = Test<Int>() // error ```
1 parent cd057bb commit be4df5a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9167,3 +9167,9 @@ bool ConcreteTypeSpecialization::diagnoseAsError() {
91679167
emitDiagnostic(diag::not_a_generic_type, ConcreteType);
91689168
return true;
91699169
}
9170+
9171+
bool InvalidTypeSpecializationArity::diagnoseAsError() {
9172+
emitDiagnostic(diag::type_parameter_count_mismatch, D->getBaseIdentifier(),
9173+
NumParams, NumArgs, NumArgs < NumParams, HasParameterPack);
9174+
return true;
9175+
}

lib/Sema/CSDiagnostics.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,30 @@ class ConcreteTypeSpecialization final : public FailureDiagnostic {
30693069
bool diagnoseAsError() override;
30703070
};
30713071

3072+
/// Diagnose attempts to specialize with invalid number of generic arguments:
3073+
///
3074+
/// \code
3075+
/// struct Test<T, U> {}
3076+
///
3077+
/// _ = Test<Int>() // error
3078+
/// \endcode
3079+
class InvalidTypeSpecializationArity final : public FailureDiagnostic {
3080+
ValueDecl *D;
3081+
unsigned NumParams;
3082+
unsigned NumArgs;
3083+
bool HasParameterPack;
3084+
3085+
public:
3086+
InvalidTypeSpecializationArity(const Solution &solution, ValueDecl *decl,
3087+
unsigned numParams, unsigned numArgs,
3088+
bool hasParameterPack,
3089+
ConstraintLocator *locator)
3090+
: FailureDiagnostic(solution, locator), D(decl), NumParams(numParams),
3091+
NumArgs(numArgs), HasParameterPack(hasParameterPack) {}
3092+
3093+
bool diagnoseAsError() override;
3094+
};
3095+
30723096
} // end namespace constraints
30733097
} // end namespace swift
30743098

lib/Sema/CSFix.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2845,7 +2845,9 @@ AllowConcreteTypeSpecialization::create(ConstraintSystem &cs, Type concreteTy,
28452845

28462846
bool IgnoreGenericSpecializationArityMismatch::diagnose(
28472847
const Solution &solution, bool asNote) const {
2848-
return false;
2848+
InvalidTypeSpecializationArity failure(solution, D, NumParams, NumArgs,
2849+
HasParameterPack, getLocator());
2850+
return failure.diagnose(asNote);
28492851
}
28502852

28512853
IgnoreGenericSpecializationArityMismatch *

0 commit comments

Comments
 (0)