Skip to content

Commit bda2e5c

Browse files
committed
Some fixes + reformat
1 parent 297735f commit bda2e5c

File tree

8 files changed

+101
-48
lines changed

8 files changed

+101
-48
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ ast_lowering_underscore_expr_lhs_assign =
175175
176176
ast_lowering_union_default_field_values = unions cannot have default field values
177177
178+
ast_lowering_unresolved_delegation_callee = failed to resolve delegation callee
178179
ast_lowering_unstable_inline_assembly = inline assembly is not stable yet on this architecture
179180
ast_lowering_unstable_inline_assembly_label_operand_with_outputs =
180181
using both label and output operands for inline assembly is unstable
@@ -186,5 +187,3 @@ ast_lowering_yield = yield syntax is experimental
186187
ast_lowering_yield_in_closure =
187188
`yield` can only be used in `#[coroutine]` closures, or `gen` blocks
188189
.suggestion = use `#[coroutine]` to make this closure a coroutine
189-
190-
ast_lowering_unresolved_delegation_callee = failed to resolve delegation callee

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ 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;
51+
use rustc_hir::def_id::{DefId, LocalDefId};
5252
use rustc_middle::span_bug;
53-
use rustc_middle::ty::{Asyncness, DelegationFnSigAttrs, ResolverAstLowering};
53+
use rustc_middle::ty::{
54+
Asyncness, DelegationFnSigAttrs, DelegationResolutionInfo, ResolverAstLowering,
55+
};
5456
use rustc_span::symbol::kw;
5557
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
5658
use {rustc_ast as ast, rustc_hir as hir};
@@ -124,14 +126,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
124126
) -> DelegationResults<'hir> {
125127
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
126128

127-
let sig_id = self.resolve_callee_sig(
128-
self.resolver
129-
.delegation_resolution_info
130-
.get(&self.local_def_id(item_id))
131-
.unwrap()
132-
.sig_resolution_id,
133-
span,
134-
);
129+
let sig_id = self
130+
.delegation_res_info_or_err(&self.local_def_id(item_id), span)
131+
.map(|info| self.get_delegation_sig_id(info.sig_resolution_id, span))
132+
.flatten();
135133

136134
match sig_id {
137135
Ok(sig_id) => {
@@ -246,7 +244,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
246244
}
247245
}
248246

249-
fn resolve_callee_sig(
247+
fn get_delegation_sig_id(
250248
&self,
251249
mut node_id: NodeId,
252250
span: Span,
@@ -265,31 +263,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
265263
&& let Some(local_id) = def_id.as_local()
266264
&& !self.resolver.delegation_fn_sigs.contains_key(&local_id)
267265
{
268-
if let Some(info) = self.resolver.delegation_resolution_info.get(&local_id) {
269-
node_id = info.sig_resolution_id;
270-
if visited.contains(&node_id) {
271-
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
272-
// entity, in this case emit an error.
273-
return Err(self.dcx().emit_err(UnresolvedDelegationCallee { span }));
274-
}
275-
276-
continue;
277-
} else {
278-
// The LocalDefId for some reason refers not to another delegation, in this case
279-
// we will emit a bug (not an error), as we have to catch such cases on the resolve stage,
280-
// during smart_resolve_path in resolve_delegation.
281-
return Err(self.tcx.dcx().span_delayed_bug(span, format!(
282-
"There is no information about delegation resolution node id for LocalDefId {:?}",
283-
local_id
284-
)));
266+
node_id = self.delegation_res_info_or_err(&local_id, span)?.sig_resolution_id;
267+
if visited.contains(&node_id) {
268+
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
269+
// entity, in this case emit an error.
270+
return Err(self.dcx().emit_err(UnresolvedDelegationCallee { span }));
285271
}
272+
273+
continue;
286274
}
287275

288276
// DefId is either None or is from non-local crate, fallback to the original routine.
289277
return self.def_id_or_guaranteed_err(def_id, node_id, span);
290278
}
291279
}
292280

281+
fn delegation_res_info_or_err(
282+
&self,
283+
local_id: &LocalDefId,
284+
span: Span,
285+
) -> Result<&DelegationResolutionInfo, ErrorGuaranteed> {
286+
self.resolver.delegation_resolution_info.get(local_id).ok_or_else(|| {
287+
self.tcx.dcx().span_delayed_bug(span, format!(
288+
"There is no information about delegation resolution node id for LocalDefId {:?}",
289+
local_id
290+
))
291+
})
292+
}
293+
293294
fn opt_get_partial_res_id(&self, node_id: NodeId) -> Option<DefId> {
294295
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
295296
}
@@ -326,8 +327,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
326327
// Function parameter count, including C variadic `...` if present.
327328
fn param_count(&self, sig_id: DefId) -> (usize, bool /*c_variadic*/) {
328329
if let Some(local_sig_id) = sig_id.as_local() {
329-
// Map may be filled incorrectly due to recursive delegation.
330-
// Error will be emitted later during HIR ty lowering.
331330
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
332331
Some(sig) => (sig.param_count, sig.c_variadic),
333332
None => (0, false),

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,6 @@ fn check_constraints<'tcx>(
401401
}));
402402
};
403403

