diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index ebc4fee9b3..438cf0cc54 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -13133,26 +13133,26 @@ bool checkSharedAccess(Expression e, Scope* sc, bool returnRef = false) bool checkAddressVar(Scope* sc, Expression exp, VarDeclaration v) { //printf("checkAddressVar(exp: %s, v: %s)\n", exp.toChars(), v.toChars()); - if (v) + if (v is null) + return true; + + if (!v.canTakeAddressOf()) { - if (!v.canTakeAddressOf()) + exp.error("cannot take address of `%s`", exp.toChars()); + return false; + } + if (sc.func && !sc.intypeof && !v.isDataseg()) + { + const(char)* p = v.isParameter() ? "parameter" : "local"; + v.storage_class &= ~STC.maybescope; + v.doNotInferScope = true; + if (global.params.useDIP1000 != FeatureState.enabled && + !(v.storage_class & STC.temp) && + sc.setUnsafe()) { - exp.error("cannot take address of `%s`", exp.toChars()); + exp.error("cannot take address of %s `%s` in `@safe` function `%s`", p, v.toChars(), sc.func.toChars()); return false; } - if (sc.func && !sc.intypeof && !v.isDataseg()) - { - const(char)* p = v.isParameter() ? "parameter" : "local"; - v.storage_class &= ~STC.maybescope; - v.doNotInferScope = true; - if (global.params.useDIP1000 != FeatureState.enabled && - !(v.storage_class & STC.temp) && - sc.setUnsafe()) - { - exp.error("cannot take address of %s `%s` in `@safe` function `%s`", p, v.toChars(), sc.func.toChars()); - return false; - } - } } return true; } diff --git a/compiler/src/dmd/func.d b/compiler/src/dmd/func.d index 0dfd294f86..f3038ae9df 100644 --- a/compiler/src/dmd/func.d +++ b/compiler/src/dmd/func.d @@ -4466,12 +4466,15 @@ void errorSupplementalInferredSafety(FuncDeclaration fd, int maxDepth, bool depr errorFunc(s.loc, s.fmtStr, s.arg0 ? s.arg0.toChars() : "", s.arg1 ? s.arg1.toChars() : "", s.arg2 ? s.arg2.toChars() : ""); } - else if (FuncDeclaration fd2 = cast(FuncDeclaration) s.arg0) + else if (s.arg0.dyncast() == DYNCAST.dsymbol) { - if (maxDepth > 0) + if (FuncDeclaration fd2 = (cast(Dsymbol) s.arg0).isFuncDeclaration()) { - errorFunc(s.loc, "which calls `%s`", fd2.toPrettyChars()); - errorSupplementalInferredSafety(fd2, maxDepth - 1, deprecation); + if (maxDepth > 0) + { + errorFunc(s.loc, "which calls `%s`", fd2.toPrettyChars()); + errorSupplementalInferredSafety(fd2, maxDepth - 1, deprecation); + } } } }