Skip to content

Conversation

@Inkrementator
Copy link
Contributor

@Inkrementator Inkrementator commented Jan 8, 2026

variant.get!Variant used to segfault, but after PR #10697 it only threw an exception.

The reason for that was that get!Variant called the handler with OpID.get. Handler is a function pointer to a templated function parametrized on the type currently wrapped by Variant. Because Variant generally get flattened, so Variant(Variant(4)) just wraps the value 4, instead of Variant(4), the wrapped type (should generally) not be "Variant", and thus the OpID.get operation will fail.

This couldn't be fixed in the handler, because OpID.get uses typeinfo for the target type, but VariantN is a template, so afaik we can't really test it against the infinite set of valid VariantN instantiations. get still gets the target type as a template parameter, so we can handle the problem there.

Supporting get!Variant incidentally also fixed #10518. Adding variant inside an associative array of variants failed:

Variant variant = Variant([
    "one": Variant(1),
  ]);

  variant["four"] = Variant(4);

This was because opIndexAssign wrapps the index as well as the value you want to assign in a Variant again.

Variant opIndexAssign(T, N)(T value, N i)
{
    Variant[2] args = [ Variant(value), Variant(i) ];
    fptr(OpID.indexAssign, &store, &args) == 0 || assert(false);
    return args[0];
}

The handler of variant containing the AA is parametrized with Variant[string], but opIndexAssign passes a variant of type int instead of Variant(Variant(int)) due to the aformentioned flattening.

Eventually in the handler, it ends up calling Variant(i).get!Variant, which crashed.

For the github automation:

Fixes #10431
Fixes #10518
Fixes #9980

variant.get!Variant used to segfault, but after PR dlang#10697 it only threw
an exception.

The reason for that was that get!Variant called the `handler` with
OpID.get. Handler is a function pointer to a templated function
parametrized on the type currently wrapped by Variant. Because Variant
generally get flattened, so `Variant(Variant(4))` just wraps the value
`4`, instead of `Variant(4)`, the wrapped type (should generally) not
be "Variant", and thus the OpID.get operation will fail.

This couldn't be fixed in the handler, because OpID.get uses
typeinfo for the target type, but VariantN is a template, so afaik we
can't really test it against the infinite set of valid VariantN
instantiations. `get` still gets the target type as a template
parameter, so we can handle the problem there.

Supporting get!Variant incidentally also fixed dlang#10518. Adding variant
inside an associative array of variants failed:
```
Variant variant = Variant([
    "one": Variant(1),
  ]);

  variant["four"] = Variant(4);
```
This was because `opIndexAssign` wrapps the index as well as the value
you want to assign in a Variant again.
```
Variant opIndexAssign(T, N)(T value, N i)
{
    Variant[2] args = [ Variant(value), Variant(i) ];
    fptr(OpID.indexAssign, &store, &args) == 0 || assert(false);
    return args[0];
}
```
The handler of `variant` containing the AA is parametrized with
`Variant[string]`, but opIndexAssign passes a variant of type int
instead of Variant(Variant(int)) due to the aformentioned flattening.

Eventually in the handler, it ends up calling `Variant(i).get!Variant`,
which crashed.

For the github automation:

Fixes dlang#10431
Fixes dlang#10518
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Inkrementator! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10923"

This bug also depended on get!Variant working.

Fixes dlang#9980
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding to associative array inside Variant segfaults Variant.get!Variant segfault Variant[] within Variant[] can be accessed, but not assigned to

2 participants