Fix bugzilla 24803 - __traits(location) is inconsistent with modules

This commit is contained in:
Dennis Korpel 2024-10-10 11:33:00 +02:00 committed by The Dlang Bot
parent 2db13b18d4
commit 00df883e9f
10 changed files with 17 additions and 34 deletions

View file

@ -632,7 +632,7 @@ extern (C++) final class Module : Package
const name = srcfile.toString(); const name = srcfile.toString();
if (FileName.equals(name, "object.d")) if (FileName.equals(name, "object.d"))
{ {
ObjectNotFound(loc, ident); ObjectNotFound(Loc.initial, ident);
} }
else if (FileName.ext(this.arg) || !loc.isValid()) else if (FileName.ext(this.arg) || !loc.isValid())
{ {
@ -1408,7 +1408,7 @@ private const(char)[] processSource (const(ubyte)[] src, Module mod)
{ {
enum SourceEncoding { utf16, utf32} enum SourceEncoding { utf16, utf32}
enum Endian { little, big} enum Endian { little, big}
immutable loc = mod.getLoc(); immutable loc = mod.loc;
/* /*
* Convert a buffer from UTF32 to UTF8 * Convert a buffer from UTF32 to UTF8

View file

@ -344,19 +344,6 @@ extern (C++) class Dsymbol : ASTNode
return toChars(); return toChars();
} }
final const(Loc) getLoc()
{
if (!loc.isValid()) // avoid bug 5861.
if (const m = getModule())
return Loc(m.srcfile.toChars(), 0, 0);
return loc;
}
final const(char)* locToChars()
{
return getLoc().toChars();
}
override bool equals(const RootObject o) const override bool equals(const RootObject o) const
{ {
if (this == o) if (this == o)
@ -1303,7 +1290,7 @@ public:
} }
else else
{ {
.error(s1.loc, "%s `%s` conflicts with %s `%s` at %s", s1.kind, s1.toPrettyChars, s2.kind(), s2.toPrettyChars(), s2.locToChars()); .error(s1.loc, "%s `%s` conflicts with %s `%s` at %s", s1.kind, s1.toPrettyChars, s2.kind(), s2.toPrettyChars(), s2.loc.toChars());
} }
} }

View file

@ -195,8 +195,6 @@ public:
CPPNamespaceDeclaration* cppnamespace(CPPNamespaceDeclaration* ns); CPPNamespaceDeclaration* cppnamespace(CPPNamespaceDeclaration* ns);
UserAttributeDeclaration* userAttribDecl(UserAttributeDeclaration* uad); UserAttributeDeclaration* userAttribDecl(UserAttributeDeclaration* uad);
virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments
Loc getLoc();
const char *locToChars();
bool equals(const RootObject * const o) const override; bool equals(const RootObject * const o) const override;
bool isAnonymous() const; bool isAnonymous() const;
Module *getModule(); Module *getModule();

View file

@ -6813,7 +6813,7 @@ extern(C++) class ImportAllVisitor : Visitor
(*m.members)[0].ident != Id.object || (*m.members)[0].ident != Id.object ||
(*m.members)[0].isImport() is null)) (*m.members)[0].isImport() is null))
{ {
auto im = new Import(Loc.initial, null, Id.object, null, 0); auto im = new Import(m.loc, null, Id.object, null, 0);
m.members.shift(im); m.members.shift(im);
} }
if (!m.symtab) if (!m.symtab)

View file

@ -474,8 +474,6 @@ public:
CPPNamespaceDeclaration* cppnamespace(CPPNamespaceDeclaration* ns); CPPNamespaceDeclaration* cppnamespace(CPPNamespaceDeclaration* ns);
UserAttributeDeclaration* userAttribDecl(UserAttributeDeclaration* uad); UserAttributeDeclaration* userAttribDecl(UserAttributeDeclaration* uad);
virtual const char* toPrettyCharsHelper(); virtual const char* toPrettyCharsHelper();
const Loc getLoc();
const char* locToChars();
bool equals(const RootObject* const o) const override; bool equals(const RootObject* const o) const override;
bool isAnonymous() const; bool isAnonymous() const;
Module* getModule(); Module* getModule();

View file

@ -280,7 +280,7 @@ private void obj_write_deferred(ref OutBuffer objbuf, Library library, ref Dsymb
{ {
Identifier id = Identifier.create(idbuf.extractChars()); Identifier id = Identifier.create(idbuf.extractChars());
Module md = new Module(mnames, id, 0, 0); Module md = new Module(m.loc, mnames, id, 0, 0);
md.members = new Dsymbols(); md.members = new Dsymbols();
md.members.push(s); // its only 'member' is s md.members.push(s); // its only 'member' is s
md.doppelganger = 1; // identify this module as doppelganger md.doppelganger = 1; // identify this module as doppelganger

View file

@ -1814,6 +1814,7 @@ bool createModule(const(char)* file, ref Strings libmodules, const ref Target ta
} }
const(char)[] p = FileName.name(file.toDString()); // strip path const(char)[] p = FileName.name(file.toDString()); // strip path
const(char)[] ext = FileName.ext(p); const(char)[] ext = FileName.ext(p);
Loc loc = Loc(file, 0, 0);
if (!ext) if (!ext)
{ {
if (!p.length) if (!p.length)
@ -1822,7 +1823,7 @@ bool createModule(const(char)* file, ref Strings libmodules, const ref Target ta
return true; return true;
} }
auto id = Identifier.idPool(p); auto id = Identifier.idPool(p);
m = new Module(file.toDString, id, global.params.ddoc.doOutput, global.params.dihdr.doOutput); m = new Module(loc, file.toDString, id, global.params.ddoc.doOutput, global.params.dihdr.doOutput);
return false; return false;
} }
@ -1902,8 +1903,7 @@ bool createModule(const(char)* file, ref Strings libmodules, const ref Target ta
* its path and extension. * its path and extension.
*/ */
auto id = Identifier.idPool(name); auto id = Identifier.idPool(name);
m = new Module(loc, file.toDString, id, global.params.ddoc.doOutput, global.params.dihdr.doOutput);
m = new Module(file.toDString, id, global.params.ddoc.doOutput, global.params.dihdr.doOutput);
return false; return false;
} }
eSink.error(Loc.initial, "unrecognized file extension %.*s", cast(int)ext.length, ext.ptr); eSink.error(Loc.initial, "unrecognized file extension %.*s", cast(int)ext.length, ext.ptr);
@ -1954,7 +1954,7 @@ bool createModules(ref Strings files, ref Strings libmodules, const ref Target t
/// Returns: a compiled module (semantic3) containing an empty main() function, for the -main flag /// Returns: a compiled module (semantic3) containing an empty main() function, for the -main flag
Module moduleWithEmptyMain() Module moduleWithEmptyMain()
{ {
auto result = new Module("__main.d", Identifier.idPool("__main"), false, false); auto result = new Module(Loc.initial, "__main.d", Identifier.idPool("__main"), false, false);
// need 2 trailing nulls for sentinel and 2 for lexer // need 2 trailing nulls for sentinel and 2 for lexer
auto data = arraydup("version(D_BetterC)extern(C)int main(){return 0;}else int main(){return 0;}\0\0\0\0"); auto data = arraydup("version(D_BetterC)extern(C)int main(){return 0;}else int main(){return 0;}\0\0\0\0");
result.src = cast(ubyte[]) data[0 .. $-4]; result.src = cast(ubyte[]) data[0 .. $-4];

View file

@ -1991,9 +1991,9 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
return dimError(1); return dimError(1);
auto arg0 = (*e.args)[0]; auto arg0 = (*e.args)[0];
Dsymbol s = getDsymbolWithoutExpCtx(arg0); Dsymbol s = getDsymbolWithoutExpCtx(arg0);
if (!s || !s.loc.isValid()) if (!s || !s.loc.isValid() || s.isModule())
{ {
error(e.loc, "can only get the location of a symbol, not `%s`", arg0.toChars()); error(e.loc, "can only get the location of a symbol, not `%s`", s ? s.toPrettyChars() : arg0.toChars());
return ErrorExp.get(); return ErrorExp.get();
} }

View file

@ -2712,23 +2712,21 @@ Type typeSemantic(Type type, const ref Loc loc, Scope* sc)
which isn't a particularly good error message (x is a variable?). which isn't a particularly good error message (x is a variable?).
*/ */
Dsymbol varDecl = mtype.toDsymbol(sc); Dsymbol varDecl = mtype.toDsymbol(sc);
const(Loc) varDeclLoc = varDecl.getLoc();
Module varDeclModule = varDecl.getModule(); //This can be null Module varDeclModule = varDecl.getModule(); //This can be null
.error(loc, "variable `%s` is used as a type", mtype.toChars()); .error(loc, "variable `%s` is used as a type", mtype.toChars());
//Check for null to avoid https://issues.dlang.org/show_bug.cgi?id=22574 //Check for null to avoid https://issues.dlang.org/show_bug.cgi?id=22574
if ((varDeclModule !is null) && varDeclModule != sc._module) // variable is imported if ((varDeclModule !is null) && varDeclModule != sc._module) // variable is imported
{ {
const(Loc) varDeclModuleImportLoc = varDeclModule.getLoc();
.errorSupplemental( .errorSupplemental(
varDeclModuleImportLoc, varDeclModule.loc,
"variable `%s` is imported here from: `%s`", "variable `%s` is imported here from: `%s`",
varDecl.toChars, varDecl.toChars,
varDeclModule.toPrettyChars, varDeclModule.toPrettyChars,
); );
} }
.errorSupplemental(varDeclLoc, "variable `%s` is declared here", varDecl.toChars); .errorSupplemental(varDecl.loc, "variable `%s` is declared here", varDecl.toChars);
} }
else else
.error(loc, "`%s` is used as a type", mtype.toChars()); .error(loc, "`%s` is used as a type", mtype.toChars());

View file

@ -1,8 +1,9 @@
/* /*
TEST_OUTPUT: TEST_OUTPUT:
--- ---
fail_compilation/trait_loc_err.d(13): Error: can only get the location of a symbol, not `trait_loc_err` fail_compilation/trait_loc_err.d(14): Error: can only get the location of a symbol, not `trait_loc_err`
fail_compilation/trait_loc_err.d(14): Error: can only get the location of a symbol, not `stdc` fail_compilation/trait_loc_err.d(15): Error: can only get the location of a symbol, not `core.stdc`
fail_compilation/trait_loc_err.d(16): Error: can only get the location of a symbol, not `core.stdc.stdio`
--- ---
*/ */
module trait_loc_err; module trait_loc_err;
@ -12,4 +13,5 @@ void main()
{ {
__traits(getLocation, __traits(parent, main)); __traits(getLocation, __traits(parent, main));
__traits(getLocation, __traits(parent, core.stdc.stdio)); __traits(getLocation, __traits(parent, core.stdc.stdio));
__traits(getLocation, core.stdc.stdio);
} }