Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.safe

Checks whether member access or array casting is allowed in @safe code.

Specification Function Safety

Authors:

Source safe.d

bool checkUnsafeAccess(Scope* sc, Expression e, bool readonly, bool printmsg);
Check for unsafe access in @safe code:
  1. read overlapped pointers
  2. write misaligned pointers
  3. write overlapped storage classes
Print error if unsafe.
Parameters:
Scope* sc scope
Expression e expression to check
bool readonly if access is read-only
bool printmsg print error message if true
Returns:
true if error
bool isSafeCast(Expression e, Type tfrom, Type tto, ref string msg);
Determine if it is @safe to cast e from tfrom to tto.
Parameters:
Expression e expression to be cast
Type tfrom type of e
Type tto type to cast e to
string msg reason why cast is unsafe or deprecated
Returns:
true if @safe or deprecated
bool checkUnsafeDotExp(Scope* sc, Expression e, Identifier id, int flag);
Check for unsafe use of .ptr or .funcptr
Parameters:
Scope* sc context
Expression e expression for error messages
Identifier id ptr or funcptr
int flag DotExpFlag
Returns:
true if error
bool isSaferD(FuncDeclaration fd);
Safer D adds safety checks to functions with the default trust setting.
void reportSafeError(FuncDeclaration fd, bool gag, Loc loc, const(char)* format, RootObject[] args...);
Report safety violation for function fd, or squirrel away error message in fd.safetyViolation if needed later. Call when fd was just inferred to be @system OR fd was @safe and an tried something unsafe.
Parameters:
FuncDeclaration fd function we're gonna rat on
bool gag suppress error message (used in escape.d)
Loc loc location of error
const(char)* format printf-style format string
RootObject[] args arguments for %s format specifier
bool setFunctionToUnsafe(FuncDeclaration fd);
Function is doing something unsafe. If inference is in process, commit the function to be @system.
Parameters:
FuncDeclaration fd the naughty function
Returns:
true if this is a safe function and so an error OR is inferred to be @system, false otherwise.
bool setUnsafeCall(FuncDeclaration fd, FuncDeclaration f);
The function is calling @system function f, so mark it as unsafe.
Parameters:
FuncDeclaration fd caller
FuncDeclaration f function being called (needed for diagnostic of inferred functions)
Returns:
whether there's a safe error
bool setUnsafe(Scope* sc, bool gag, Loc loc, const(char)* format, RootObject[] args...);
A statement / expression in this scope is not @safe, so mark the enclosing function as @system
Parameters:
Scope* sc scope that the unsafe statement / expression is in
bool gag surpress error message (used in escape.d)
Loc loc location of error
const(char)* format printf-style format string
RootObject[] args arguments for format string
Returns:
whether there is a safe error
bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char)* format, RootObject[] args...);
Like setUnsafe, but for safety errors still behind preview switches
Given a FeatureState fs, for example dip1000 / dip25 / systemVariables, the behavior changes based on the setting:
  • In case of -revert=fs, it does nothing.
  • In case of -preview=fs, it's the same as setUnsafe
  • By default, print a deprecation in @safe functions, or store an attribute violation in inferred functions.
Parameters:
Scope* sc used to find affected function/variable, and for checking whether we are in a deprecated / speculative scope
FeatureState fs feature state from the preview flag
bool gag surpress error message
Loc loc location of error
const(char)* format printf-style format string
RootObject[] args arguments for format string
Returns:
whether an actual safe error (not deprecation) occured