Skip to content

Commit b07d42b

Browse files
committed
Address review comments
1 parent 05467ca commit b07d42b

File tree

4 files changed

+32
-53
lines changed

4 files changed

+32
-53
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_data_structures::fx::FxHashSet;
4848
use rustc_errors::ErrorGuaranteed;
4949
use rustc_hir::Target;
5050
use rustc_hir::attrs::{AttributeKind, InlineAttr};
51-
use rustc_hir::def_id::{DefId, LocalDefId};
51+
use rustc_hir::def_id::DefId;
5252
use rustc_middle::span_bug;
5353
use rustc_middle::ty::{Asyncness, DelegationFnSigAttrs, ResolverAstLowering};
5454
use rustc_span::symbol::kw;
@@ -125,7 +125,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
125125
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
126126

127127
let sig_id = self.get_delegation_sig_id(
128-
self.get_delegation_sig_node_id(&self.local_def_id(item_id)),
128+
self.resolver.delegation_sig_resolution_nodes[&self.local_def_id(item_id)],
129129
span,
130130
);
131131

@@ -261,7 +261,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
261261
&& let Some(local_id) = def_id.as_local()
262262
&& !self.resolver.delegation_fn_sigs.contains_key(&local_id)
263263
{
264-
node_id = self.get_delegation_sig_node_id(&local_id);
264+
node_id = self.resolver.delegation_sig_resolution_nodes[&local_id];
265265
if visited.contains(&node_id) {
266266
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
267267
// entity, in this case emit an error.
@@ -274,42 +274,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
274274
continue;
275275
}
276276

277-
// DefId is either None or is from non-local crate, fallback to the original routine.
278-
return self.def_id_or_guaranteed_err(def_id, node_id, span);
277+
return def_id.ok_or_else(|| {
278+
self.tcx.dcx().span_delayed_bug(
279+
span,
280+
format!(
281+
"LoweringContext: couldn't resolve node {:?} in delegation item",
282+
node_id
283+
),
284+
)
285+
});
279286
}
280287
}
281288

282-
fn get_delegation_sig_node_id(&self, local_id: &LocalDefId) -> NodeId {
283-
*self
284-
.resolver
285-
.delegation_sig_resolution_nodes
286-
.get(local_id)
287-
.expect("All delegations should have signature resolution NodeId")
288-
}
289-
290289
fn opt_get_partial_res_id(&self, node_id: NodeId) -> Option<DefId> {
291290
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
292291
}
293292

