Skip to content

Commit 52b540f

Browse files
estebankpietroalbini
authored andcommitted
Sidestep ICE in type_of_def_id() when called from return_type_impl_trait
1 parent 7c23dcd commit 52b540f

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/librustc/ty/context.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use session::Session;
1717
use session::config::{BorrowckMode, OutputFilenames};
1818
use session::config::CrateType;
1919
use middle;
20-
use hir::{TraitCandidate, HirId, ItemLocalId, Node};
20+
use hir::{TraitCandidate, HirId, ItemKind, ItemLocalId, Node};
2121
use hir::def::{Def, Export};
2222
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2323
use hir::map as hir_map;
@@ -1604,6 +1604,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
16041604
&self,
16051605
scope_def_id: DefId,
16061606
) -> Option<Ty<'tcx>> {
1607+
// HACK: `type_of_def_id()` will fail on these (#55796), so return None
1608+
let node_id = self.hir.as_local_node_id(scope_def_id).unwrap();
1609+
match self.hir.get(node_id) {
1610+
Node::Item(item) => {
1611+
match item.node {
1612+
ItemKind::Trait(..)
1613+
| ItemKind::TraitAlias(..)
1614+
| ItemKind::Mod(..)
1615+
| ItemKind::ForeignMod(..)
1616+
| ItemKind::GlobalAsm(..)
1617+
| ItemKind::ExternCrate(..)
1618+
| ItemKind::Use(..) => {
1619+
return None;
1620+
}
1621+
_ => { /* type_of_def_id() will work */ }
1622+
}
1623+
}
1624+
_ => { /* type_of_def_id() will work or panic */ }
1625+
}
1626+
16071627
let ret_ty = self.type_of(scope_def_id);
16081628
match ret_ty.sty {
16091629
ty::FnDef(_, _) => {

src/test/ui/issues/issue-55796.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub trait EdgeTrait<N> {
2+
fn target(&self) -> N;
3+
}
4+
5+
pub trait Graph<'a> {
6+
type Node;
7+
type Edge: EdgeTrait<Self::Node>;
8+
type NodesIter: Iterator<Item = Self::Node> + 'a;
9+
type EdgesIter: Iterator<Item = Self::Edge> + 'a;
10+
11+
fn nodes(&'a self) -> Self::NodesIter;
12+
fn out_edges(&'a self, u: &Self::Node) -> Self::EdgesIter;
13+
fn in_edges(&'a self, u: &Self::Node) -> Self::EdgesIter;
14+
15+
fn out_neighbors(&'a self, u: &Self::Node) -> Box<Iterator<Item = Self::Node>> {
16+
Box::new(self.out_edges(u).map(|e| e.target()))
17+
}
18+
19+
fn in_neighbors(&'a self, u: &Self::Node) -> Box<Iterator<Item = Self::Node>> {
20+
Box::new(self.in_edges(u).map(|e| e.target()))
21+
}
22+
}

src/test/ui/issues/issue-55796.stderr

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0601]: `main` function not found in crate `issue_55796`
2+
|
3+
= note: consider adding a `main` function to `$DIR/issue-55796.rs`
4+
5+
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
6+
--> $DIR/issue-55796.rs:16:9
7+
|
8+
LL | Box::new(self.out_edges(u).map(|e| e.target()))
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
|
11+
note: first, the lifetime cannot outlive the lifetime 'a as defined on the trait at 5:17...
12+
--> $DIR/issue-55796.rs:5:17
13+
|
14+
LL | pub trait Graph<'a> {
15+
| ^^
16+
note: ...so that the type `std::iter::Map<<Self as Graph<'a>>::EdgesIter, [closure@$DIR/issue-55796.rs:16:40: 16:54]>` will meet its required lifetime bounds
17+
--> $DIR/issue-55796.rs:16:9
18+
|
19+
LL | Box::new(self.out_edges(u).map(|e| e.target()))
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
= note: but, the lifetime must be valid for the static lifetime...
22+
= note: ...so that the expression is assignable:
23+
expected std::boxed::Box<(dyn std::iter::Iterator<Item=<Self as Graph<'a>>::Node> + 'static)>
24+
found std::boxed::Box<dyn std::iter::Iterator<Item=<Self as Graph<'a>>::Node>>
25+
26+
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
27+
--> $DIR/issue-55796.rs:20:9
28+
|
29+
LL | Box::new(self.in_edges(u).map(|e| e.target()))
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
note: first, the lifetime cannot outlive the lifetime 'a as defined on the trait at 5:17...
33+
--> $DIR/issue-55796.rs:5:17
34+
|
35+
LL | pub trait Graph<'a> {
36+
| ^^
37+
note: ...so that the type `std::iter::Map<<Self as Graph<'a>>::EdgesIter, [closure@$DIR/issue-55796.rs:20:39: 20:53]>` will meet its required lifetime bounds
38+
--> $DIR/issue-55796.rs:20:9
39+
|
40+
LL | Box::new(self.in_edges(u).map(|e| e.target()))
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
= note: but, the lifetime must be valid for the static lifetime...
43+
= note: ...so that the expression is assignable:
44+
expected std::boxed::Box<(dyn std::iter::Iterator<Item=<Self as Graph<'a>>::Node> + 'static)>
45+
found std::boxed::Box<dyn std::iter::Iterator<Item=<Self as Graph<'a>>::Node>>
46+
47+
error: aborting due to 3 previous errors
48+
49+
Some errors occurred: E0495, E0601.
50+
For more information about an error, try `rustc --explain E0495`.

0 commit comments

Comments
 (0)