Skip to content

Allow implicit string literal -> const(wchar_t)[]#8950

Merged
thewilsonator merged 1 commit intodlang:masterfrom
thewilsonator:string-const-wchar_t-array
Nov 15, 2018
Merged

Allow implicit string literal -> const(wchar_t)[]#8950
thewilsonator merged 1 commit intodlang:masterfrom
thewilsonator:string-const-wchar_t-array

Conversation

@thewilsonator
Copy link
Copy Markdown
Contributor

for druntime#2349

@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request and interest in making D better, @thewilsonator! 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 annotated coverage diff directly on GitHub with CodeCov's browser extension
  • 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 fetch digger
dub run digger -- build "master + dmd#8950"

@thewilsonator
Copy link
Copy Markdown
Contributor Author

cc @rainers is this going to do what I think it does?

@nordlow
Copy link
Copy Markdown
Contributor

nordlow commented Nov 13, 2018

Tests?

Comment thread src/dmd/mtype.d Outdated
@Geod24
Copy link
Copy Markdown
Member

Geod24 commented Nov 13, 2018

Could you provide some context ? I'm not sure I follow the reasoning.

@thewilsonator
Copy link
Copy Markdown
Contributor Author

enum foo : wchar { whatever }

immutable(foo) [] a = "something"; // fails

enum __c_wchar_t : wchar;
immutable(__c_wchar_t)[] b = "something"; // fails but as __c_wchar_t is magic, it should pass

__c_wchar_t is rather a lot less useful without this.

Also where are implicit conversions tested in the test suite?

Copy link
Copy Markdown
Contributor

@jacob-carlborg jacob-carlborg left a comment

Choose a reason for hiding this comment

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

  • Needs to have tests
  • Needs to have a changelog entry
  • Needs to have a spec update

@thewilsonator
Copy link
Copy Markdown
Contributor Author

thewilsonator commented Nov 13, 2018

Needs to have a changelog entry
Needs to have a spec update

dlang/druntime#2349 Hmm, actually might be better to have it in DMD and fold the changelog for #8342 into it.

@thewilsonator thewilsonator force-pushed the string-const-wchar_t-array branch 5 times, most recently from b2a5710 to f2bfb1c Compare November 13, 2018 11:27
@thewilsonator
Copy link
Copy Markdown
Contributor Author

How is the implicit conversion from e.g. string to const(char)* for string literals done? I can't seem to see anything in mtype.d.

Geod24
Geod24 previously requested changes Nov 13, 2018
Copy link
Copy Markdown
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

It's not that simple. Check the following snippet:

import core.stdc.stdio;
import core.stdc.string;
import core.stdc.errno;

import std.conv;
import std.stdio;

extern(C) int wprintf(const __c_wchar_t* format, ...);

enum __c_wchar_t : dchar; // Use `wchar` if you are on Windows

static immutable string foo = "Hello World   !\n";

void main ()
{
    immutable(__c_wchar_t)[] str = foo;
    if (-1 == wprintf(str.ptr))
        writeln("Error: ", strerror(errno).to!string);
}

This prints `Error: Illegal byte sequence" on my machine (OSX).
It's because this implicit conv is just blindly doing an array cast in this case. Removing one of the character will yield an "array cast misalignment" assert error.

There is no implicit conversion between {w,d}string and string, it's just that "foo" is not typed as string (but ""c is).

Side note: testing with literals is never enough.

@thewilsonator
Copy link
Copy Markdown
Contributor Author

Ahh, right. Where is the string literal specific stuff handled? Not mtype.d apparently.

@thewilsonator thewilsonator force-pushed the string-const-wchar_t-array branch from f2bfb1c to 49518e2 Compare November 14, 2018 06:28
@thewilsonator thewilsonator dismissed stale reviews from Geod24 and jacob-carlborg November 14, 2018 06:29

Added tests for non-literal conversion failure

@thewilsonator thewilsonator force-pushed the string-const-wchar_t-array branch from 1880231 to 819e11c Compare November 14, 2018 07:51
@thewilsonator thewilsonator changed the title Allow implicit string -> const(wchar_t)[] Allow implicit string literal -> const(wchar_t)[] Nov 14, 2018
@thewilsonator thewilsonator merged commit 9814644 into dlang:master Nov 15, 2018
@ibuclaw
Copy link
Copy Markdown
Member

ibuclaw commented Nov 17, 2018

I see no one approving this, why was it merged?

Comment thread src/dmd/dcast.d
*/
if (TypeBasic tob = tn.toBasetype().isTypeBasic())
result = tn.implicitConvTo(tob);
return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indentation here looks off. I can't tell whether the early return here should be part of the first or second condition.

@ghost
Copy link
Copy Markdown

ghost commented Mar 17, 2019

Change brought by #9460 would break the test added in the PR. Have a look if you have some time to @thewilsonator .

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants