Skip to content

Commit b19da8b

Browse files
committed
Remove error reporting + move Result<...> in DelegationInfo
1 parent 4c2c364 commit b19da8b

38 files changed

Lines changed: 96 additions & 362 deletions

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use rustc_middle::span_bug;
5252
use rustc_middle::ty::{Asyncness, TyCtxt};
5353
use rustc_span::symbol::kw;
5454
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
55-
use smallvec::SmallVec;
5655

5756
use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
5857
use crate::errors::CycleInDelegationSignatureResolution;
@@ -122,42 +121,36 @@ pub fn delegations_resolutions(
122121
let delegation = ast_index[def_id].delegation().expect("processing delegations");
123122
let span = delegation.last_segment_span();
124123

125-
if let Some(res_result) = resolver.delegation_info(def_id) {
126-
let res = res_result.map(|info| check_for_cycles(tcx, info.resolution_id, span));
124+
if let Some(info) = resolver.delegation_info(def_id) {
125+
let res = info.resolution_id.map(|id| check_for_cycles(tcx, id, span).map(|_| id));
127126
result.insert(def_id, res.flatten());
128127
}
129128
}
130129

131130
result
132131
}
133132

134-
fn check_for_cycles(
135-
tcx: TyCtxt<'_>,
136-
mut def_id: DefId,
137-
span: Span,
138-
) -> Result<DefId, ErrorGuaranteed> {
133+
fn check_for_cycles(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
139134
let mut visited: FxHashSet<DefId> = Default::default();
140-
let mut path: SmallVec<[DefId; 1]> = Default::default();
141135

142136
let (resolver, _) = &*tcx.hir_crate(()).delayed_resolver.borrow();
143137

144138
loop {
145139
visited.insert(def_id);
146140

147-
path.push(def_id);
148-
149141
// If def_id is in local crate and it corresponds to another delegation
150142
// it means that we refer to another delegation as a callee, so in order to obtain
151143
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
152144
if let Some(local_id) = def_id.as_local()
153-
&& let Some(Ok(delegation_info)) = resolver.delegation_info(local_id)
145+
&& let Some(info) = resolver.delegation_info(local_id)
146+
&& let Ok(id) = info.resolution_id
154147
{
155-
def_id = delegation_info.resolution_id;
148+
def_id = id;
156149
if visited.contains(&def_id) {
157150
return Err(tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }));
158151
}
159152
} else {
160-
return Ok(path[0]);
153+
return Ok(());
161154
}
162155
}
163156
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use rustc_middle::span_bug;
6464
use rustc_middle::ty::{DelegationInfo, PerOwnerResolverData, ResolverAstLowering, TyCtxt};
6565
use rustc_session::errors::add_feature_diagnostics;
6666
use rustc_span::symbol::{Ident, Symbol, kw, sym};
67-
use rustc_span::{DUMMY_SP, DesugaringKind, ErrorGuaranteed, Span};
67+
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
6868
use smallvec::SmallVec;
6969
use thin_vec::ThinVec;
7070
use tracing::{debug, instrument, trace};
@@ -304,7 +304,7 @@ impl<'tcx> ResolverAstLowering<'tcx> {
304304
self.extra_lifetime_params_map.get(&id).map_or(&[], |v| &v[..])
305305
}
306306

307-
fn delegation_info(&self, id: LocalDefId) -> Option<Result<DelegationInfo, ErrorGuaranteed>> {
307+
fn delegation_info(&self, id: LocalDefId) -> Option<DelegationInfo> {
308308
self.delegation_infos.get(&id).copied()
309309
}
310310

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub struct ResolverAstLowering<'tcx> {
257257
pub lint_buffer: Steal<LintBuffer>,
258258

259259
// Information about delegations which is used when handling recursive delegations
260-
pub delegation_infos: LocalDefIdMap<Result<DelegationInfo, ErrorGuaranteed>>,
260+
pub delegation_infos: LocalDefIdMap<DelegationInfo>,
261261

262262
pub disambiguators: LocalDefIdMap<Steal<PerParentDisambiguatorState>>,
263263
}
@@ -269,7 +269,7 @@ pub struct DelegationInfo {
269269
/// Refers to the next element in a delegation resolution chain.
270270
/// Usually points to the final resolution, as most "chains" are just
271271
/// one step to a trait or an impl.
272-
pub resolution_id: DefId,
272+
pub resolution_id: Result<DefId, ErrorGuaranteed>,
273273
}
274274

