Index: lib/Sema/SemaInit.cpp =================================================================== --- lib/Sema/SemaInit.cpp +++ lib/Sema/SemaInit.cpp @@ -4672,17 +4672,26 @@ // conversion to allow creating temporaries in the alloca address space. auto AS1 = T1Quals.getAddressSpace(); auto AS2 = T2Quals.getAddressSpace(); - T1Quals.removeAddressSpace(); - T2Quals.removeAddressSpace(); + if (AS1 != AS2) { + T1Quals.removeAddressSpace(); + T2Quals.removeAddressSpace(); + } QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1Quals); if (T1Quals != T2Quals) Sequence.AddQualificationConversionStep(cv1T4, ValueKind); Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); ValueKind = isLValueRef ? VK_LValue : VK_XValue; if (AS1 != AS2) { - T1Quals.addAddressSpace(AS1); - QualType cv1AST4 = S.Context.getQualifiedType(cv2T2, T1Quals); - Sequence.AddQualificationConversionStep(cv1AST4, ValueKind); + if (AS1 != LangAS::Default) + T1Quals.addAddressSpace(AS1); + // If there has been an addr space qualifier on cv2T2 we will fail + // to create a new type. To avoid this we re-create the type ignoring + // addr spaces first. + auto IgnoreAScv2T2 = S.Context.getQualifiedType(T2, T2Quals); + if (AS2 != LangAS::Default) + T2Quals.addAddressSpace(AS2); + QualType WithAScv1T4 = S.Context.getQualifiedType(IgnoreAScv2T2, T1Quals); + Sequence.AddQualificationConversionStep(WithAScv1T4, ValueKind); } // In any case, the reference is bound to the resulting glvalue (or to Index: test/SemaOpenCLCXX/address-space-of-this.cl =================================================================== --- /dev/null +++ test/SemaOpenCLCXX/address-space-of-this.cl @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 %s -triple spir-unknown-unknown -cl-std=c++ -pedantic -verify -fsyntax-only +// expected-no-diagnostics + +// Extract from PR38614 +struct C {}; + +C f1() { + return C{}; +} + +C f2(){ + C c; + return c; +}