Skip to content

Commit 3a7d0ab

Browse files
committed
elided_named_lifetimes: get rid of crate_node_id
1 parent f0c9cec commit 3a7d0ab

File tree

4 files changed

+74
-44
lines changed

4 files changed

+74
-44
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,6 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
699699

700700
/// Count the number of places a lifetime is used.
701701
lifetime_uses: FxHashMap<LocalDefId, LifetimeUseSet>,
702-
703-
/// We need some "real" `NodeId` to emit
704-
/// [`elided_named_lifetimes`](lint::builtin::ELIDED_NAMED_LIFETIMES).
705-
/// See comments in [`MissingLifetime::id_if_exists_in_source_or`].
706-
crate_node_id: NodeId,
707702
}
708703

709704
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -1322,10 +1317,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
13221317
}
13231318

13241319
impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1325-
fn new(
1326-
resolver: &'b mut Resolver<'a, 'tcx>,
1327-
krate: &Crate,
1328-
) -> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1320+
fn new(resolver: &'b mut Resolver<'a, 'tcx>) -> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
13291321
// During late resolution we only track the module component of the parent scope,
13301322
// although it may be useful to track other components as well for diagnostics.
13311323
let graph_root = resolver.graph_root;
@@ -1348,7 +1340,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
13481340
// errors at module scope should always be reported
13491341
in_func_body: false,
13501342
lifetime_uses: Default::default(),
1351-
crate_node_id: krate.id,
13521343
}
13531344
}
13541345

@@ -1573,7 +1564,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15731564
}
15741565

15751566
if ident.name == kw::UnderscoreLifetime {
1576-
return self.resolve_anonymous_lifetime(lifetime, false);
1567+
return self.resolve_anonymous_lifetime(lifetime, lifetime.id, false);
15771568
}
15781569

15791570
let mut lifetime_rib_iter = self.lifetime_ribs.iter().rev();
@@ -1676,13 +1667,23 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16761667
}
16771668

16781669
#[instrument(level = "debug", skip(self))]
1679-
fn resolve_anonymous_lifetime(&mut self, lifetime: &Lifetime, elided: bool) {
1670+
fn resolve_anonymous_lifetime(
1671+
&mut self,
1672+
lifetime: &Lifetime,
1673+
id_for_lint: NodeId,
1674+
elided: bool,
1675+
) {
16801676
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
16811677

16821678
let kind =
16831679
if elided { MissingLifetimeKind::Ampersand } else { MissingLifetimeKind::Underscore };
1684-
let missing_lifetime =
1685-
MissingLifetime { id: lifetime.id, span: lifetime.ident.span, kind, count: 1 };
1680+
let missing_lifetime = MissingLifetime {
1681+
id: lifetime.id,
1682+
span: lifetime.ident.span,
1683+
kind,
1684+
count: 1,
1685+
id_for_lint,
1686+
};
16861687
let elision_candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
16871688
for (i, rib) in self.lifetime_ribs.iter().enumerate().rev() {
16881689
debug!(?rib.kind);
@@ -1810,7 +1811,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
18101811
LifetimeRes::ElidedAnchor { start: id, end: NodeId::from_u32(id.as_u32() + 1) },
18111812
LifetimeElisionCandidate::Ignore,
18121813
);
1813-
self.resolve_anonymous_lifetime(&lt, true);
1814+
self.resolve_anonymous_lifetime(&lt, anchor_id, true);
18141815
}
18151816

18161817
#[instrument(level = "debug", skip(self))]
@@ -1926,6 +1927,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
19261927
};
19271928
let missing_lifetime = MissingLifetime {
19281929
id: node_ids.start,
1930+
id_for_lint: segment_id,
19291931
span: elided_lifetime_span,
19301932
kind,
19311933
count: expected_lifetimes,
@@ -2051,32 +2053,27 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20512053
}
20522054

