Skip to content

Commit 83122e6

Browse files
committed
Infallible version of fn_sig.
1 parent 1842886 commit 83122e6

File tree

13 files changed

+88
-73
lines changed

13 files changed

+88
-73
lines changed

compiler/rustc_incremental/src/persist/dirty_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const BASE_CONST: &[&str] = &[label_strs::try_type_of];
4242
/// DepNodes for functions + methods
4343
const BASE_FN: &[&str] = &[
4444
// Callers will depend on the signature of these items, so we better test
45-
label_strs::fn_sig,
45+
label_strs::try_fn_sig,
4646
label_strs::generics_of,
4747
label_strs::predicates_of,
4848
label_strs::try_type_of,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
123123
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
124124
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
125125
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
126-
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
126+
try_fn_sig => { Ok(cdata.fn_sig(def_id.index, tcx)) }
127127
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
128128
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
129129
asyncness => { cdata.asyncness(def_id.index) }

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ rustc_queries! {
600600
}
601601

602602
/// The signature of functions.
603-
query fn_sig(key: DefId) -> ty::PolyFnSig<'tcx> {
603+
query try_fn_sig(key: DefId) -> Result<ty::PolyFnSig<'tcx>, String> {
604604
desc { |tcx| "computing function signature of `{}`", tcx.def_path_str(key) }
605605
}
606606

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ impl TyCtxt<'tcx> {
313313
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
314314
}
315315

