Skip to content

Commit

Permalink
check nogc on default func args but do not check nogc in if(__ctfe) b…
Browse files Browse the repository at this point in the history
…locks
  • Loading branch information
adamdruppe committed Nov 19, 2024
1 parent 9adbc04 commit ba85bbd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ extern (C++) /* IN_LLVM abstract */ class Expression : ASTNode
Loc loc; // file location
const EXP op; // to minimize use of dynamic_cast

bool ctfe = false; // TODO: bake this byte flag or move to relevant Expressions

extern (D) this(const ref Loc loc, EXP op) scope @safe
{
//printf("Expression::Expression(op = %d) this = %p\n", op, this);
Expand Down
26 changes: 25 additions & 1 deletion compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2899,9 +2899,27 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
arg = p.defaultArg;
if (!arg.type)
arg = arg.expressionSemantic(sc);

if (auto efd = sc.getEnclosingFunction()) {
TypeFunction etf = efd.type.toTypeFunction();
if (etf && etf.isnogc) {
if (auto ce = arg.isCallExp()) {
if (TypeFunction atf = ce.f.type.toTypeFunction()) {
// Both conditions works
if (!atf.isnogc) {
// if (!ce.f.setGC(arg.loc, "`@nogc` %s `%s` cannot call non-@nogc")) {
error(loc, "`@nogc` `%s` cannot call `%s` with non-@nogc argument `%s`", efd.toChars(), tf.toChars(), arg.toChars());
return true;
}
}
}
}
}

arg = inlineCopy(arg, sc);
// __FILE__, __LINE__, __MODULE__, __FUNCTION__, and __PRETTY_FUNCTION__
arg = arg.resolveLoc(loc, sc);

if (i >= nargs)
{
arguments.push(arg);
Expand Down Expand Up @@ -14190,7 +14208,13 @@ extern (C++) Expression expressionSemantic(Expression e, Scope* sc)
{
scope v = new ExpressionSemanticVisitor(sc);
e.accept(v);
return v.result;

auto res = v.result;

if(sc && (sc.flags & SCOPE.ctfeBlock))
e.ctfe = true;

return res;
}

private Expression dotIdSemanticPropX(DotIdExp exp, Scope* sc)
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dmd/nogc.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public:
if (global.params.ehnogc && e.thrownew)
return; // separate allocator is called for this, not the GC
if (explicit_gc)
return; // `new` is an explicit allocation
return; // `new` is an explicit allocation
if (setGC(e, "cannot use `new` in `@nogc` %s `%s`"))
return;
f.printGCUsage(e.loc, "`new` causes a GC allocation");
Expand Down Expand Up @@ -219,6 +219,9 @@ Expression checkGC(Scope* sc, Expression e)
if (sc.flags & SCOPE.ctfeBlock) // ignore GC in ctfe blocks
return e;

if (e.ctfe)
return e;

/* If betterC, allow GC to happen in non-CTFE code.
* Just don't generate code for it.
* Detect non-CTFE use of the GC in betterC code.
Expand Down

0 comments on commit ba85bbd

Please sign in to comment.