Skip to content

Improve std.container.util.make with forwarding and clearer errors#10973

Open
shhreya13 wants to merge 1 commit intodlang:masterfrom
shhreya13:improve-make-util
Open

Improve std.container.util.make with forwarding and clearer errors#10973
shhreya13 wants to merge 1 commit intodlang:masterfrom
shhreya13:improve-make-util

Conversation

@shhreya13
Copy link

Summary

This PR improves the make template in std.container.util.

Changes

  • Use std.algorithm.mutation.forward when passing constructor arguments to preserve move semantics and avoid unnecessary copies.
  • Add clearer compile-time diagnostics using static assert when a constructor cannot be called with the provided arguments.
  • Improve documentation explaining that make abstracts the difference between struct construction (T(args)) and class construction (new T(args)).
  • Add additional edge-case unit tests for empty containers and single-element input.

Motivation

These changes improve developer experience by providing clearer error messages and slightly improving argument handling while keeping existing behavior unchanged.

Related Issue

Closes #10972

@shhreya13 shhreya13 requested a review from PetarKirov as a code owner March 10, 2026 15:36
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @shhreya13! 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.

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#10973"

Copy link
Contributor

@pbackus pbackus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has clearly had very little effort put into its preparation. Judging from the formatting of the PR message and the content of the submitter's Github account, I suspect that it was generated by an LLM with minimal human oversight.

Given the above, I think it is unlikely that this PR will ever result in any useful contribution to Phobos, but I will leave it open for now to give its human submitter the chance to fix these shortcomings.

Comment on lines +2 to +12
This module contains common utilities used by containers.

This module is a submodule of $(MREF std, container).

Source: $(PHOBOSSRC std/container/util.d)

Copyright: 2010- Andrei Alexandrescu. All rights reserved by the respective holders.
Copyright: 2010- Andrei Alexandrescu.

License: Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at $(HTTP
boost.org/LICENSE_1_0.txt)).

Authors: $(HTTP erdani.com, Andrei Alexandrescu)

$(SCRIPT inhibitQuickIndex = 1;)
Authors: Andrei Alexandrescu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated documentation changes in this PR.

Comment on lines +20 to +37
Creates and returns an initialized instance of type `T`.

This utility abstracts the difference between constructing
structs and classes.

Structs are constructed using:
---
T(args)
---

Classes are constructed using:
---
new T(args)
---

This allows generic container code to construct objects
without worrying about whether `T` is a struct or class.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated documentation changes in this PR.

{
T make(Args...)(Args arguments)
if (is(T == struct) && __traits(compiles, T(arguments)))
@safe T make(Args...)(Args arguments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make is a template function, which means that attributes like @safe are inferred automatically by the compiler. It should not be necessary to add an explicit @safe attribute here.

If there is something in the function body that prevents @safe from being inferred when it ought to be, the body should be changed to make that inference possible.


T make(Args...)(Args arguments)
if (is(T == class) && __traits(compiles, new T(arguments)))
@safe T make(Args...)(Args arguments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: inference of @safe.

* Convenience function for constructing a generic container.
*/
Convenience function for constructing a generic container.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated formatting changes in this PR.

$(SCRIPT inhibitQuickIndex = 1;)
Authors: Andrei Alexandrescu
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated formatting changes in this PR.


auto arr1 = make!(Array!dchar)();
assert(arr1.empty);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated formatting changes in this PR.


auto rtb1 = make!(RedBlackTree!dchar)();
assert(rtb1.empty);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated formatting changes in this PR.

auto d = make!(DList!int)(1,2,3,4,5);
assert(a == b); // this better terminate!

assert(a == b);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not include unrelated formatting changes in this PR.


module std.container.util;

import std.algorithm.mutation : forward;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import std.algorithm.mutation : forward;
import core.lifetime : forward;

The fact that this import is incorrect suggests that you did not even attempt to compile or test this change on your own device before submitting a PR. Please do so in the future; it will save both you and us a lot of time.

@0xEAB 0xEAB added the Review:AI Generated Code that is generated by an LLM AI — or suspected to be. label Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review:AI Generated Code that is generated by an LLM AI — or suspected to be.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve std.container.util.make with forwarding and clearer compile-time errors

4 participants