404-
if let Some(local_sig_id) = sig_id.as_local()
405-
&& tcx.hir_opt_delegation_sig_id(local_sig_id).is_some()
406-
{
407-
emit("recursive delegation is not supported yet");
408-
}
409-
410404
if tcx.fn_sig(sig_id).skip_binder().skip_binder().c_variadic {
411405
// See issue #127443 for explanation.
412406
emit("delegation to C-variadic functions is not allowed");

compiler/rustc_resolve/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ use rustc_middle::middle::privacy::EffectiveVisibilities;
7070
use rustc_middle::query::Providers;
7171
use rustc_middle::span_bug;
7272
use rustc_middle::ty::{
73-
self, DelegationFnSig, DelegationResolutionInfo, Feed, MainDefinition, RegisteredTools, ResolverAstLowering, ResolverGlobalCtxt, TyCtxt, TyCtxtFeed, Visibility
73+
self, DelegationFnSig, DelegationResolutionInfo, Feed, MainDefinition, RegisteredTools,
74+
ResolverAstLowering, ResolverGlobalCtxt, TyCtxt, TyCtxtFeed, Visibility,
7475
};
7576
use rustc_query_system::ich::StableHashingContext;
7677
use rustc_session::config::CrateType;
@@ -1823,7 +1824,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18231824
lifetime_elision_allowed: self.lifetime_elision_allowed,
18241825
lint_buffer: Steal::new(self.lint_buffer),
18251826
delegation_fn_sigs: self.delegation_fn_sigs,
1826-
delegation_resolution_info: self.delegation_resolution_info
1827+
delegation_resolution_info: self.delegation_resolution_info,
18271828
};
18281829
ResolverOutputs { global_ctxt, ast_lowering }
18291830
}

tests/ui/delegation/auxiliary/recursive-delegation-aux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ fn foo() {
66
}
77

88
reuse foo as bar;
9-
pub reuse bar as goo;
9+
pub reuse bar as goo;

tests/ui/delegation/recursive_delegation_errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ mod fourth_mod {
4141
}
4242
}
4343

44+
mod fifth_mod {
45+
reuse super::fifth_mod::{bar as foo, foo as bar};
46+
//~^ ERROR failed to resolve delegation callee
47+
//~| ERROR failed to resolve delegation callee
48+
49+
trait GlobReuse {
50+
reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
51+
//~^ ERROR failed to resolve delegation callee
52+
//~| ERROR failed to resolve delegation callee
53+
//~| ERROR failed to resolve delegation callee
54+
}
55+
}
56+
4457
fn main() {}

tests/ui/delegation/recursive_delegation_errors.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,35 @@ error: failed to resolve delegation callee
6464
LL | reuse Trait::bar as foo;
6565
| ^^^
6666

67-
error: aborting due to 11 previous errors
67+
error: failed to resolve delegation callee
68+
--> $DIR/recursive_delegation_errors.rs:45:30
69+
|
70+
LL | reuse super::fifth_mod::{bar as foo, foo as bar};
71+
| ^^^
72+
73+
error: failed to resolve delegation callee
74+
--> $DIR/recursive_delegation_errors.rs:45:42
75+
|
76+
LL | reuse super::fifth_mod::{bar as foo, foo as bar};
77+
| ^^^
78+
79+
error: failed to resolve delegation callee
80+
--> $DIR/recursive_delegation_errors.rs:50:27
81+
|
82+
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
83+
| ^^^
84+
85+
error: failed to resolve delegation callee
86+
--> $DIR/recursive_delegation_errors.rs:50:39
87+
|
88+
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
89+
| ^^^
90+
91+
error: failed to resolve delegation callee
92+
--> $DIR/recursive_delegation_errors.rs:50:51
93+
|
94+
LL | reuse GlobReuse::{foo as bar, bar as goo, goo as foo};
95+
| ^^^
96+
97+
error: aborting due to 16 previous errors
6898

tests/ui/delegation/recursive_delegation_pass.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ mod first_mod {
1616
}
1717
}
1818

19-
reuse to_reuse::foo;
20-
reuse foo as bar;
21-
reuse foo as bar1;
22-
reuse bar as goo;
23-
reuse goo as koo;
24-
reuse koo as too;
19+
mod single_reuse {
20+
reuse crate::first_mod::to_reuse::foo;
21+
reuse foo as bar;
22+
reuse foo as bar1;
23+
reuse bar as goo;
24+
reuse goo as koo;
25+
reuse koo as too;
26+
}
27+
28+
mod glob_reuse {
29+
reuse super::to_reuse::{foo as bar, foo as bar1} { self }
30+
reuse super::glob_reuse::{bar as goo, goo as koo, koo as too} { self }
31+
}
2532
}
2633

2734
mod second_mod {
@@ -31,6 +38,16 @@ mod second_mod {
3138
reuse T::bar as goo;
3239
reuse T::goo as poo;
3340
}
41+
42+
trait TGlob {
43+
fn xd(&self) -> &Self;
44+
fn foo1(&self);
45+
fn foo2(&self);
46+
fn foo3(&self);
47+
fn foo4(&self);
48+
49+
reuse TGlob::{foo1 as bar1, foo3 as bar3, bar1 as bar11, bar11 as bar111} { self.xd() }
50+
}
3451
}
3552

3653
mod third_mod {

0 commit comments

Comments
 (0)