Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/src/dmd/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,10 @@ class FuncDeclaration : public Declaration
bool nothrowInprocess(bool v);
bool nogcInprocess() const;
bool nogcInprocess(bool v);
bool returnInprocess() const;
bool returnInprocess(bool v);
bool scopeInprocess() const;
bool scopeInprocess(bool v);
bool inlineScanned() const;
bool inlineScanned(bool v);
bool inferScope() const;
bool inferScope(bool v);
bool hasCatches() const;
bool hasCatches(bool v);
bool skipCodegen() const;
Expand Down
28 changes: 12 additions & 16 deletions compiler/src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ private bool checkReturnEscapeImpl(ref Scope sc, Expression e, bool refs, bool g
return;
}
FuncDeclaration fd = p.isFuncDeclaration();
if (fd && sc.func.returnInprocess)
if (fd && sc.func.scopeInprocess)
{
/* Code like:
* int x;
Expand Down Expand Up @@ -1448,7 +1448,7 @@ private bool inferReturn(FuncDeclaration fd, VarDeclaration v, bool returnScope)
if (!v.isParameter() || v.isTypesafeVariadicArray || (returnScope && v.doNotInferReturn))
return false;

if (!fd.returnInprocess)
if (!fd.scopeInprocess)
return false;

if (returnScope && !(v.isScope() || v.maybeScope))
Expand Down Expand Up @@ -2073,24 +2073,20 @@ private void doNotInferScope(VarDeclaration v, RootObject o)
public
void finishScopeParamInference(FuncDeclaration funcdecl, ref TypeFunction f)
{
if (!funcdecl.scopeInprocess)
return;
funcdecl.scopeInprocess = false;

if (funcdecl.returnInprocess)
if (funcdecl.storage_class & STC.return_)
{
funcdecl.returnInprocess = false;
if (funcdecl.storage_class & STC.return_)
{
if (funcdecl.type == f)
f = cast(TypeFunction)f.copy();
f.isreturn = true;
f.isreturnscope = cast(bool) (funcdecl.storage_class & STC.returnScope);
if (funcdecl.storage_class & STC.returninferred)
f.isreturninferred = true;
}
if (funcdecl.type == f)
f = cast(TypeFunction)f.copy();
f.isreturn = true;
f.isreturnscope = cast(bool) (funcdecl.storage_class & STC.returnScope);
if (funcdecl.storage_class & STC.returninferred)
f.isreturninferred = true;
}

if (!funcdecl.inferScope)
return;
funcdecl.inferScope = false;

// Infer STC.scope_
if (funcdecl.parameters && !funcdecl.errors)
Expand Down
6 changes: 2 additions & 4 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3698,12 +3698,10 @@ class FuncDeclaration : public Declaration
bool nothrowInprocess(bool v);
bool nogcInprocess() const;
bool nogcInprocess(bool v);
bool returnInprocess() const;
bool returnInprocess(bool v);
bool scopeInprocess() const;
bool scopeInprocess(bool v);
bool inlineScanned() const;
bool inlineScanned(bool v);
bool inferScope() const;
bool inferScope(bool v);
bool hasCatches() const;
bool hasCatches(bool v);
bool skipCodegen() const;
Expand Down
10 changes: 3 additions & 7 deletions compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ private struct FUNCFLAG
bool safetyInprocess; /// working on determining safety
bool nothrowInprocess; /// working on determining nothrow
bool nogcInprocess; /// working on determining @nogc
bool returnInprocess; /// working on inferring 'return' for parameters
bool scopeInprocess; /// infer `return` and `scope` for parameters
bool inlineScanned; /// function has been scanned for inline possibilities
bool inferScope; /// infer 'scope' for parameters
bool hasCatches; /// function has try-catch statements
bool skipCodegen; /// do not generate code for this function.
bool printf; /// is a printf-like function
Expand Down Expand Up @@ -722,11 +721,8 @@ extern (C++) class FuncDeclaration : Declaration
if (!tf.isnogc)
nogcInprocess = true;

if (!isVirtual() || this.isIntroducing())
returnInprocess = true;

// Initialize for inferring STC.scope_
inferScope = true;
scopeInprocess = true;
}

extern (D) final uint saveFlags()
Expand Down Expand Up @@ -1674,7 +1670,7 @@ extern (C++) final class FuncLiteralDeclaration : FuncDeclaration
this.fes = fes;
// Always infer scope for function literals
// See https://issues.dlang.org/show_bug.cgi?id=20362
this.inferScope = true;
this.scopeInprocess = true;
//printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this.ident.toChars(), type.toChars());
}

Expand Down
20 changes: 20 additions & 0 deletions compiler/test/fail_compilation/test24680.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
REQUIRED_ARGS: -preview=dip1000
TEST_OUTPUT:
---
fail_compilation/test24680.d(19): Error: returning `c.peek(buf[])` escapes a reference to local variable `buf`
---
*/

// https://issues.dlang.org/show_bug.cgi?id=24680

class C
{
final auto peek(ubyte[] buf) { return buf; }
}

@safe escape(C c)
{
ubyte[5] buf;
return c.peek(buf[]);
}