275275
#[derive(Clone, Copy, Debug, StableHash)]

compiler/rustc_resolve/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,10 +1776,3 @@ pub(crate) enum UnusedImportsSugg {
17761776
num_to_remove: usize,
17771777
},
17781778
}
1779-
1780-
#[derive(Diagnostic)]
1781-
#[diag("failed to resolve delegation callee")]
1782-
pub(crate) struct UnresolvedDelegationCallee {
1783-
#[primary_span]
1784-
pub span: Span,
1785-
}

compiler/rustc_resolve/src/late.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use smallvec::{SmallVec, smallvec};
3939
use thin_vec::ThinVec;
4040
use tracing::{debug, instrument, trace};
4141

42-
use crate::errors::UnresolvedDelegationCallee;
4342
use crate::{
4443
BindingError, BindingKey, Decl, DelegationFnSig, Finalize, IdentKey, LateDecl, LocalModule,
4544
Module, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError, Resolver, Segment,
@@ -3907,25 +3906,23 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
39073906
.get(&resolution_node_id)
39083907
.and_then(|r| r.expect_full_res().opt_def_id());
39093908

3910-
let resolution = if let Some(resolution_id) = def_id
3909+
let resolution_id = if let Some(resolution_id) = def_id
39113910
// If there is a single unresolved `reuse foo` in a mod it resolves to itself,
39123911
// in this case emit `UnresolvedDelegationCallee` error instead of cycle error.
39133912
&& resolution_id != self.r.current_owner.def_id.to_def_id()
39143913
{
3915-
Ok(DelegationInfo { resolution_id })
3914+
Ok(resolution_id)
39163915
} else {
3917-
self.r.tcx.dcx().span_delayed_bug(
3916+
Err(self.r.tcx.dcx().span_delayed_bug(
39183917
delegation.path.span,
39193918
format!(
39203919
"LateResolutionVisitor: couldn't resolve node {resolution_node_id:?} in delegation item",
39213920
),
3922-
);
3923-
3924-
let err = UnresolvedDelegationCallee { span: delegation.last_segment_span() };
3925-
Err(self.r.tcx.dcx().emit_err(err))
3921+
))
39263922
};
39273923

3928-
self.r.delegation_infos.insert(self.r.current_owner.def_id, resolution);
3924+
let info = DelegationInfo { resolution_id };
3925+
self.r.delegation_infos.insert(self.r.current_owner.def_id, info);
39293926

39303927
let Some(body) = &delegation.body else { return };
39313928
self.with_rib(ValueNS, RibKind::FnOrCoroutine, |this| {

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ pub struct Resolver<'ra, 'tcx> {
15091509
/// Generic args to suggest for required params (e.g. `<'_>`, `<_, _>`), if any.
15101510
item_required_generic_args_suggestions: FxHashMap<LocalDefId, String> = default::fx_hash_map(),
15111511
delegation_fn_sigs: LocalDefIdMap<DelegationFnSig> = Default::default(),
1512-
delegation_infos: LocalDefIdMap<Result<DelegationInfo, ErrorGuaranteed>> = Default::default(),
1512+
delegation_infos: LocalDefIdMap<DelegationInfo> = Default::default(),
15131513

15141514
main_def: Option<MainDefinition> = None,
15151515
trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,

tests/ui/delegation/bad-resolve.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,24 @@ impl Trait for S {
2323
reuse <F as Trait>::C;
2424
//~^ ERROR item `C` is an associated method, which doesn't match its trait `Trait`
2525
//~| ERROR expected function, found associated constant `Trait::C`
26-
//~| ERROR: failed to resolve delegation callee
2726
reuse <F as Trait>::Type;
2827
//~^ ERROR item `Type` is an associated method, which doesn't match its trait `Trait`
2928
//~| ERROR expected method or associated constant, found associated type `Trait::Type`
30-
//~| ERROR: failed to resolve delegation callee
3129
reuse <F as Trait>::baz;
3230
//~^ ERROR method `baz` is not a member of trait `Trait`
3331
//~| ERROR cannot find method or associated constant `baz` in trait `Trait`
34-
//~| ERROR: failed to resolve delegation callee
3532
reuse <F as Trait>::bar;
3633

3734
reuse foo { &self.0 }
3835
//~^ ERROR cannot find function `foo` in this scope
3936
reuse Trait::foo2 { self.0 }
4037
//~^ ERROR cannot find function `foo2` in trait `Trait`
4138
//~| ERROR method `foo2` is not a member of trait `Trait`
42-
//~| ERROR: failed to resolve delegation callee
4339
}
4440

4541
mod prefix {}
4642
reuse unresolved_prefix::{a, b, c}; //~ ERROR cannot find module or crate `unresolved_prefix`
47-
//~^ ERROR: failed to resolve delegation callee
48-
//~| ERROR: failed to resolve delegation callee
49-
//~| ERROR: failed to resolve delegation callee
5043
reuse prefix::{self, super, crate}; //~ ERROR `crate` in paths can only be used in start position
5144
//~^ ERROR expected function, found module `prefix::self`
52-
//~| ERROR: failed to resolve delegation callee
53-
//~| ERROR: failed to resolve delegation callee
54-
//~| ERROR: failed to resolve delegation callee
5545

5646
fn main() {}

tests/ui/delegation/bad-resolve.stderr

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,47 @@ LL | const C: u32 = 0;
77
LL | reuse <F as Trait>::C;
88
| ^^^^^^^^^^^^^^^^^^^^^^ does not match trait
99

10-
error: failed to resolve delegation callee
11-
--> $DIR/bad-resolve.rs:23:25
12-
|
13-
LL | reuse <F as Trait>::C;
14-
| ^
15-
1610
error[E0324]: item `Type` is an associated method, which doesn't match its trait `Trait`
17-
--> $DIR/bad-resolve.rs:27:5
11+
--> $DIR/bad-resolve.rs:26:5
1812
|
1913
LL | type Type;
2014
| ---------- item in trait
2115
...
2216
LL | reuse <F as Trait>::Type;
2317
| ^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait
2418

25-
error: failed to resolve delegation callee
26-
--> $DIR/bad-resolve.rs:27:25
27-
|
28-
LL | reuse <F as Trait>::Type;
29-
| ^^^^
30-
3119
error[E0407]: method `baz` is not a member of trait `Trait`
32-
--> $DIR/bad-resolve.rs:31:5
20+
--> $DIR/bad-resolve.rs:29:5
3321
|
3422
LL | reuse <F as Trait>::baz;
3523
| ^^^^^^^^^^^^^^^^^^^^---^
3624
| | |
3725
| | help: there is an associated function with a similar name: `bar`
3826
| not a member of trait `Trait`
3927

40-
error: failed to resolve delegation callee
41-
--> $DIR/bad-resolve.rs:31:25
42-
|
43-
LL | reuse <F as Trait>::baz;
44-
| ^^^
45-
4628
error[E0407]: method `foo2` is not a member of trait `Trait`
47-
--> $DIR/bad-resolve.rs:39:5
29+
--> $DIR/bad-resolve.rs:36:5
4830
|
4931
LL | reuse Trait::foo2 { self.0 }
5032
| ^^^^^^^^^^^^^----^^^^^^^^^^^
5133
| | |
5234
| | help: there is an associated function with a similar name: `foo`
5335
| not a member of trait `Trait`
5436

55-
error: failed to resolve delegation callee
56-
--> $DIR/bad-resolve.rs:39:18
57-
|
58-
LL | reuse Trait::foo2 { self.0 }
59-
| ^^^^
60-
61-
error: failed to resolve delegation callee
62-
--> $DIR/bad-resolve.rs:46:27
63-
|
64-
LL | reuse unresolved_prefix::{a, b, c};
65-
| ^
66-
67-
error: failed to resolve delegation callee
68-
--> $DIR/bad-resolve.rs:46:30
69-
|
70-
LL | reuse unresolved_prefix::{a, b, c};
71-
| ^
72-
73-
error: failed to resolve delegation callee
74-
--> $DIR/bad-resolve.rs:46:33
75-
|
76-
LL | reuse unresolved_prefix::{a, b, c};
77-
| ^
78-
79-
error: failed to resolve delegation callee
80-
--> $DIR/bad-resolve.rs:50:16
81-
|
82-
LL | reuse prefix::{self, super, crate};
83-
| ^^^^
84-
85-
error: failed to resolve delegation callee
86-
--> $DIR/bad-resolve.rs:50:22
87-
|
88-
LL | reuse prefix::{self, super, crate};
89-
| ^^^^^
90-
91-
error: failed to resolve delegation callee
92-
--> $DIR/bad-resolve.rs:50:29
93-
|
94-
LL | reuse prefix::{self, super, crate};
95-
| ^^^^^
96-
9737
error[E0423]: expected function, found associated constant `Trait::C`
9838
--> $DIR/bad-resolve.rs:23:11
9939
|
10040
LL | reuse <F as Trait>::C;
10141
| ^^^^^^^^^^^^^^^ not a function
10242

10343
error[E0575]: expected method or associated constant, found associated type `Trait::Type`
104-
--> $DIR/bad-resolve.rs:27:11
44+
--> $DIR/bad-resolve.rs:26:11
10545
|
10646
LL | reuse <F as Trait>::Type;
10747
| ^^^^^^^^^^^^^^^^^^ not a method or associated constant
10848

10949
error[E0576]: cannot find method or associated constant `baz` in trait `Trait`
110-
--> $DIR/bad-resolve.rs:31:25
50+
--> $DIR/bad-resolve.rs:29:25
11151
|
11252
LL | fn bar() {}
11353
| -------- similarly named associated function `bar` defined here
@@ -122,13 +62,13 @@ LL + reuse <F as Trait>::bar;
12262
|
12363

12464
error[E0425]: cannot find function `foo` in this scope
125-
--> $DIR/bad-resolve.rs:37:11
65+
--> $DIR/bad-resolve.rs:34:11
12666
|
12767
LL | reuse foo { &self.0 }
12868
| ^^^ not found in this scope
12969

13070
error[E0425]: cannot find function `foo2` in trait `Trait`
131-
--> $DIR/bad-resolve.rs:39:18
71+
--> $DIR/bad-resolve.rs:36:18
13272
|
13373
LL | fn foo(&self, x: i32) -> i32 { x }
13474
| ---------------------------- similarly named associated function `foo` defined here
@@ -143,7 +83,7 @@ LL + reuse Trait::foo { self.0 }
14383
|
14484

14585
error[E0423]: expected function, found module `prefix::self`
146-
--> $DIR/bad-resolve.rs:50:7
86+
--> $DIR/bad-resolve.rs:43:7
14787
|
14888
LL | reuse prefix::{self, super, crate};
14989
| ^^^^^^ not a function
@@ -158,20 +98,20 @@ LL | impl Trait for S {
15898
| ^^^^^^^^^^^^^^^^ missing `Type` in implementation
15999

160100
error[E0433]: cannot find module or crate `unresolved_prefix` in this scope
161-
--> $DIR/bad-resolve.rs:46:7
101+
--> $DIR/bad-resolve.rs:42:7
162102
|
163103
LL | reuse unresolved_prefix::{a, b, c};
164104
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
165105
|
166106
= help: you might be missing a crate named `unresolved_prefix`
167107

168108
error[E0433]: `crate` in paths can only be used in start position
169-
--> $DIR/bad-resolve.rs:50:29
109+
--> $DIR/bad-resolve.rs:43:29
170110
|
171111
LL | reuse prefix::{self, super, crate};
172112
| ^^^^^ can only be used in path start position
173113

174-
error: aborting due to 23 previous errors
114+
error: aborting due to 13 previous errors
175115

176116
Some errors have detailed explanations: E0046, E0324, E0407, E0423, E0425, E0433, E0575, E0576.
177117
For more information about an error, try `rustc --explain E0046`.

tests/ui/delegation/duplicate-definition-inside-trait-impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ impl Trait for S {
1919
//~^ ERROR duplicate definitions with name `foo`
2020
//~| ERROR: this function takes 1 argument but 0 arguments were supplied
2121
//~| ERROR: mismatched types
22-
//~| ERROR: failed to resolve delegation callee
2322
}
2423

2524
fn main() {}

tests/ui/delegation/duplicate-definition-inside-trait-impl.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ LL | reuse to_reuse::foo { self }
99
LL | reuse Trait::foo;
1010
| ^^^^^^^^^^^^^^^^^ duplicate definition
1111

12-
error: failed to resolve delegation callee
13-
--> $DIR/duplicate-definition-inside-trait-impl.rs:18:18
14-
|
15-
LL | reuse Trait::foo;
16-
| ^^^
17-
1812
error[E0061]: this function takes 1 argument but 0 arguments were supplied
1913
--> $DIR/duplicate-definition-inside-trait-impl.rs:18:18
2014
|
@@ -40,7 +34,7 @@ LL | reuse Trait::foo;
4034
| expected `()`, found `u32`
4135
| expected `()` because of default return type
4236

43-
error: aborting due to 4 previous errors
37+
error: aborting due to 3 previous errors
4438

4539
Some errors have detailed explanations: E0061, E0201, E0308.
4640
For more information about an error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)