316+
pub fn fn_sig(self, def_id: impl IntoQueryParam<DefId>) -> ty::PolyFnSig<'tcx> {
317+
let def_id = def_id.into_query_param();
318+
self.try_fn_sig(def_id)
319+
.unwrap_or_else(|s| span_bug!(self.def_span(def_id), "fn_sig: {}", s))
320+
}
321+
316322
pub fn type_of(self, def_id: impl IntoQueryParam<DefId>) -> Ty<'tcx> {
317323
let def_id = def_id.into_query_param();
318324
self.try_type_of(def_id).unwrap_or_else(|msg| {
@@ -329,6 +335,12 @@ impl TyCtxtAt<'tcx> {
329335
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
330336
}
331337

338+
pub fn fn_sig(self, def_id: impl IntoQueryParam<DefId>) -> ty::PolyFnSig<'tcx> {
339+
let def_id = def_id.into_query_param();
340+
self.try_fn_sig(def_id)
341+
.unwrap_or_else(|s| span_bug!(self.def_span(def_id), "fn_sig: {}", s))
342+
}
343+
332344
pub fn type_of(self, def_id: impl IntoQueryParam<DefId>) -> Ty<'tcx> {
333345
let def_id = def_id.into_query_param();
334346
self.try_type_of(def_id).unwrap_or_else(|msg| {

compiler/rustc_typeck/src/collect.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn provide(providers: &mut Providers) {
8383
type_param_predicates,
8484
trait_def,
8585
adt_def,
86-
fn_sig,
86+
try_fn_sig,
8787
impl_trait_ref,
8888
impl_polarity,
8989
is_foreign_item,
@@ -730,7 +730,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
730730
tcx.ensure().try_type_of(item.def_id);
731731
tcx.ensure().predicates_of(item.def_id);
732732
if let hir::ForeignItemKind::Fn(..) = item.kind {
733-
tcx.ensure().fn_sig(item.def_id);
733+
tcx.ensure().try_fn_sig(item.def_id);
734734
}
735735
}
736736
}
@@ -793,7 +793,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
793793
tcx.ensure().try_type_of(def_id);
794794
tcx.ensure().predicates_of(def_id);
795795
match it.kind {
796-
hir::ItemKind::Fn(..) => tcx.ensure().fn_sig(def_id),
796+
hir::ItemKind::Fn(..) => tcx.ensure().try_fn_sig(def_id),
797797
hir::ItemKind::OpaqueTy(..) => tcx.ensure().item_bounds(def_id),
798798
_ => (),
799799
}
@@ -808,7 +808,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
808808
match trait_item.kind {
809809
hir::TraitItemKind::Fn(..) => {
810810
tcx.ensure().try_type_of(trait_item_id.def_id);
811-
tcx.ensure().fn_sig(trait_item_id.def_id);
811+
tcx.ensure().try_fn_sig(trait_item_id.def_id);
812812
}
813813

814814
hir::TraitItemKind::Const(.., Some(_)) => {
@@ -854,7 +854,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
854854
let impl_item = tcx.hir().impl_item(impl_item_id);
855855
match impl_item.kind {
856856
hir::ImplItemKind::Fn(..) => {
857-
tcx.ensure().fn_sig(def_id);
857+
tcx.ensure().try_fn_sig(def_id);
858858
}
859859
hir::ImplItemKind::TyAlias(_) => {
860860
// Account for `type T = _;`
@@ -1631,7 +1631,7 @@ pub fn get_infer_ret_ty(output: &'hir hir::FnRetTy<'hir>) -> Option<&'hir hir::T
16311631
None
16321632
}
16331633

1634-
fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1634+
fn try_fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> Result<ty::PolyFnSig<'_>, String> {
16351635
use rustc_hir::Node::*;
16361636
use rustc_hir::*;
16371637

@@ -1640,7 +1640,8 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
16401640

16411641
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
16421642

1643-
match tcx.hir().get(hir_id) {
1643+
let node = tcx.hir().find(hir_id).ok_or_else(|| "not found in HIR map".to_owned())?;
1644+
let sig = match node {
16441645
TraitItem(hir::TraitItem {
16451646
kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
16461647
ident,
@@ -1749,15 +1750,17 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17491750
// `sig` method on the `ClosureSubsts`:
17501751
//
17511752
// substs.as_closure().sig(def_id, tcx)
1752-
bug!(
1753-
"to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`",
1753+
return Err(
1754+
"to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`"
1755+
.to_owned(),
17541756
);
17551757
}
17561758

1757-
x => {
1758-
bug!("unexpected sort of node in fn_sig(): {:?}", x);
1759+
_ => {
1760+
return Err(format!("unexpected sort of node in fn_sig()"));
17591761
}
1760-
}
1762+
};
1763+
Ok(sig)
17611764
}
17621765

17631766
fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {

src/test/incremental/hashes/enum_constructors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub mod change_constructor_path_indirectly_struct_like {
139139

140140
#[rustc_clean(
141141
cfg="cfail2",
142-
except="fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
142+
except="try_fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
143143
typeck"
144144
)]
145145
#[rustc_clean(cfg="cfail3")]
@@ -232,7 +232,7 @@ pub mod change_constructor_path_indirectly_tuple_like {
232232

233233
#[rustc_clean(
234234
cfg="cfail2",
235-
except="fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
235+
except="try_fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
236236
typeck"
237237
)]
238238
#[rustc_clean(cfg="cfail3")]
@@ -309,7 +309,7 @@ pub mod change_constructor_path_indirectly_c_like {
309309

310310
#[rustc_clean(
311311
cfg="cfail2",
312-
except="fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
312+
except="try_fn_sig,hir_owner,hir_owner_nodes,optimized_mir,\
313313
typeck"
314314
)]
315315
#[rustc_clean(cfg="cfail3")]

src/test/incremental/hashes/function_interfaces.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn add_parameter() {}
2222
#[cfg(not(cfail1))]
2323
#[rustc_clean(
2424
cfg = "cfail2",
25-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
25+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
2626
)]
2727
#[rustc_clean(cfg = "cfail3")]
2828
pub fn add_parameter(p: i32) {}
@@ -45,7 +45,7 @@ pub fn type_of_parameter(p: i32) {}
4545
#[cfg(not(cfail1))]
4646
#[rustc_clean(
4747
cfg = "cfail2",
48-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
48+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
4949
)]
5050
#[rustc_clean(cfg = "cfail3")]
5151
pub fn type_of_parameter(p: i64) {}
@@ -58,7 +58,7 @@ pub fn type_of_parameter_ref(p: &i32) {}
5858
#[cfg(not(cfail1))]
5959
#[rustc_clean(
6060
cfg = "cfail2",
61-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
61+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
6262
)]
6363
#[rustc_clean(cfg = "cfail3")]
6464
pub fn type_of_parameter_ref(p: &mut i32) {}
@@ -71,7 +71,7 @@ pub fn order_of_parameters(p1: i32, p2: i64) {}
7171
#[cfg(not(cfail1))]
7272
#[rustc_clean(
7373
cfg = "cfail2",
74-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
74+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
7575
)]
7676
#[rustc_clean(cfg = "cfail3")]
7777
pub fn order_of_parameters(p2: i64, p1: i32) {}
@@ -84,7 +84,7 @@ pub fn make_unsafe() {}
8484
#[cfg(not(cfail1))]
8585
#[rustc_clean(
8686
cfg = "cfail2",
87-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
87+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
8888
)]
8989
#[rustc_clean(cfg = "cfail3")]
9090
pub unsafe fn make_unsafe() {}
@@ -95,7 +95,7 @@ pub unsafe fn make_unsafe() {}
9595
pub fn make_extern() {}
9696

