@@ -775,6 +775,40 @@ Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
775775 TemplateArgs);
776776}
777777
778+ ExprResult Sema::BuildSubstNonTypeTemplateParmExpr (
779+ Decl *AssociatedDecl, const NonTypeTemplateParmDecl *NTTP,
780+ SourceLocation Loc, TemplateArgument Arg, UnsignedOrNone PackIndex,
781+ bool Final) {
782+ // The template argument itself might be an expression, in which case we just
783+ // return that expression. This happens when substituting into an alias
784+ // template.
785+ Expr *Replacement;
786+ bool refParam = true ;
787+ if (Arg.getKind () == TemplateArgument::Expression) {
788+ Replacement = Arg.getAsExpr ();
789+ refParam = Replacement->isLValue ();
790+ if (refParam && Replacement->getType ()->isRecordType ()) {
791+ QualType ParamType =
792+ NTTP->isExpandedParameterPack ()
793+ ? NTTP->getExpansionType (*SemaRef.ArgPackSubstIndex )
794+ : NTTP->getType ();
795+ if (const auto *PET = dyn_cast<PackExpansionType>(ParamType))
796+ ParamType = PET->getPattern ();
797+ refParam = ParamType->isReferenceType ();
798+ }
799+ } else {
800+ ExprResult result =
801+ SemaRef.BuildExpressionFromNonTypeTemplateArgument (Arg, Loc);
802+ if (result.isInvalid ())
803+ return ExprError ();
804+ Replacement = result.get ();
805+ refParam = Arg.getNonTypeTemplateArgumentType ()->isReferenceType ();
806+ }
807+ return new (SemaRef.Context ) SubstNonTypeTemplateParmExpr (
808+ Replacement->getType (), Replacement->getValueKind (), Loc, Replacement,
809+ AssociatedDecl, NTTP->getIndex (), PackIndex, refParam, Final);
810+ }
811+
778812bool Sema::DiagnoseUninstantiableTemplate (SourceLocation PointOfInstantiation,
779813 NamedDecl *Instantiation,
780814 bool InstantiatedFromMember,
@@ -7068,22 +7102,8 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
70687102
70697103 // If the parameter type somehow involves auto, deduce the type now.
70707104 DeducedType *DeducedT = ParamType->getContainedDeducedType ();
7071- if (getLangOpts ().CPlusPlus17 && DeducedT && !DeducedT->isDeduced ()) {
7072- // During template argument deduction, we allow 'decltype(auto)' to
7073- // match an arbitrary dependent argument.
7074- // FIXME: The language rules don't say what happens in this case.
7075- // FIXME: We get an opaque dependent type out of decltype(auto) if the
7076- // expression is merely instantiation-dependent; is this enough?
7077- if (DeductionArg->isTypeDependent ()) {
7078- auto *AT = dyn_cast<AutoType>(DeducedT);
7079- if (AT && AT->isDecltypeAuto ()) {
7080- SugaredConverted = TemplateArgument (Arg, /* IsCanonical=*/ false );
7081- CanonicalConverted = TemplateArgument (
7082- Context.getCanonicalTemplateArgument (SugaredConverted));
7083- return Arg;
7084- }
7085- }
7086-
7105+ bool IsDeduced = DeducedT && !DeducedT->isDeduced ();
7106+ if (IsDeduced) {
70877107 // When checking a deduced template argument, deduce from its type even if
70887108 // the type is dependent, in order to check the types of non-type template
70897109 // arguments line up properly in partial ordering.
@@ -7112,17 +7132,21 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
71127132 // along with the other associated constraints after
71137133 // checking the template argument list.
71147134 /* IgnoreConstraints=*/ true );
7115- if (Result == TemplateDeductionResult::AlreadyDiagnosed) {
7116- return ExprError ();
7117- } else if (Result != TemplateDeductionResult::Success) {
7118- if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
7119- Diag (Arg->getExprLoc (),
7120- diag::err_non_type_template_parm_type_deduction_failure)
7121- << Param->getDeclName () << NTTP->getType () << Arg->getType ()
7122- << Arg->getSourceRange ();
7135+ if (Result != TemplateDeductionResult::Success) {
7136+ ParamType = TSI->getType ();
7137+ if (StrictCheck || !DeductionArg->isTypeDependent ()) {
7138+ if (Result == TemplateDeductionResult::AlreadyDiagnosed)
7139+ return ExprError ();
7140+ if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param))
7141+ Diag (Arg->getExprLoc (),
7142+ diag::err_non_type_template_parm_type_deduction_failure)
7143+ << Param->getDeclName () << NTTP->getType () << Arg->getType ()
7144+ << Arg->getSourceRange ();
7145+ NoteTemplateParameterLocation (*Param);
7146+ return ExprError ();
71237147 }
7124- NoteTemplateParameterLocation (*Param );
7125- return ExprError ( );
7148+ ParamType = SubstAutoTypeDependent (ParamType );
7149+ assert (!ParamType. isNull () && " substituting DependentTy can't fail " );
71267150 }
71277151 }
71287152 // CheckNonTypeTemplateParameterType will produce a diagnostic if there's
@@ -7144,14 +7168,16 @@ ExprResult Sema::CheckTemplateArgument(NamedDecl *Param, QualType ParamType,
71447168 // type-dependent, there's nothing we can check now.
71457169 if (ParamType->isDependentType () || DeductionArg->isTypeDependent ()) {
71467170 // Force the argument to the type of the parameter to maintain invariants.
7147- ExprResult E = ImpCastExprToType (
7148- DeductionArg, ParamType.getNonLValueExprType (Context), CK_Dependent,
7149- ParamType->isLValueReferenceType () ? VK_LValue
7150- : ParamType->isRValueReferenceType () ? VK_XValue
7151- : VK_PRValue);
7152- if (E.isInvalid ())
7153- return ExprError ();
7154- setDeductionArg (E.get ());
7171+ if (!IsDeduced) {
7172+ ExprResult E = ImpCastExprToType (
7173+ DeductionArg, ParamType.getNonLValueExprType (Context), CK_Dependent,
7174+ ParamType->isLValueReferenceType () ? VK_LValue
7175+ : ParamType->isRValueReferenceType () ? VK_XValue
7176+ : VK_PRValue);
7177+ if (E.isInvalid ())
7178+ return ExprError ();
7179+ setDeductionArg (E.get ());
7180+ }
71557181 SugaredConverted = TemplateArgument (Arg, /* IsCanonical=*/ false );
71567182 CanonicalConverted = TemplateArgument (
71577183 Context.getCanonicalTemplateArgument (SugaredConverted));
@@ -8555,6 +8581,7 @@ static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) {
85558581static bool CheckNonTypeTemplatePartialSpecializationArgs (
85568582 Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param,
85578583 const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) {
8584+ bool HasError = false ;
85588585 for (unsigned I = 0 ; I != NumArgs; ++I) {
85598586 if (Args[I].getKind () == TemplateArgument::Pack) {
85608587 if (CheckNonTypeTemplatePartialSpecializationArgs (
@@ -8569,6 +8596,10 @@ static bool CheckNonTypeTemplatePartialSpecializationArgs(
85698596 continue ;
85708597
85718598 Expr *ArgExpr = Args[I].getAsExpr ();
8599+ if (ArgExpr->containsErrors ()) {
8600+ HasError = true ;
8601+ continue ;
8602+ }
85728603
85738604 // We can have a pack expansion of any of the bullets below.
85748605 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
@@ -8638,7 +8669,7 @@ static bool CheckNonTypeTemplatePartialSpecializationArgs(
86388669 }
86398670 }
86408671
8641- return false ;
8672+ return HasError ;
86428673}
86438674
86448675bool Sema::CheckTemplatePartialSpecializationArgs (
0 commit comments