Skip to content

Privacy: move macros handling to early stage#156500

Open
Bryanskiy wants to merge 1 commit into
rust-lang:mainfrom
Bryanskiy:macros_vis
Open

Privacy: move macros handling to early stage#156500
Bryanskiy wants to merge 1 commit into
rust-lang:mainfrom
Bryanskiy:macros_vis

Conversation

@Bryanskiy
Copy link
Copy Markdown
Contributor

@Bryanskiy Bryanskiy commented May 12, 2026

The patch moves effective visibility computation for macros from rustc_privacy to rustc_resolve. It will enable this optimization: #156228.

However, I found some problems with macro handling while I was doing this. The current implementation was written ~6 years ago and checks the reachability of a definition from a macro by nominal visibility. In general this is incorrect.

For example, in the current implementation modules are not traversed if their nominal visibility is less then the nominal visibility of a module defining macro:

DefKind::Mod => {
if vis.is_accessible_from(module, self.tcx) {
self.update_macro_reachable(
LocalModDefId::new_unchecked(def_id),
module,
macro_ev,
);
}
}

As a result, in order to compile code like tests/ui/definition-reachable/auxiliary/field-method-macro.rs. we have to additionally traverse types of adt fields:

DefKind::Struct | DefKind::Union => {
// While structs and unions have type privacy, their fields do not.
let struct_def = self.tcx.adt_def(def_id);
for field in &struct_def.non_enum_variant().fields {
let def_id = field.did.expect_local();
let field_vis = self.tcx.local_visibility(def_id);
if field_vis.is_accessible_from(module, self.tcx) {
self.reach(def_id, macro_ev).ty();
}
}
}

This is a hack and the proper solution would be to check definitions with EffectiveVisibilities::is_reachable. I haven’t done this yet, as it would start to trigger many lints as more items become reachable. I think it’s better to leave the change to another commit.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 12, 2026
Comment thread compiler/rustc_middle/src/ty/mod.rs Outdated
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
pub expn_that_defined: UnordMap<LocalDefId, ExpnId>,
pub effective_visibilities: EffectiveVisibilities,
pub macro_reachable_adts: FxIndexMap<LocalDefId, EffectiveVisibility>,
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

This exists purely for bug-compatibility with the old algorithm, right?
Could you add a comment about this?

View changes since the review

Comment thread compiler/rustc_privacy/src/lib.rs Outdated
visitor.changed = false;
}

for macro_reachable_adt in tcx.resolutions(()).macro_reachable_adts.iter() {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

Suggested change
for macro_reachable_adt in tcx.resolutions(()).macro_reachable_adts.iter() {
for &(adt_def_id, adt_eff_vis) in &tcx.resolutions(()).macro_reachable_adts {

View changes since the review

.def_effective_visibilities
.effective_vis(local_def_id)
.filter(|ev| ev.public_at_level().is_some())
.cloned()
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

Suggested change
.cloned()
.copied()

View changes since the review

continue;
};

if !decl.is_import()
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

This is a change compared to the previous version, module_children_local include imports.
I suspect that we can remove this condition and update imports by their DefIds here too.

View changes since the review

DefKind::Struct | DefKind::Union => {
self.r.macro_reachable_adts.insert(def_id, macro_ev);
}
_ => {}
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

The behavior is different from the previous version.

DefKind::Const { .. } | DefKind::Static { .. } | DefKind::TraitAlias | DefKind::TyAlias are not processed as before.
DefKind::Macro is also not processed recursively.

View changes since the review

Copy link
Copy Markdown
Contributor Author

@Bryanskiy Bryanskiy May 12, 2026

Choose a reason for hiding this comment

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

For all def_kind there was update call in the beginning of update_macro_reachable_def:

self.update(def_id, macro_ev, Level::Reachable);

so second update doesn't make sense at all.

self.update_macro_reachable(def_id, module, macro_ev);
}
DefKind::Struct | DefKind::Union => {
self.r.macro_reachable_adts.insert(def_id, macro_ev);
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 12, 2026

Choose a reason for hiding this comment

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

What happens if we reach this case several times with different macro_evs?
The last one will win, but that's not how the update operation worked before.
Can macro_reachable_adts be turned into a proper EffectiveVisibilities table instead?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The last one will win, but that's not how the update operation worked before.

Why not? Last macro_ev is the max possible macro_ev because it follows update logic. For fields in rustc_privacy effective visibility is updated at once with max possible value.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 12, 2026
Comment thread compiler/rustc_privacy/src/lib.rs
@Bryanskiy
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants