2 * Perform checks for `nothrow`.
4 * Specification: $(LINK2 https://dlang.org/spec/function.html#nothrow-functions, Nothrow Functions)
6 * Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/canthrow.d, _canthrow.d)
10 * Documentation: https://dlang.org/phobos/dmd_canthrow.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/canthrow.d
18 import dmd.arraytypes;
21 import dmd.declaration;
23 import dmd.expression;
28 import dmd.root.rootobject;
32 /********************************************
33 * Returns true if the expression may throw exceptions.
34 * If 'mustNotThrow' is true, generate an error if it throws
36 extern (C++) bool canThrow(Expression e, FuncDeclaration func, bool mustNotThrow)
38 //printf("Expression::canThrow(%d) %s\n", mustNotThrow, toChars());
39 // stop walking if we determine this expression can throw
40 extern (C++) final class CanThrow : StoppableVisitor
42 alias visit = typeof(super).visit;
47 extern (D) this(FuncDeclaration func, bool mustNotThrow)
50 this.mustNotThrow = mustNotThrow;
53 void checkFuncThrows(Expression e, FuncDeclaration f)
55 auto tf = f.type.toBasetype().isTypeFunction();
56 if (tf && !tf.isnothrow)
60 e.error("%s `%s` is not `nothrow`",
61 f.kind(), f.toPrettyChars());
63 e.checkOverridenDtor(null, f, dd => dd.type.toTypeFunction().isnothrow, "not nothrow");
65 stop = true; // if any function throws, then the whole expression throws
69 override void visit(Expression)
73 override void visit(DeclarationExp de)
75 stop = Dsymbol_canThrow(de.declaration, func, mustNotThrow);
78 override void visit(CallExp ce)
80 if (ce.inDebugStatement)
83 if (global.errors && !ce.e1.type)
84 return; // error recovery
85 /* If calling a function or delegate that is typed as nothrow,
86 * then this expression cannot throw.
87 * Note that pure functions can throw.
89 if (ce.f && ce.f == func)
91 Type t = ce.e1.type.toBasetype();
92 auto tf = t.isTypeFunction();
93 if (tf && tf.isnothrow)
97 auto td = t.isTypeDelegate();
98 if (td && td.nextOf().isTypeFunction().isnothrow)
103 checkFuncThrows(ce, ce.f);
104 else if (mustNotThrow)
107 if (auto pe = e1.isPtrExp()) // print 'fp' if e1 is (*fp)
109 ce.error("`%s` is not `nothrow`", e1.toChars());
114 override void visit(NewExp ne)
118 // See if constructor call can throw
119 checkFuncThrows(ne, ne.member);
121 // regard storage allocation failures as not recoverable
124 override void visit(DeleteExp de)
126 Type tb = de.e1.type.toBasetype();
127 AggregateDeclaration ad = null;
131 ad = tb.isTypeClass().sym;
136 auto ts = tb.nextOf().baseElemOf().isTypeStruct();
143 assert(0); // error should have been detected by semantic()
147 checkFuncThrows(de, ad.dtor);
150 override void visit(AssignExp ae)
152 // blit-init cannot throw
153 if (ae.op == TOK.blit)
155 /* Element-wise assignment could invoke postblits.
158 if (ae.type.toBasetype().ty == Tsarray)
160 if (!ae.e2.isLvalue())
164 else if (auto se = ae.e1.isSliceExp())
169 if (auto ts = t.baseElemOf().isTypeStruct())
170 if (auto postblit = ts.sym.postblit)
171 checkFuncThrows(ae, postblit);
174 override void visit(NewAnonClassExp)
176 assert(0); // should have been lowered by semantic()
180 scope CanThrow ct = new CanThrow(func, mustNotThrow);
181 return walkPostorder(e, ct);
184 /**************************************
185 * Does symbol, when initialized, throw?
186 * Mirrors logic in Dsymbol_toElem().
188 private bool Dsymbol_canThrow(Dsymbol s, FuncDeclaration func, bool mustNotThrow)
190 int symbolDg(Dsymbol s)
192 return Dsymbol_canThrow(s, func, mustNotThrow);
195 //printf("Dsymbol_toElem() %s\n", s.toChars());
196 if (auto vd = s.isVarDeclaration())
200 return Dsymbol_canThrow(s, func, mustNotThrow);
201 if (vd.storage_class & STC.manifest)
204 else if (vd.isStatic() || vd.storage_class & (STC.extern_ | STC.tls | STC.gshared))
211 if (auto ie = vd._init.isExpInitializer())
212 if (canThrow(ie.exp, func, mustNotThrow))
215 if (vd.needsScopeDtor())
216 return canThrow(vd.edtor, func, mustNotThrow);
219 else if (auto ad = s.isAttribDeclaration())
221 return ad.include(null).foreachDsymbol(&symbolDg) != 0;
223 else if (auto tm = s.isTemplateMixin())
225 return tm.members.foreachDsymbol(&symbolDg) != 0;
227 else if (auto td = s.isTupleDeclaration())
229 for (size_t i = 0; i < td.objects.dim; i++)
231 RootObject o = (*td.objects)[i];
232 if (o.dyncast() == DYNCAST.expression)
234 Expression eo = cast(Expression)o;
235 if (auto se = eo.isDsymbolExp())
237 if (Dsymbol_canThrow(se.s, func, mustNotThrow))