9797
#[cfg(not(cfail1))]
98-
#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, fn_sig")]
98+
#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, try_fn_sig")]
9999
#[rustc_clean(cfg = "cfail3")]
100100
pub extern "C" fn make_extern() {}
101101

@@ -241,7 +241,7 @@ pub fn return_impl_trait() -> i32 {
241241
}
242242

243243
#[cfg(not(cfail1))]
244-
#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, fn_sig")]
244+
#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, try_fn_sig")]
245245
#[rustc_clean(cfg = "cfail3")]
246246
pub fn return_impl_trait() -> impl Clone {
247247
0
@@ -274,7 +274,7 @@ pub mod change_return_type_indirectly {
274274

275275
#[rustc_clean(
276276
cfg = "cfail2",
277-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
277+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
278278
)]
279279
#[rustc_clean(cfg = "cfail3")]
280280
pub fn indirect_return_type() -> ReturnType {
@@ -292,7 +292,7 @@ pub mod change_parameter_type_indirectly {
292292

293293
#[rustc_clean(
294294
cfg = "cfail2",
295-
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, fn_sig"
295+
except = "hir_owner, hir_owner_nodes, optimized_mir, typeck, try_fn_sig"
296296
)]
297297
#[rustc_clean(cfg = "cfail3")]
298298
pub fn indirect_parameter_type(p: ParameterType) {}

src/test/incremental/hashes/inherent_impls.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl Foo {
120120
impl Foo {
121121
#[rustc_clean(
122122
cfg="cfail2",
123-
except="hir_owner,hir_owner_nodes,fn_sig,typeck,optimized_mir"
123+
except="hir_owner,hir_owner_nodes,try_fn_sig,typeck,optimized_mir"
124124
)]
125125
#[rustc_clean(cfg="cfail3")]
126126
pub fn method_selfmutness(&mut self) { }
@@ -160,7 +160,7 @@ impl Foo {
160160
impl Foo {
161161
#[rustc_clean(
162162
cfg="cfail2",
163-
except="hir_owner,hir_owner_nodes,fn_sig,typeck,optimized_mir"
163+
except="hir_owner,hir_owner_nodes,try_fn_sig,typeck,optimized_mir"
164164
)]
165165
#[rustc_clean(cfg="cfail3")]
166166
pub fn add_method_parameter(&self, _: i32) { }
@@ -197,7 +197,7 @@ impl Foo {
197197
impl Foo {
198198
#[rustc_clean(
199199
cfg="cfail2",
200-
except="hir_owner,hir_owner_nodes,fn_sig,optimized_mir,typeck")]
200+
except="hir_owner,hir_owner_nodes,try_fn_sig,optimized_mir,typeck")]
201201
#[rustc_clean(cfg="cfail3")]
202202
pub fn change_method_return_type(&self) -> u8 { 0 }
203203
}
@@ -251,7 +251,7 @@ impl Foo {
251251
impl Foo {
252252
#[rustc_clean(
253253
cfg="cfail2",
254-
except="hir_owner,hir_owner_nodes,fn_sig,typeck,optimized_mir"
254+
except="hir_owner,hir_owner_nodes,try_fn_sig,typeck,optimized_mir"
255255
)]
256256
#[rustc_clean(cfg="cfail3")]
257257
pub unsafe fn make_method_unsafe(&self) { }
@@ -269,7 +269,7 @@ impl Foo {
269269
#[rustc_clean(cfg="cfail2")]
270270
#[rustc_clean(cfg="cfail3")]
271271
impl Foo {
272-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,fn_sig,typeck")]
272+
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,try_fn_sig,typeck")]
273273
#[rustc_clean(cfg="cfail3")]
274274
pub extern "C" fn make_method_extern(&self) { }
275275
}
@@ -286,7 +286,7 @@ impl Foo {
286286
#[rustc_clean(cfg="cfail2")]
287287
#[rustc_clean(cfg="cfail3")]
288288
impl Foo {
289-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,fn_sig,typeck")]
289+
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,try_fn_sig,typeck")]
290290
#[rustc_clean(cfg="cfail3")]
291291
pub extern "system" fn change_method_calling_convention(&self) { }
292292
}
@@ -453,7 +453,7 @@ impl Bar<u32> {
453453
impl<T> Bar<T> {
454454
#[rustc_clean(
455455
cfg="cfail2",
456-
except="generics_of,fn_sig,typeck,try_type_of,optimized_mir"
456+
except="generics_of,try_fn_sig,typeck,try_type_of,optimized_mir"
457457
)]
458458
#[rustc_clean(cfg="cfail3")]
459459
pub fn add_type_parameter_to_impl(&self) { }
@@ -471,7 +471,7 @@ impl Bar<u32> {
471471
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
472472
#[rustc_clean(cfg="cfail3")]
473473
impl Bar<u64> {
474-
#[rustc_clean(cfg="cfail2", except="fn_sig,optimized_mir,typeck")]
474+
#[rustc_clean(cfg="cfail2", except="try_fn_sig,optimized_mir,typeck")]
475475
#[rustc_clean(cfg="cfail3")]
476476
pub fn change_impl_self_type(&self) { }
477477
}

src/test/incremental/hashes/struct_constructors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub mod change_constructor_path_indirectly_regular_struct {
173173

174174
#[rustc_clean(
175175
cfg="cfail2",
176-
except="fn_sig,hir_owner,hir_owner_nodes,optimized_mir,typeck"
176+
except="try_fn_sig,hir_owner,hir_owner_nodes,optimized_mir,typeck"
177177
)]
178178
#[rustc_clean(cfg="cfail3")]
179179
pub fn function() -> Struct {
@@ -230,7 +230,7 @@ pub mod change_constructor_path_indirectly_tuple_struct {
230230

231231
#[rustc_clean(
232232
cfg="cfail2",
233-
except="fn_sig,hir_owner,hir_owner_nodes,optimized_mir,typeck"
233+
except="try_fn_sig,hir_owner,hir_owner_nodes,optimized_mir,typeck"
234234
)]
235235
#[rustc_clean(cfg="cfail3")]
236236
pub fn function() -> Struct {

src/test/ui/dep-graph/dep-graph-struct-signature.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ mod signatures {
2828
#[rustc_then_this_would_need(associated_item)] //~ ERROR no path
2929
#[rustc_then_this_would_need(trait_def)] //~ ERROR no path
3030
trait Bar {
31-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR OK
31+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR OK
3232
fn do_something(x: WillChange);
3333
}
3434

35-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR OK
35+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR OK
3636
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
3737
fn some_fn(x: WillChange) { }
3838

39-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR OK
39+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR OK
4040
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
4141
fn new_foo(x: u32, y: u32) -> WillChange {
4242
WillChange { x: x, y: y }
4343
}
4444

4545
#[rustc_then_this_would_need(try_type_of)] //~ ERROR OK
4646
impl WillChange {
47-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR OK
47+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR OK
4848
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
4949
fn new(x: u32, y: u32) -> WillChange { loop { } }
5050
}
5151

5252
#[rustc_then_this_would_need(try_type_of)] //~ ERROR OK
5353
impl WillChange {
54-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR OK
54+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR OK
5555
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
5656
fn method(&self, x: u32) { }
5757
}
@@ -73,14 +73,14 @@ mod invalid_signatures {
7373

7474
#[rustc_then_this_would_need(try_type_of)] //~ ERROR no path
7575
trait A {
76-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR no path
76+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR no path
7777
fn do_something_else_twice(x: WontChange);
7878
}
7979

80-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR no path
80+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR no path
8181
fn b(x: WontChange) { }
8282

83-
#[rustc_then_this_would_need(fn_sig)] //~ ERROR no path from `WillChange`
83+
#[rustc_then_this_would_need(try_fn_sig)] //~ ERROR no path from `WillChange`
8484
#[rustc_then_this_would_need(typeck)] //~ ERROR no path from `WillChange`
8585
fn c(x: u32) { }
8686
}

0 commit comments

Comments
 (0)