20532055
match candidate {
2054-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { span: elided, .. }) => {
2056+
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
20552057
debug_assert_eq!(id, missing.id);
20562058
match res {
20572059
LifetimeRes::Static { suppress_elision_warning } => {
20582060
if !suppress_elision_warning {
20592061
self.r.lint_buffer.buffer_lint(
20602062
lint::builtin::ELIDED_NAMED_LIFETIMES,
2061-
missing.id_if_exists_in_source_or(self.crate_node_id),
2063+
missing.id_for_lint,
20622064
missing.span,
2063-
BuiltinLintDiag::ElidedIsStatic { elided },
2065+
BuiltinLintDiag::ElidedIsStatic { elided: missing.span },
20642066
);
20652067
}
20662068
}
2067-
LifetimeRes::Param { param, binder } => {
2069+
LifetimeRes::Param { param, binder: _ } => {
20682070
let tcx = self.r.tcx();
20692071
self.r.lint_buffer.buffer_lint(
20702072
lint::builtin::ELIDED_NAMED_LIFETIMES,
2071-
// It should be possible to use `self.crate_node_id`
2072-
// or `param`'s `NodeId` here as a fallback instead of the `binder`,
2073-
// but `binder` sounds like a more appropriate place than the crate,
2074-
// and to convert `param` from `LocalDefId` to `NodeId`,
2075-
// we would have to do some additional work.
2076-
missing.id_if_exists_in_source_or(binder),
2073+
missing.id_for_lint,
20772074
missing.span,
20782075
BuiltinLintDiag::ElidedIsParam {
2079-
elided,
2076+
elided: missing.span,
20802077
param: (tcx.item_name(param.into()), tcx.source_span(param)),
20812078
},
20822079
);
@@ -4990,7 +4987,7 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
49904987
impl<'a, 'tcx> Resolver<'a, 'tcx> {
49914988
pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) {
49924989
visit::walk_crate(&mut ItemInfoCollector { r: self }, krate);
4993-
let mut late_resolution_visitor = LateResolutionVisitor::new(self, krate);
4990+
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
49944991
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
49954992
visit::walk_crate(&mut late_resolution_visitor, krate);
49964993
for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,19 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
102102
pub(super) struct MissingLifetime {
103103
/// Used to overwrite the resolution with the suggestion, to avoid cascading errors.
104104
pub id: NodeId,
105-
/// Where to suggest adding the lifetime.
106-
pub span: Span,
107-
/// How the lifetime was introduced, to have the correct space and comma.
108-
pub kind: MissingLifetimeKind,
109-
/// Number of elided lifetimes, used for elision in path.
110-
pub count: usize,
111-
}
112-
113-
impl MissingLifetime {
114105
/// As we cannot yet emit lints in this crate and have to buffer them instead,
115106
/// we need to associate each lint with some `NodeId`,
116107
/// however for some `MissingLifetime`s their `NodeId`s are "fake",
117108
/// in a sense that they are temporary and not get preserved down the line,
118109
/// which means that the lints for those nodes will not get emitted.
119110
/// To combat this, we can try to use some other `NodeId`s as a fallback option.
120-
pub(super) fn id_if_exists_in_source_or(self, fallback: NodeId) -> NodeId {
121-
match self.kind {
122-
MissingLifetimeKind::Underscore => self.id,
123-
MissingLifetimeKind::Ampersand
124-
| MissingLifetimeKind::Brackets
125-
| MissingLifetimeKind::Comma => fallback,
126-
}
127-
}
111+
pub id_for_lint: NodeId,
112+
/// Where to suggest adding the lifetime.
113+
pub span: Span,
114+
/// How the lifetime was introduced, to have the correct space and comma.
115+
pub kind: MissingLifetimeKind,
116+
/// Number of elided lifetimes, used for elision in path.
117+
pub count: usize,
128118
}
129119

130120
/// Description of the lifetimes appearing in a function parameter.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![allow(elided_named_lifetimes)]
2+
3+
#[warn(elided_named_lifetimes)]
4+
mod foo {
5+
fn bar(x: &'static u8) -> &u8 {
6+
//~^ WARNING elided lifetime has a name
7+
x
8+
}
9+
10+
#[deny(elided_named_lifetimes)]
11+
fn baz(x: &'static u8) -> &u8 {
12+
//~^ ERROR elided lifetime has a name
13+
x
14+
}
15+
}
16+
17+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: elided lifetime has a name
2+
--> $DIR/not-tied-to-crate.rs:5:31
3+
|
4+
LL | fn bar(x: &'static u8) -> &u8 {
5+
| ^ this elided lifetime gets resolved as `'static`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/not-tied-to-crate.rs:3:8
9+
|
10+
LL | #[warn(elided_named_lifetimes)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: elided lifetime has a name
14+
--> $DIR/not-tied-to-crate.rs:11:31
15+
|
16+
LL | fn baz(x: &'static u8) -> &u8 {
17+
| ^ this elided lifetime gets resolved as `'static`
18+
|
19+
note: the lint level is defined here
20+
--> $DIR/not-tied-to-crate.rs:10:12
21+
|
22+
LL | #[deny(elided_named_lifetimes)]
23+
| ^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 1 previous error; 1 warning emitted
26+

0 commit comments

Comments
 (0)