1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

[#457] Fix partial specialisation matching for deduced primitive types (#458)

Current partial specialisation selection matches deduced type by
IBinding, not by IType, meaning that it can't work for types (such as
primitive types) that aren't represented by an IBinding. Fix that by
wrapping the result of resolution in a type which can handle either.
This commit is contained in:
Davin McCall 2023-12-29 09:07:49 +10:00 committed by GitHub
parent f6481742dd
commit 232b24cd80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 22 deletions

View file

@ -6903,6 +6903,37 @@ public class AST2TemplateTests extends AST2CPPTestBase {
assertTrue(varIp.getType().isSameType(varJp.getType()));
}
// template <typename A, typename B = typename A::t1>
// struct C {
// };
//
// template <typename A>
// struct C<A, decltype(typename A::t1())> {
// int ccc1;
// using t = int;
// };
//
// template <typename A>
// struct C<A, decltype(typename A::t2())> {
// int ccc2;
// };
//
// struct marker {
// using t1 = int;
// using t2 = long;
// };
//
// int b1 = C<marker>().ccc1;
// C<marker>::t b2;
public void testSfinae_d() throws Exception {
BindingAssertionHelper bh = getAssertionHelper();
IVariable varB1 = bh.assertNonProblem("b1");
IVariable varB2 = bh.assertNonProblem("b2");
assertFalse(varB1.getInitialValue() instanceof IProblemBinding);
assertTrue(varB1.getType().isSameType(varB2.getType()));
}
// template<typename T>
// struct is_pod {
// static const bool value = __is_pod(T);

View file

@ -1614,19 +1614,23 @@ public class CPPTemplates {
}
}
} else if (type instanceof TypeOfUnknownMember) {
IBinding binding = resolveUnknown(((TypeOfUnknownMember) type).getUnknownMember(), context);
if (binding instanceof IType) {
return (IType) binding;
} else if (binding instanceof IVariable) {
return ((IVariable) binding).getType();
} else if (binding instanceof IFunction) {
return ((IFunction) binding).getType();
BindingOrType bindingType = resolveUnknownBindingOrType(
((TypeOfUnknownMember) type).getUnknownMember(), context);
if (bindingType.getType() != null) {
return bindingType.getType();
} else {
IBinding binding = bindingType.getBinding();
if (binding instanceof IVariable) {
return ((IVariable) binding).getType();
} else if (binding instanceof IFunction) {
return ((IFunction) binding).getType();
}
}
return type;
} else {
IBinding binding = resolveUnknown((ICPPUnknownBinding) type, context);
if (binding instanceof IType)
return (IType) binding;
BindingOrType bindingType = resolveUnknownBindingOrType((ICPPUnknownBinding) type, context);
if (bindingType.getType() != null)
return bindingType.getType();
return type;
}
@ -3116,29 +3120,63 @@ public class CPPTemplates {
/**
* Attempts to (partially) resolve an unknown binding with the given arguments.
* Cannot resolved types that are not representable as a binding.
*/
public static IBinding resolveUnknown(ICPPUnknownBinding unknown, InstantiationContext context)
throws DOMException {
BindingOrType bindingType = resolveUnknownBindingOrType(unknown, context);
if (bindingType.getBinding() != null)
return bindingType.getBinding();
return unknown;
}
public static class BindingOrType {
Object bindingOrType;
BindingOrType(IBinding binding) {
bindingOrType = binding;
}
BindingOrType(IType type) {
bindingOrType = type;
}
IType getType() {
if (bindingOrType instanceof IType)
return (IType) bindingOrType;
return null;
}
IBinding getBinding() {
if (bindingOrType instanceof IBinding)
return (IBinding) bindingOrType;
return null;
}
}
/**
* Attempts to (partially) resolve an unknown binding with the given arguments.
*/
public static BindingOrType resolveUnknownBindingOrType(ICPPUnknownBinding unknown, InstantiationContext context)
throws DOMException {
if (unknown instanceof ICPPDeferredClassInstance) {
return resolveDeferredClassInstance((ICPPDeferredClassInstance) unknown, context);
return new BindingOrType(resolveDeferredClassInstance((ICPPDeferredClassInstance) unknown, context));
}
if (unknown instanceof ICPPDeferredVariableInstance) {
return resolveDeferredVariableInstance((ICPPDeferredVariableInstance) unknown, context);
return new BindingOrType(resolveDeferredVariableInstance((ICPPDeferredVariableInstance) unknown, context));
}
if (unknown instanceof ICPPUnknownMember) {
return resolveUnknownMember((ICPPUnknownMember) unknown, context);
return new BindingOrType(resolveUnknownMember((ICPPUnknownMember) unknown, context));
}
if (unknown instanceof ICPPTemplateParameter && unknown instanceof IType) {
IType type = resolveTemplateTypeParameter((ICPPTemplateParameter) unknown, context);
if (type instanceof IBinding)
return (IBinding) type;
return new BindingOrType(type);
}
if (unknown instanceof TypeOfDependentExpression) {
IType type = instantiateType((IType) unknown, context);
if (type instanceof IBinding)
return (IBinding) type;
return new BindingOrType(type);
}
return unknown;
return new BindingOrType(unknown);
}
private static IBinding resolveUnknownMember(ICPPUnknownMember unknown, InstantiationContext context)

View file

@ -1044,11 +1044,13 @@ public class TemplateArgumentDeduction {
// Verify that the resolved binding matches the argument type.
InstantiationContext context = InstantiationContext.forDeduction(fDeducedArgs);
IBinding binding = CPPTemplates.resolveUnknown((ICPPUnknownBinding) p, context);
if (binding instanceof ICPPUnknownBinding)
CPPTemplates.BindingOrType bindingType = CPPTemplates
.resolveUnknownBindingOrType((ICPPUnknownBinding) p, context);
if (bindingType.getBinding() instanceof ICPPUnknownBinding)
return true; // An unknown type may match anything.
return binding instanceof IType && ((IType) binding).isSameType(a);
if (bindingType.getType() != null) {
return bindingType.getType().isSameType(a);
}
} else {
return p.isSameType(a);
}