Bug 23385 – Consider making currTime @nogc and nothrow

Status
RESOLVED
Resolution
LATER
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-10-04T14:49:56Z
Last change time
2022-10-06T02:57:16Z
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2022-10-04T14:49:56Z
std.datetime.systime is an excellent abstraction over the OS-provided timers. And yet we cannot check the current time in @nogc nothrow code. With just a few changes we can make it @nogc, but it will probably take a bit more care than that. ----- diff --git a/std/datetime/systime.d b/std/datetime/systime.d index fcb8184..0fb7b82 100755 --- a/std/datetime/systime.d +++ b/std/datetime/systime.d @@ -530,7 +530,7 @@ public: given $(REF DateTime,std,datetime,date) is assumed to be in the given time zone. +/ - this(DateTime dateTime, return scope immutable TimeZone tz = null) return scope @safe nothrow + this(DateTime dateTime, return scope immutable TimeZone tz = null) return scope @safe nothrow @nogc { try this(dateTime, Duration.zero, tz); @@ -581,7 +581,7 @@ public: $(REF DateTimeException,std,datetime,date) if `fracSecs` is negative or if it's greater than or equal to one second. +/ - this(DateTime dateTime, Duration fracSecs, return scope immutable TimeZone tz = null) return scope @safe + this(DateTime dateTime, Duration fracSecs, return scope immutable TimeZone tz = null) return scope @safe @nogc { enforce(fracSecs >= Duration.zero, new DateTimeException("A SysTime cannot have negative fractional seconds.")); enforce(fracSecs < seconds(1), new DateTimeException("Fractional seconds must be less than one second.")); @@ -638,7 +638,7 @@ public: given $(REF Date,std,datetime,date) is assumed to be in the given time zone. +/ - this(Date date, return scope immutable TimeZone tz = null) return scope @safe nothrow + this(Date date, return scope immutable TimeZone tz = null) return scope @safe nothrow @nogc { _timezone = tz is null ? LocalTime() : tz; @@ -691,7 +691,7 @@ public: $(LREF SysTime). If null, $(REF LocalTime,std,datetime,timezone) will be used. +/ - this(long stdTime, return scope immutable TimeZone tz = null) return scope @safe pure nothrow + this(long stdTime, return scope immutable TimeZone tz = null) return scope @safe pure nothrow @nogc { _stdTime = stdTime; _timezone = tz is null ? LocalTime() : tz; @@ -10101,15 +10101,14 @@ else version (Windows) private enum hnsecsFrom1601 = 504_911_232_000_000_000L; - long FILETIMEToStdTime(scope const FILETIME* ft) @safe + long FILETIMEToStdTime(scope const FILETIME* ft) @safe @nogc { ULARGE_INTEGER ul; ul.HighPart = ft.dwHighDateTime; ul.LowPart = ft.dwLowDateTime; ulong tempHNSecs = ul.QuadPart; - if (tempHNSecs > long.max - hnsecsFrom1601) - throw new DateTimeException("The given FILETIME cannot be represented as a stdTime value."); + assert(!(tempHNSecs > long.max - hnsecsFrom1601)); return cast(long) tempHNSecs + hnsecsFrom1601; } ----- For now we have to resort to hacks like https://p0nce.github.io/d-idioms/#Bypassing-@nogc
Comment #1 by andrej.mitrovich — 2022-10-04T14:51:52Z
This is probably impossible to fix since various platforms have different types of errors and some should be recoverable. Honestly D really needs an escape hatch from using exceptions in Phobos.
Comment #2 by razvan.nitu1305 — 2022-10-06T02:57:16Z
Phobos has been striving for years to be @nogc. This is an ongoing process, so I don't really see the this bug report as adding any value. I will close this as LATER since attaining a @nogc phobos is an ongoing concern.