Skip to content

Emit error for unused target expression in glob delegations#157601

Open
aerooneqq wants to merge 4 commits into
rust-lang:mainfrom
aerooneqq:delegation-glob-useless-target-expr-error
Open

Emit error for unused target expression in glob delegations#157601
aerooneqq wants to merge 4 commits into
rust-lang:mainfrom
aerooneqq:delegation-glob-useless-target-expr-error

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

This PR emits error if unuse target expression is specified for glob delegation. Second part of #156798.

Part of #118212.
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 Jun 8, 2026
@rust-log-analyzer

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the delegation-glob-useless-target-expr-error branch from e6b6c5e to c967907 Compare June 8, 2026 08:36
Comment thread compiler/rustc_middle/src/hir/map.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/delegation.rs Outdated
@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 Jun 9, 2026
@aerooneqq

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 Jun 9, 2026
generated_target_expr,
glob_id: {
let expn_id = self.tcx.expn_that_defined(self.owner.def_id);
if expn_id == ExpnId::root() { None } else { Some(expn_id) }

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if expn_id == ExpnId::root() { None } else { Some(expn_id) }
(delegation_source == DelegationSource::Glob).then(|| self.tcx.expn_that_defined(self.owner.def_id))

Right now we are still not checking that the delegation came from a glob, and not from some random other macro.

View changes since the review

let mut delegations_by_expansions = FxIndexMap::default();

for &id in tcx.resolutions(()).delegation_infos.keys() {
if let Some(info) = tcx.hir_opt_delegation_info(id)

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if let Some(info) = tcx.hir_opt_delegation_info(id)
let info = tcx.hir_delegation_info(id);

Can it ever fail here? We are only processing delegations.

View changes since the review

use rustc_span::Span;

pub fn check_glob_delegations_target_expr(tcx: TyCtxt<'_>) {
let mut delegations_by_expansions = FxIndexMap::default();

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
let mut delegations_by_expansions = FxIndexMap::default();
let mut delegations_by_glob_id = FxIndexMap::default();

The glob id being and expansion is not a detail that is important here.

View changes since the review

}
}

for (_, state) in delegations_by_expansions {

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (_, state) in delegations_by_expansions {
for (_, (removed_target_expr, span)) in delegations_by_expansions {

View changes since the review

{
let entry = delegations_by_expansions.entry(expn_id);
let entry = entry.or_insert_with(|| (true, tcx.def_span(id)));
entry.0 &= !info.generated_target_expr;

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style nit: all the 3 statements can be merged into a single one.

View changes since the review

pub self_ty_id: Option<HirId>,
pub propagate_self_ty: bool,
pub generated_target_expr: bool,
pub glob_id: Option<ExpnId>,

@petrochenkov petrochenkov Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub glob_id: Option<ExpnId>,
pub glob_id: Option<(ExpnId, /*removed_target_expr*/ bool)>,

The boolean flag is irrelevant for non-glob delegations.

View changes since the review

@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 Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants