Debian Codegraph @

gcc/d/dmd/canthrow.d

raw
1 /**
2 * Perform checks for `nothrow`.
3 *
4 * Specification: $(LINK2 https://dlang.org/spec/function.html#nothrow-functions, Nothrow Functions)
5 *
6 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/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/compiler/src/dmd/canthrow.d
12 */
13
14 module dmd.canthrow;
15
16 import dmd.aggregate;
17 import dmd.arraytypes;
18 import dmd.attrib;
19 import dmd.astenums;
20 import dmd.blockexit : BE, checkThrow;
21 import dmd.declaration;
22 import dmd.dsymbol;
23 import dmd.dsymbolsem : include;
24 import dmd.errorsink;
25 import dmd.expression;
26 import dmd.expressionsem : errorSupplementalInferredAttr;
27 import dmd.func;
28 import dmd.globals;
29 import dmd.init;
30 import dmd.mtype;
31 import dmd.tokens;
32 import dmd.visitor;
33 import dmd.visitor.postorder;
34
35 /**
36 * Status indicating what kind of throwable might be caused by an expression.
37 *
38 * This is a subset of `BE` restricted to the values actually used by `canThrow`.
39 */
40 enum CT : BE
41 {
42 /// Never throws an `Exception` or `Throwable`
43 none = BE.none,
44
45 /// Might throw an `Exception`
46 exception = BE.throw_,
47
48 // Might throw an `Error`
49 error = BE.errthrow,
50 }
51
52 /********************************************
53 * If `eSink` is not null, generate an error if `e` throws
54 * Params:
55 * e = expression to check for throwing
56 * func = function
57 * eSink = if !null, then send error messages to eSink
58 * Returns: `CT.exception` or `CT.error` if the expression may throw exceptions.
59 */
60 CT canThrow(Expression e, FuncDeclaration func, ErrorSink eSink)
61 {
62 //printf("Expression::canThrow(%d) %s\n", mustNotThrow, e.toChars());
63 // stop walking if we determine this expression can throw
64 extern (C++) final class CanThrow : StoppableVisitor
65 {
66 alias visit = typeof(super).visit;
67 CT result;
68
69 public:
70 extern (D) this() scope @safe
71 {
72 }
73
74 void checkFuncThrows(Expression e, FuncDeclaration f)
75 {
76 auto tf = f.type.toBasetype().isTypeFunction();
77 if (tf && !tf.isNothrow)
78 {
79 if (eSink)
80 {
81 eSink.error(e.loc, "%s `%s` is not `nothrow`", f.kind(), f.toPrettyChars());
82 if (!f.isDtorDeclaration())
83 errorSupplementalInferredAttr(f, 10, false, STC.nothrow_, eSink);
84
85 import dmd.expressionsem : checkOverriddenDtor;
86 f.checkOverriddenDtor(null, e.loc, dd => dd.type.toTypeFunction().isNothrow, "not nothrow");
87 }
88 else if (func)
89 {
90 func.setThrowCall(e.loc, f);
91 }
92 result |= CT.exception;
93 }
94 }
95
96 override void visit(Expression)
97 {
98 }
99
100 override void visit(DeclarationExp de)
101 {
102 result |= Dsymbol_canThrow(de.declaration, func, eSink);
103 }
104
105 override void visit(CallExp ce)
106 {
107 if (ce.inDebugStatement)
108 return;
109
110 if (global.errors && !ce.e1.type)
111 return; // error recovery
112
113 if (ce.f && ce.arguments.length > 0)
114 {
115 Type tb = (*ce.arguments)[0].type.toBasetype();
116 if (auto tbNext = tb.nextOf())
117 {
118 if (auto ts = tbNext.baseElemOf().isTypeStruct())
119 {
120 auto sd = ts.sym;
121 const id = ce.f.ident;
122 if (sd.postblit && isArrayConstruction(id))
123 {
124 checkFuncThrows(ce, sd.postblit);
125 return;
126 }
127 }
128 }
129 }
130
131 /* If calling a function or delegate that is typed as nothrow,
132 * then this expression cannot throw.
133 * Note that pure functions can throw.
134 */
135 if (ce.f && ce.f == func)
136 return;
137 const tf = ce.calledFunctionType();
138 if (tf && tf.isNothrow)
139 return;
140
141 if (ce.f)
142 checkFuncThrows(ce, ce.f);
143 else if (eSink)
144 {
145 auto e1 = ce.e1;
146 if (auto pe = e1.isPtrExp()) // print 'fp' if e1 is (*fp)
147 e1 = pe.e1;
148 eSink.error(ce.loc, "`%s` is not `nothrow`", e1.toChars());
149 }
150 result |= CT.exception;
151 }
152
153 override void visit(NewExp ne)
154 {
155 if (ne.member)
156 {
157 // See if constructor call can throw
158 checkFuncThrows(ne, ne.member);
159 }
160 // regard storage allocation failures as not recoverable
161 }
162
163 override void visit(DeleteExp de)
164 {
165 Type tb = de.e1.type.toBasetype();
166 AggregateDeclaration ad = null;
167 switch (tb.ty)
168 {
169 case Tclass:
170 ad = tb.isTypeClass().sym;
171 break;
172
173 default:
174 assert(0); // error should have been detected by semantic()
175 }
176
177 if (ad.dtor)
178 checkFuncThrows(de, ad.dtor);
179 }
180
181 override void visit(AssignExp ae)
182 {
183 // blit-init cannot throw
184 if (ae.op == EXP.blit)
185 return;
186 /* Element-wise assignment could invoke postblits.
187 */
188 Type t;
189 if (ae.type.toBasetype().ty == Tsarray)
190 {
191 if (!ae.e2.isLvalue())
192 return;
193 t = ae.type;
194 }
195 else if (auto se = ae.e1.isSliceExp())
196 t = se.e1.type;
197 else
198 return;
199
200 if (auto ts = t.baseElemOf().isTypeStruct())
201 if (auto postblit = ts.sym.postblit)
202 checkFuncThrows(ae, postblit);
203 }
204
205 override void visit(ThrowExp te)
206 {
207 const res = checkThrow(te.loc, te.e1, func, eSink);
208 assert((res & ~(CT.exception | CT.error)) == 0);
209 result |= res;
210 }
211
212 override void visit(NewAnonClassExp)
213 {
214 assert(0); // should have been lowered by semantic()
215 }
216 }
217
218 scope CanThrow ct = new CanThrow();
219 walkPostorder(e, ct);
220 return ct.result;
221 }
222
223 /**************************************
224 * Does symbol `s`, when initialized, throw?
225 * Mirrors logic in Dsymbol_toElem().
226 */
227 private CT Dsymbol_canThrow(Dsymbol s, FuncDeclaration func, ErrorSink eSink)
228 {
229 CT result;
230
231 int symbolDg(Dsymbol s)
232 {
233 result |= Dsymbol_canThrow(s, func, eSink);
234 return 0;
235 }
236
237 //printf("Dsymbol_toElem() %s\n", s.toChars());
238 if (auto vd = s.isVarDeclaration())
239 {
240 s = s.toAlias();
241 if (s != vd)
242 return Dsymbol_canThrow(s, func, eSink);
243 if (vd.storage_class & STC.manifest)
244 {
245 }
246 else if (vd.isStatic() || vd.storage_class & (STC.extern_ | STC.gshared))
247 {
248 }
249 else
250 {
251 if (vd._init)
252 {
253 if (auto ie = vd._init.isExpInitializer())
254 result |= canThrow(ie.exp, func, eSink);
255 }
256 if (vd.needsScopeDtor())
257 result |= canThrow(vd.edtor, func, eSink);
258 }
259 }
260 else if (auto ad = s.isAttribDeclaration())
261 {
262 ad.include(null).foreachDsymbol(&symbolDg);
263 }
264 else if (auto tm = s.isTemplateMixin())
265 {
266 tm.members.foreachDsymbol(&symbolDg);
267 }
268 else if (auto td = s.isTupleDeclaration())
269 {
270 td.foreachVar(&symbolDg);
271 }
272 return result;
273 }