294-
fn def_id_or_guaranteed_err(
295-
&self,
296-
def_id: Option<DefId>,
297-
node_id: NodeId,
298-
span: Span,
299-
) -> Result<DefId, ErrorGuaranteed> {
300-
def_id.ok_or_else(|| {
301-
self.tcx.dcx().span_delayed_bug(
302-
span,
303-
format!("LoweringContext: couldn't resolve node {:?} in delegation item", node_id),
304-
)
305-
})
306-
}
307-
308-
fn get_resolution_id(&mut self, node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
309-
let def_id = self.opt_get_partial_res_id(node_id);
310-
self.def_id_or_guaranteed_err(def_id, node_id, span)
311-
}
312-
313293
fn lower_delegation_generics(&mut self, span: Span) -> &'hir hir::Generics<'hir> {
314294
self.arena.alloc(hir::Generics {
315295
params: &[],
@@ -539,8 +519,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
539519
delegation.path.segments.iter().rev().skip(1).any(|segment| segment.args.is_some());
540520

541521
let call = if self
542-
.get_resolution_id(delegation.id, span)
543-
.and_then(|def_id| Ok(self.is_method(def_id, span)))
522+
.opt_get_partial_res_id(delegation.id)
523+
.and_then(|def_id| Some(self.is_method(def_id, span)))
544524
.unwrap_or_default()
545525
&& delegation.qself.is_none()
546526
&& !has_generic_args

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ pub struct ResolverAstLowering {
220220

221221
/// Information about functions signatures for delegation items expansion
222222
pub delegation_fn_sigs: LocalDefIdMap<DelegationFnSig>,
223-
// NodeIds for delegation signature resolution
223+
// NodeIds (either delegation.id or item_id in case of a trait impl) for signature resolution,
224+
// for details see https://github.com/rust-lang/rust/issues/118212#issuecomment-2160686914
224225
pub delegation_sig_resolution_nodes: LocalDefIdMap<ast::NodeId>,
225226
}
226227

tests/ui/delegation/recursive_delegation_errors.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![feature(fn_delegation)]
22
#![allow(incomplete_features)]
3-
#![allow(dead_code)]
4-
#![allow(unused_variables)]
53

64

75
mod first_mod {

tests/ui/delegation/recursive_delegation_errors.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
11
error: failed to resolve delegation callee
2-
--> $DIR/recursive_delegation_errors.rs:8:11
2+
--> $DIR/recursive_delegation_errors.rs:6:11
33
|
44
LL | reuse foo;
55
| ^^^
66

77
error: encountered a cycle during delegation signature resolution
8-
--> $DIR/recursive_delegation_errors.rs:13:11
8+
--> $DIR/recursive_delegation_errors.rs:11:11
99
|
1010
LL | reuse foo as bar;
1111
| ^^^
1212

1313
error: encountered a cycle during delegation signature resolution
14-
--> $DIR/recursive_delegation_errors.rs:15:11
14+
--> $DIR/recursive_delegation_errors.rs:13:11
1515
|
1616
LL | reuse bar as foo;
1717
| ^^^
1818

1919
error: encountered a cycle during delegation signature resolution
20-
--> $DIR/recursive_delegation_errors.rs:21:11
20+
--> $DIR/recursive_delegation_errors.rs:19:11
2121
|
2222
LL | reuse foo as foo1;
2323
| ^^^
2424

2525
error: encountered a cycle during delegation signature resolution
26-
--> $DIR/recursive_delegation_errors.rs:23:11
26+
--> $DIR/recursive_delegation_errors.rs:21:11
2727
|
2828
LL | reuse foo1 as foo2;
2929
| ^^^^
3030

3131
error: encountered a cycle during delegation signature resolution
32-
--> $DIR/recursive_delegation_errors.rs:25:11
32+
--> $DIR/recursive_delegation_errors.rs:23:11
3333
|
3434
LL | reuse foo2 as foo3;
3535
| ^^^^
3636

3737
error: encountered a cycle during delegation signature resolution
38-
--> $DIR/recursive_delegation_errors.rs:27:11
38+
--> $DIR/recursive_delegation_errors.rs:25:11
3939
|
4040
LL | reuse foo3 as foo4;
4141
| ^^^^
4242

4343
error: encountered a cycle during delegation signature resolution
44-
--> $DIR/recursive_delegation_errors.rs:29:11
44+
--> $DIR/recursive_delegation_errors.rs:27:11
4545
|
4646
LL | reuse foo4 as foo5;
4747
| ^^^^
4848

4949
error: encountered a cycle during delegation signature resolution
50-
--> $DIR/recursive_delegation_errors.rs:31:11
50+
--> $DIR/recursive_delegation_errors.rs:29:11
5151
|
5252
LL | reuse foo5 as foo;
5353
| ^^^^
5454

5555
error: encountered a cycle during delegation signature resolution
56-
--> $DIR/recursive_delegation_errors.rs:37:22
56+
--> $DIR/recursive_delegation_errors.rs:35:22
5757
|
5858
LL | reuse Trait::foo as bar;
5959
| ^^^
6060

6161
error: encountered a cycle during delegation signature resolution
62-
--> $DIR/recursive_delegation_errors.rs:39:22
62+
--> $DIR/recursive_delegation_errors.rs:37:22
6363
|
6464
LL | reuse Trait::bar as foo;
6565
| ^^^
6666

6767
error: encountered a cycle during delegation signature resolution
68-
--> $DIR/recursive_delegation_errors.rs:45:30
68+
--> $DIR/recursive_delegation_errors.rs:43:30
6969
|
7070
LL | reuse super::fifth_mod::{bar as foo, foo as bar};
7171
| ^^^
7272

7373
error: encountered a cycle during delegation signature resolution
74-
--> $DIR/recursive_delegation_errors.rs:45:42
74+
--> $DIR/recursive_delegation_errors.rs:43:42
7575
|
7676
LL | reuse super::fifth_mod::{bar as foo, foo as bar};
7777
| ^^^
7878

7979
error: encountered a cycle during delegation signature resolution
80-
--> $DIR/recursive_delegation_errors.rs:50:27
80+
--> $DIR/recursive_delegation_errors.rs:48:27
8181
|
8282
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
8383
| ^^^
8484

8585
error: encountered a cycle during delegation signature resolution
86-
--> $DIR/recursive_delegation_errors.rs:50:39
86+
--> $DIR/recursive_delegation_errors.rs:48:39
8787
|
8888
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
8989
| ^^^
9090

9191
error: encountered a cycle during delegation signature resolution
92-
--> $DIR/recursive_delegation_errors.rs:50:51
92+
--> $DIR/recursive_delegation_errors.rs:48:51
9393
|
9494
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
9595
| ^^^

0 commit comments

Comments
 (0)