Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a location to indirect imports. #4098

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Jul 1, 2024

By adding an ImportDecl instruction, this creates something that can be referenced through ImportIRInst. packages/no_prelude/implicit_imports_entities.carbon is getting a test of this (import_conflict and import_conflict_reverse).

Also re-packs ImportIR from 24 bytes to 16 on 64-bit, since I'm touching everywhere that makes one anyways.

Copy link
Contributor

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines +284 to +289
auto import_ir_inst_id = context.import_ir_insts().Add(
{.ir_id = SemIR::ImportIRId::ApiForImpl,
.inst_id = api_imports->import_decl_id});
import_decl_id = context.AddInst<SemIR::ImportDecl>(
import_ir_inst_id, {.package_id = SemIR::NameId::ForIdentifier(
api_imports_entry.first)});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered making this an ImportRef naming the API's file's ImportDecl, instead of an ImportDecl that's a copy of the one from the API file? I'm not really sure which makes more sense here and not pushing for one over the other, but my first instinct was that this is naming an import in a different file so maybe ImportRef might make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, but right now we're only using ImportRef for entities. That is, ImportRef points at a typed entity in name lookup.

While imports from other packages can create a namespace that's in name lookup, the import declaration itself isn't in name lookup, and the current package's imports don't have any name lookup data. So it felt like this approach, closer to Namespace handling than entity handling, was a better fit.

Also, presumably it'd be an ImportRefLoaded (ImportRefUnloaded feels a little odd to me since we do load the import), and that ImportRefLoaded should be linked to a resolved ImportDecl and have type information. Building that would require defining a type for ImportDecl, and I'm not sure what that would be (maybe engaging in a more circular definition of ImportDecl as a back-reference to the Namespace?)

Comment on lines 675 to 676
case ImportDecl::Kind:
CARBON_FATAL() << "Not valid in an expression.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an ExprCategory::NotExpr for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, although after debugging #4089 I have apprehension about this: it feels like it makes it really difficult to debug issues, that we don't do more to prevent calls here.

// The imported IR.
const File* sem_ir;
// The `import` declaration.
InstId decl_id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe import_decl_id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The equivalents I would draw would be decl_id on AssociatedEntity, or on non-instruction types, Function and Generic.

};

static_assert(sizeof(ImportIR) == 16, "Unexpected size");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size will be 12 for a 32-bit compilation. If it's worth checking this, maybe sizeof(ImportIR) == 8 + sizeof(void*).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (though uintptr_t I think is supposed to be preferred now?).

I did feel it's worth checking because it had been 24 before the re-pack (this is why I reordered fields).

if (inst.package_id.is_valid()) {
add_inst_name_id(inst.package_id, ".import");
} else {
add_inst_name("<default>.import");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_inst_name("<default>.import");
add_inst_name("default.import");

We generally don't use <...> as part of valid names (eg, after a %), only at the top level for error cases (and for builtin types).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jonmeow jonmeow added this pull request to the merge queue Jul 3, 2024
Merged via the queue into carbon-language:trunk with commit 5ebcbae Jul 3, 2024
7 checks passed
@jonmeow jonmeow deleted the import-loc branch July 3, 2024 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants