@@ -1903,9 +1903,9 @@ namespace {
1903
1903
// / introduce them as an explicit generic arguments constraint.
1904
1904
// /
1905
1905
// / \returns true if resolving any of the specialization types failed.
1906
- void addSpecializationConstraint (ConstraintLocator *locator, Type boundType,
1907
- SourceLoc lAngleLoc ,
1908
- ArrayRef<TypeRepr *> specializationArgs) {
1906
+ bool addSpecializationConstraint (
1907
+ ConstraintLocator *locator, Type boundType ,
1908
+ ArrayRef<TypeRepr *> specializationArgs) {
1909
1909
// Resolve each type.
1910
1910
SmallVector<Type, 2 > specializationArgTypes;
1911
1911
auto options =
@@ -1916,36 +1916,61 @@ namespace {
1916
1916
options |= TypeResolutionFlags::AllowPackReferences;
1917
1917
elementEnv = OuterExpansions.back ();
1918
1918
}
1919
- auto result = TypeResolution::resolveContextualType (
1919
+ const auto result = TypeResolution::resolveContextualType (
1920
1920
specializationArg, CurDC, options,
1921
1921
// Introduce type variables for unbound generics.
1922
1922
OpenUnboundGenericType (CS, locator),
1923
1923
HandlePlaceholderType (CS, locator),
1924
1924
OpenPackElementType (CS, locator, elementEnv));
1925
- if (result->hasError ()) {
1926
- auto &ctxt = CS.getASTContext ();
1927
- auto *repr = new (ctxt) PlaceholderTypeRepr (specializationArg->getLoc ());
1928
- result = PlaceholderType::get (ctxt, repr);
1929
- ctxt.Diags .diagnose (lAngleLoc,
1930
- diag::while_parsing_as_left_angle_bracket);
1931
- }
1925
+ if (result->hasError ())
1926
+ return true ;
1927
+
1932
1928
specializationArgTypes.push_back (result);
1933
1929
}
1934
1930
1935
- auto constraint = Constraint::create (
1936
- CS, ConstraintKind::ExplicitGenericArguments, boundType,
1937
- PackType::get (CS.getASTContext (), specializationArgTypes), locator);
1938
- CS. addUnsolvedConstraint (constraint );
1939
- CS. activateConstraint (constraint) ;
1931
+ CS. addConstraint (
1932
+ ConstraintKind::ExplicitGenericArguments, boundType,
1933
+ PackType::get (CS.getASTContext (), specializationArgTypes),
1934
+ locator );
1935
+ return false ;
1940
1936
}
1941
1937
1942
1938
Type visitUnresolvedSpecializeExpr (UnresolvedSpecializeExpr *expr) {
1943
1939
auto baseTy = CS.getType (expr->getSubExpr ());
1944
- auto *overloadLocator = CS.getConstraintLocator (expr->getSubExpr ());
1945
- addSpecializationConstraint (
1946
- overloadLocator, baseTy->getMetatypeInstanceType (),
1947
- expr->getLAngleLoc (), expr->getUnresolvedParams ());
1948
- return baseTy;
1940
+
1941
+ if (baseTy->isTypeVariableOrMember ()) {
1942
+ return baseTy;
1943
+ }
1944
+
1945
+ // We currently only support explicit specialization of generic types.
1946
+ // FIXME: We could support explicit function specialization.
1947
+ auto &de = CS.getASTContext ().Diags ;
1948
+ if (baseTy->is <AnyFunctionType>()) {
1949
+ de.diagnose (expr->getSubExpr ()->getLoc (),
1950
+ diag::cannot_explicitly_specialize_generic_function);
1951
+ de.diagnose (expr->getLAngleLoc (),
1952
+ diag::while_parsing_as_left_angle_bracket);
1953
+ return Type ();
1954
+ }
1955
+
1956
+ if (AnyMetatypeType *meta = baseTy->getAs <AnyMetatypeType>()) {
1957
+ auto *overloadLocator = CS.getConstraintLocator (expr->getSubExpr ());
1958
+ if (addSpecializationConstraint (overloadLocator,
1959
+ meta->getInstanceType (),
1960
+ expr->getUnresolvedParams ())) {
1961
+ return Type ();
1962
+ }
1963
+
1964
+ return baseTy;
1965
+ }
1966
+
1967
+ // FIXME: If the base type is a type variable, constrain it to a metatype
1968
+ // of a bound generic type.
1969
+ de.diagnose (expr->getSubExpr ()->getLoc (),
1970
+ diag::not_a_generic_definition);
1971
+ de.diagnose (expr->getLAngleLoc (),
1972
+ diag::while_parsing_as_left_angle_bracket);
1973
+ return Type ();
1949
1974
}
1950
1975
1951
1976
Type visitSequenceExpr (SequenceExpr *expr) {
@@ -4055,9 +4080,10 @@ namespace {
4055
4080
4056
4081
// Add explicit generic arguments, if there were any.
4057
4082
if (expr->getGenericArgsRange ().isValid ()) {
4058
- addSpecializationConstraint (CS.getConstraintLocator (expr), macroRefType,
4059
- expr->getGenericArgsRange ().Start ,
4060
- expr->getGenericArgs ());
4083
+ if (addSpecializationConstraint (
4084
+ CS.getConstraintLocator (expr), macroRefType,
4085
+ expr->getGenericArgs ()))
4086
+ return Type ();
4061
4087
}
4062
4088
4063
4089
// Form the applicable-function constraint. The result type
0 commit comments