Skip to content

Commit

Permalink
[eclipse-cdt#457] Fix partial specialisation matching for deduced pri…
Browse files Browse the repository at this point in the history
…mitive types

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.
  • Loading branch information
davmac314 authored and jonahgraham committed Dec 28, 2023
1 parent a7bfdd2 commit 74d1288
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6865,6 +6865,37 @@ public void testSfinae_b() throws Exception {
parseAndCheckBindings();
}

// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1614,19 +1614,23 @@ public static IType instantiateType(final IType type, InstantiationContext conte
}
}
} 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;
}
Expand Down Expand Up @@ -3115,29 +3119,63 @@ public static boolean containsDependentArg(ObjectMap tpMap) {

/**
* 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,11 +1044,13 @@ private boolean fromType(IType p, IType a, boolean allowCVQConversion, boolean v

// 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);
}
Expand Down

0 comments on commit 74d1288

Please sign in to comment.