Skip to content

Collapse ivalue::from's tag dispatch into one function - #195105

Closed
cyyever wants to merge 1 commit into
pytorch:mainfrom
cyyever:cleanup/ivalue-from-if-constexpr
Closed

cyyever wants to merge 1 commit into
pytorch:mainfrom
cyyever:cleanup/ivalue-from-if-constexpr

Conversation

@cyyever

@cyyever cyyever commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

detail::from_ had three overloads selected by an is_constructible tag. The unreachable one (intrusive_ptr + false_type) is more specialized than the catch-all, so it shadows the diagnostic the catch-all exists to produce: calling from with an intrusive_ptr to a non-custom class reports an ambiguous-constructor error instead of the intended static_assert message.

Folding the three into one if constexpr in from() drops the tag and the detail namespace, and lets the static_assert fire where it was meant to.

detail::from_ had three overloads selected by an is_constructible tag. Two of
them are reachable and one is not, but the unreachable one is not merely dead:
being more specialized than the catch-all, it shadows the diagnostic the
catch-all exists to produce.

IValue is constructible from intrusive_ptr<T> whenever T derives from
CustomClassHolder, so custom classes take the true_type overload. What reaches
the intrusive_ptr overload is an intrusive_ptr whose pointee is not a custom
class, and IValue(std::move(x)) does not compile for that either. The result is
that the one case the overload was added for -- the 2019 torchbind prototype,
before the custom-class constructor existed -- now reports

  error: call of overloaded 'IValue(...)' is ambiguous

from inside ivalue_inl.h, instead of the message written for it:

  You are calling from with a type that it doesn't support, and isn't a
  potential custom class (ie: is an intrusive_ptr)

Folding the three into one if constexpr in from() drops the tag, drops the
detail namespace, and lets the static_assert fire where it was meant to.
guts::false_t stays: static_assert(false) in a discarded branch is only
well-formed from C++23, and this builds at C++20.

Test Plan:

```
# custom class and plain scalars still convert
static_assert(std::is_constructible_v<IValue, intrusive_ptr<CustomCls>>);
c10::ivalue::from(c10::make_intrusive<CustomCls>());
c10::ivalue::from(std::string("x"));
c10::ivalue::from(at::Tensor());

# an intrusive_ptr to a non-custom class now names the reason
c10::ivalue::from(c10::make_intrusive<PlainIntrusiveTarget>());
```

Compiled with g++ -std=gnu++20 -fsyntax-only against ivalue.h,
custom_class.h and make_boxed_from_unboxed_functor.h, which cover all three
in-tree callers. The supported conversions compile unchanged; the unsupported
one changes from the ambiguous-constructor error to the static_assert.
@pytorch-bot

pytorch-bot Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195105

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3e11fb8 with merge base 3d0205b (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 28, 2026
@Skylion007 Skylion007 added topic: not user facing topic category ciflow/trunk Trigger trunk jobs on your pull request and removed ciflow/trunk Trigger trunk jobs on your pull request labels Aug 28, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@cyyever
cyyever deleted the cleanup/ivalue-from-if-constexpr branch August 28, 2026 23:33
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
detail::from_ had three overloads selected by an is_constructible tag. The unreachable one (intrusive_ptr<T> + false_type) is more specialized than the catch-all, so it shadows the diagnostic the catch-all exists to produce: calling from with an intrusive_ptr to a non-custom class reports an ambiguous-constructor error instead of the intended static_assert message.

Folding the three into one if constexpr in from() drops the tag and the detail namespace, and lets the static_assert fire where it was meant to.
Pull Request resolved: pytorch#195105
Approved by: https://github.com/Skylion007
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
detail::from_ had three overloads selected by an is_constructible tag. The unreachable one (intrusive_ptr<T> + false_type) is more specialized than the catch-all, so it shadows the diagnostic the catch-all exists to produce: calling from with an intrusive_ptr to a non-custom class reports an ambiguous-constructor error instead of the intended static_assert message.

Folding the three into one if constexpr in from() drops the tag and the detail namespace, and lets the static_assert fire where it was meant to.
Pull Request resolved: pytorch#195105
Approved by: https://github.com/Skylion007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants