| 1 |
/** |
| 2 |
* Find side-effects of expressions. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/sideeffect.d, _sideeffect.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_sideeffect.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/sideeffect.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.sideeffect; |
| 13 |
|
| 14 |
import dmd.astenums; |
| 15 |
import dmd.declaration; |
| 16 |
import dmd.dscope; |
| 17 |
import dmd.errors; |
| 18 |
import dmd.expression; |
| 19 |
import dmd.expressionsem; |
| 20 |
import dmd.func; |
| 21 |
import dmd.funcsem; |
| 22 |
import dmd.globals; |
| 23 |
import dmd.hdrgen; |
| 24 |
import dmd.id; |
| 25 |
import dmd.identifier; |
| 26 |
import dmd.init; |
| 27 |
import dmd.mtype; |
| 28 |
import dmd.tokens; |
| 29 |
import dmd.typesem; |
| 30 |
import dmd.visitor; |
| 31 |
import dmd.visitor.postorder; |
| 32 |
|
| 33 |
/************************************************** |
| 34 |
* Front-end expression rewriting should create temporary variables for |
| 35 |
* non trivial sub-expressions in order to: |
| 36 |
* 1. save evaluation order |
| 37 |
* 2. prevent sharing of sub-expression in AST |
| 38 |
*/ |
| 39 |
bool isTrivialExp(Expression e) |
| 40 |
{ |
| 41 |
extern (C++) final class IsTrivialExp : StoppableVisitor |
| 42 |
{ |
| 43 |
alias visit = typeof(super).visit; |
| 44 |
public: |
| 45 |
extern (D) this() scope @safe |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
override void visit(Expression e) |
| 50 |
{ |
| 51 |
/* https://issues.dlang.org/show_bug.cgi?id=11201 |
| 52 |
* CallExp is always non trivial expression, |
| 53 |
* especially for inlining. |
| 54 |
*/ |
| 55 |
if (e.op == EXP.call) |
| 56 |
{ |
| 57 |
stop = true; |
| 58 |
return; |
| 59 |
} |
| 60 |
// stop walking if we determine this expression has side effects |
| 61 |
stop = lambdaHasSideEffect(e); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
scope IsTrivialExp v = new IsTrivialExp(); |
| 66 |
return walkPostorder(e, v) == false; |
| 67 |
} |
| 68 |
|
| 69 |
/******************************************** |
| 70 |
* Determine if Expression has any side effects. |
| 71 |
* |
| 72 |
* Params: |
| 73 |
* e = the expression |
| 74 |
* assumeImpureCalls = whether function calls should always be assumed to |
| 75 |
* be impure (e.g. debug is allowed to violate purity) |
| 76 |
*/ |
| 77 |
bool hasSideEffect(Expression e, bool assumeImpureCalls = false) |
| 78 |
{ |
| 79 |
extern (C++) final class LambdaHasSideEffect : StoppableVisitor |
| 80 |
{ |
| 81 |
alias visit = typeof(super).visit; |
| 82 |
public: |
| 83 |
extern (D) this() scope @safe |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
override void visit(Expression e) |
| 88 |
{ |
| 89 |
// stop walking if we determine this expression has side effects |
| 90 |
stop = lambdaHasSideEffect(e, assumeImpureCalls); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
scope LambdaHasSideEffect v = new LambdaHasSideEffect(); |
| 95 |
return walkPostorder(e, v); |
| 96 |
} |
| 97 |
|
| 98 |
/******************************************** |
| 99 |
* Determine if the call of f, or function type or delegate type t1, has any side effects. |
| 100 |
* Returns: |
| 101 |
* 0 has any side effects |
| 102 |
* 1 nothrow + strongly pure |
| 103 |
* 2 nothrow + strongly pure + only immutable indirections in the return |
| 104 |
* type |
| 105 |
*/ |
| 106 |
int callSideEffectLevel(FuncDeclaration f) |
| 107 |
{ |
| 108 |
/* https://issues.dlang.org/show_bug.cgi?id=12760 |
| 109 |
* https://issues.dlang.org/show_bug.cgi?id=16384 |
| 110 |
* |
| 111 |
* ctor calls and invariant calls always have side effects |
| 112 |
*/ |
| 113 |
if (f.isCtorDeclaration() || f.isInvariantDeclaration()) |
| 114 |
return 0; |
| 115 |
assert(f.type.ty == Tfunction); |
| 116 |
TypeFunction tf = cast(TypeFunction)f.type; |
| 117 |
if (!tf.isNothrow) |
| 118 |
return 0; |
| 119 |
final switch (f.isPure()) |
| 120 |
{ |
| 121 |
case PURE.impure: |
| 122 |
case PURE.fwdref: |
| 123 |
case PURE.weak: |
| 124 |
return 0; |
| 125 |
|
| 126 |
case PURE.const_: |
| 127 |
return mutabilityOfType(tf.isRef, tf.next) == 2 ? 2 : 1; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
int callSideEffectLevel(Type t) |
| 132 |
{ |
| 133 |
t = t.toBasetype(); |
| 134 |
TypeFunction tf; |
| 135 |
if (t.ty == Tdelegate) |
| 136 |
tf = cast(TypeFunction)(cast(TypeDelegate)t).next; |
| 137 |
else |
| 138 |
{ |
| 139 |
assert(t.ty == Tfunction); |
| 140 |
tf = cast(TypeFunction)t; |
| 141 |
} |
| 142 |
if (!tf.isNothrow) // function can throw |
| 143 |
return 0; |
| 144 |
|
| 145 |
tf.purityLevel(); |
| 146 |
PURE purity = tf.purity; |
| 147 |
if (t.ty == Tdelegate && purity > PURE.weak) |
| 148 |
{ |
| 149 |
if (tf.isMutable()) |
| 150 |
purity = PURE.weak; |
| 151 |
else if (!tf.isImmutable()) |
| 152 |
purity = PURE.const_; |
| 153 |
} |
| 154 |
|
| 155 |
if (purity == PURE.const_) |
| 156 |
return mutabilityOfType(tf.isRef, tf.next) == 2 ? 2 : 1; |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
private bool lambdaHasSideEffect(Expression e, bool assumeImpureCalls = false) |
| 162 |
{ |
| 163 |
switch (e.op) |
| 164 |
{ |
| 165 |
// Sort the cases by most frequently used first |
| 166 |
case EXP.assign: |
| 167 |
case EXP.plusPlus: |
| 168 |
case EXP.minusMinus: |
| 169 |
case EXP.declaration: |
| 170 |
case EXP.construct: |
| 171 |
case EXP.blit: |
| 172 |
case EXP.addAssign: |
| 173 |
case EXP.minAssign: |
| 174 |
case EXP.concatenateAssign: |
| 175 |
case EXP.concatenateElemAssign: |
| 176 |
case EXP.concatenateDcharAssign: |
| 177 |
case EXP.mulAssign: |
| 178 |
case EXP.divAssign: |
| 179 |
case EXP.modAssign: |
| 180 |
case EXP.leftShiftAssign: |
| 181 |
case EXP.rightShiftAssign: |
| 182 |
case EXP.unsignedRightShiftAssign: |
| 183 |
case EXP.andAssign: |
| 184 |
case EXP.orAssign: |
| 185 |
case EXP.xorAssign: |
| 186 |
case EXP.powAssign: |
| 187 |
case EXP.in_: |
| 188 |
case EXP.remove: |
| 189 |
case EXP.assert_: |
| 190 |
case EXP.halt: |
| 191 |
case EXP.throw_: |
| 192 |
case EXP.delete_: |
| 193 |
case EXP.new_: |
| 194 |
case EXP.newAnonymousClass: |
| 195 |
case EXP.loweredAssignExp: |
| 196 |
return true; |
| 197 |
case EXP.call: |
| 198 |
{ |
| 199 |
if (assumeImpureCalls) |
| 200 |
return true; |
| 201 |
|
| 202 |
if (e.type && e.type.ty == Tnoreturn) |
| 203 |
return true; |
| 204 |
|
| 205 |
CallExp ce = cast(CallExp)e; |
| 206 |
/* Calling a function or delegate that is pure nothrow |
| 207 |
* has no side effects. |
| 208 |
*/ |
| 209 |
if (ce.e1.type) |
| 210 |
{ |
| 211 |
Type t = ce.e1.type.toBasetype(); |
| 212 |
if (t.ty == Tdelegate) |
| 213 |
t = (cast(TypeDelegate)t).next; |
| 214 |
|
| 215 |
const level = t.ty == Tfunction && (ce.f ? callSideEffectLevel(ce.f) : callSideEffectLevel(ce.e1.type)); |
| 216 |
if (level == 0) // 0 means the function has a side effect |
| 217 |
return true; |
| 218 |
} |
| 219 |
break; |
| 220 |
} |
| 221 |
case EXP.cast_: |
| 222 |
{ |
| 223 |
CastExp ce = cast(CastExp)e; |
| 224 |
/* if: |
| 225 |
* cast(classtype)func() // because it may throw |
| 226 |
*/ |
| 227 |
if (ce.to.ty == Tclass && ce.e1.op == EXP.call && ce.e1.type.ty == Tclass) |
| 228 |
return true; |
| 229 |
break; |
| 230 |
} |
| 231 |
default: |
| 232 |
break; |
| 233 |
} |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
/*********************************** |
| 238 |
* The result of this expression will be discarded. |
| 239 |
* Print error messages if the operation has no side effects (and hence is meaningless). |
| 240 |
* Returns: |
| 241 |
* true if expression has no side effects |
| 242 |
*/ |
| 243 |
bool discardValue(Expression e) |
| 244 |
{ |
| 245 |
if (lambdaHasSideEffect(e)) // check side-effect shallowly |
| 246 |
return false; |
| 247 |
switch (e.op) |
| 248 |
{ |
| 249 |
case EXP.cast_: |
| 250 |
{ |
| 251 |
CastExp ce = cast(CastExp)e; |
| 252 |
if (ce.to.equals(Type.tvoid)) |
| 253 |
{ |
| 254 |
/* |
| 255 |
* Don't complain about an expression with no effect if it was cast to void |
| 256 |
*/ |
| 257 |
return false; |
| 258 |
} |
| 259 |
break; // complain |
| 260 |
} |
| 261 |
// Assumption that error => no side effect |
| 262 |
case EXP.error: |
| 263 |
return true; |
| 264 |
case EXP.variable: |
| 265 |
{ |
| 266 |
VarDeclaration v = (cast(VarExp)e).var.isVarDeclaration(); |
| 267 |
if (v && (v.storage_class & STC.temp)) |
| 268 |
{ |
| 269 |
// https://issues.dlang.org/show_bug.cgi?id=5810 |
| 270 |
// Don't complain about an internal generated variable. |
| 271 |
return false; |
| 272 |
} |
| 273 |
break; |
| 274 |
} |
| 275 |
case EXP.call: |
| 276 |
// https://issues.dlang.org/show_bug.cgi?id=24359 |
| 277 |
auto ce = e.isCallExp(); |
| 278 |
if (const f = ce.f) |
| 279 |
{ |
| 280 |
if (f.ident == Id.__equals && ce.arguments && ce.arguments.length == 2) |
| 281 |
{ |
| 282 |
return discardValue(new EqualExp(EXP.equal, e.loc, (*ce.arguments)[0], (*ce.arguments)[1])); |
| 283 |
} |
| 284 |
} |
| 285 |
return false; |
| 286 |
case EXP.andAnd: |
| 287 |
case EXP.orOr: |
| 288 |
{ |
| 289 |
LogicalExp aae = cast(LogicalExp)e; |
| 290 |
return discardValue(aae.e2); |
| 291 |
} |
| 292 |
case EXP.question: |
| 293 |
{ |
| 294 |
CondExp ce = cast(CondExp)e; |
| 295 |
/* https://issues.dlang.org/show_bug.cgi?id=6178 |
| 296 |
* https://issues.dlang.org/show_bug.cgi?id=14089 |
| 297 |
* Either CondExp::e1 or e2 may have |
| 298 |
* redundant expression to make those types common. For example: |
| 299 |
* |
| 300 |
* struct S { this(int n); int v; alias v this; } |
| 301 |
* S[int] aa; |
| 302 |
* aa[1] = 0; |
| 303 |
* |
| 304 |
* The last assignment statement will be rewitten to: |
| 305 |
* |
| 306 |
* 1 in aa ? aa[1].value = 0 : (aa[1] = 0, aa[1].this(0)).value; |
| 307 |
* |
| 308 |
* The last DotVarExp is necessary to take assigned value. |
| 309 |
* |
| 310 |
* int value = (aa[1] = 0); // value = aa[1].value |
| 311 |
* |
| 312 |
* To avoid false error, discardValue() should be called only when |
| 313 |
* the both tops of e1 and e2 have actually no side effects. |
| 314 |
*/ |
| 315 |
if (!lambdaHasSideEffect(ce.e1) && !lambdaHasSideEffect(ce.e2)) |
| 316 |
{ |
| 317 |
return discardValue(ce.e1) | |
| 318 |
discardValue(ce.e2); |
| 319 |
} |
| 320 |
return false; |
| 321 |
} |
| 322 |
case EXP.comma: |
| 323 |
{ |
| 324 |
CommaExp ce = cast(CommaExp)e; |
| 325 |
// Don't complain about compiler-generated comma expressions |
| 326 |
if (ce.isGenerated) |
| 327 |
return false; |
| 328 |
|
| 329 |
// Don't check e1 until we cast(void) the a,b code generation. |
| 330 |
// This is concretely done in expressionSemantic, if a CommaExp has Tvoid as type |
| 331 |
return discardValue(ce.e2); |
| 332 |
} |
| 333 |
case EXP.tuple: |
| 334 |
/* Pass without complaint if any of the tuple elements have side effects. |
| 335 |
* Ideally any tuple elements with no side effects should raise an error, |
| 336 |
* this needs more investigation as to what is the right thing to do. |
| 337 |
*/ |
| 338 |
if (!hasSideEffect(e)) |
| 339 |
break; |
| 340 |
return false; |
| 341 |
case EXP.identity, EXP.notIdentity: |
| 342 |
case EXP.equal, EXP.notEqual: |
| 343 |
/* |
| 344 |
`[side effect] == 0` |
| 345 |
Technically has a side effect but is clearly wrong; |
| 346 |
*/ |
| 347 |
BinExp tmp = e.isBinExp(); |
| 348 |
assert(tmp); |
| 349 |
|
| 350 |
error(e.loc, "the result of the equality expression `%s` is discarded", e.toErrMsg()); |
| 351 |
bool seenSideEffect = false; |
| 352 |
foreach(expr; [tmp.e1, tmp.e2]) |
| 353 |
{ |
| 354 |
if (hasSideEffect(expr)) |
| 355 |
{ |
| 356 |
errorSupplemental(expr.loc, "note that `%s` may have a side effect", expr.toErrMsg()); |
| 357 |
seenSideEffect |= true; |
| 358 |
} |
| 359 |
} |
| 360 |
return !seenSideEffect; |
| 361 |
default: |
| 362 |
break; |
| 363 |
} |
| 364 |
error(e.loc, "`%s` has no effect", e.toErrMsg()); |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
/************************************************** |
| 369 |
* Build a temporary variable to copy the value of e into. |
| 370 |
* Params: |
| 371 |
* stc = storage classes will be added to the made temporary variable |
| 372 |
* name = name for temporary variable |
| 373 |
* e = original expression |
| 374 |
* Returns: |
| 375 |
* Newly created temporary variable. |
| 376 |
*/ |
| 377 |
VarDeclaration copyToTemp(STC stc, const char[] name, Expression e) |
| 378 |
{ |
| 379 |
assert(name[0] == '_' && name[1] == '_'); |
| 380 |
auto vd = new VarDeclaration(e.loc, e.type, |
| 381 |
Identifier.generateId(name), |
| 382 |
new ExpInitializer(e.loc, e)); |
| 383 |
vd.storage_class = stc | STC.temp | STC.ctfe; // temporary is always CTFEable |
| 384 |
return vd; |
| 385 |
} |
| 386 |
|
| 387 |
/************************************************** |
| 388 |
* Build a temporary variable to extract e's evaluation, if e is not trivial. |
| 389 |
* Params: |
| 390 |
* sc = scope |
| 391 |
* name = name for temporary variable |
| 392 |
* e0 = a new side effect part will be appended to it. |
| 393 |
* e = original expression |
| 394 |
* alwaysCopy = if true, build new temporary variable even if e is trivial. |
| 395 |
* Returns: |
| 396 |
* When e is trivial and alwaysCopy == false, e itself is returned. |
| 397 |
* Otherwise, a new VarExp is returned. |
| 398 |
* Note: |
| 399 |
* e's lvalue-ness will be handled well by STC.ref_ or STC.rvalue. |
| 400 |
*/ |
| 401 |
Expression extractSideEffect(Scope* sc, const char[] name, |
| 402 |
ref Expression e0, Expression e, bool alwaysCopy = false) |
| 403 |
{ |
| 404 |
//printf("extractSideEffect(e: %s)\n", e.toChars()); |
| 405 |
|
| 406 |
/* The trouble here is that if CTFE is running, extracting the side effect |
| 407 |
* results in an assignment, and then the interpreter says it cannot evaluate the |
| 408 |
* side effect assignment variable. But we don't have to worry about side |
| 409 |
* effects in function calls anyway, because then they won't CTFE. |
| 410 |
* https://issues.dlang.org/show_bug.cgi?id=17145 |
| 411 |
*/ |
| 412 |
if (!alwaysCopy && |
| 413 |
(sc.ctfe ? !hasSideEffect(e) : isTrivialExp(e))) |
| 414 |
return e; |
| 415 |
|
| 416 |
auto vd = copyToTemp(STC.none, name, e); |
| 417 |
vd.storage_class |= e.isLvalue() ? STC.ref_ : STC.rvalue; |
| 418 |
|
| 419 |
e0 = Expression.combine(e0, new DeclarationExp(vd.loc, vd) |
| 420 |
.expressionSemantic(sc)); |
| 421 |
|
| 422 |
return new VarExp(vd.loc, vd) |
| 423 |
.expressionSemantic(sc); |
| 424 |
} |