Skip to content

Commit e554f8d

Browse files
committed
fix panicking bug
1 parent f6e4461 commit e554f8d

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

compiler/rustc_mir_build/src/lints.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ use rustc_span::sym;
3636
fn is_default_impl<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
3737
if tcx.def_kind(def_id) == DefKind::AssocFn {
3838
// Check if this is a trait impl and get the defid of the trait if it is
39-
if let Some(trait_def_id) = tcx.trait_id_of_impl(tcx.parent(def_id.into())) {
40-
// check if it is a default impl
41-
if tcx.get_diagnostic_name(trait_def_id) == Some(sym::Default) {
42-
return true;
39+
if let Some(parent_def_id) = tcx.opt_parent(def_id.into()) {
40+
if tcx.def_kind(parent_def_id) == (DefKind::Impl {of_trait: true}) {
41+
if let Some(trait_def_id) = tcx.trait_id_of_impl(parent_def_id) {
42+
// check if it is a default impl
43+
if tcx.get_diagnostic_name(trait_def_id) == Some(sym::Default) {
44+
return true;
45+
}
46+
}
4347
}
4448
}
4549
}

tests/ui/lint/lint-unconditional-recursion.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ pub fn overflow_check(a: i32, b: i32) { //~ ERROR function cannot return without
177177
overflow_check(a, b);
178178
}
179179

180-
// pub struct Point {
181-
// pub x: f32,
182-
// pub y: f32,
183-
// }
184-
//
185-
// impl Default for Point {
186-
// fn default() -> Self { //~ ERROR function cannot return without recursing
187-
// Point {
188-
// x: Default::default(),
189-
// ..Default::default()
190-
// }
191-
// }
192-
// }
180+
pub struct Point {
181+
pub x: f32,
182+
pub y: f32,
183+
}
184+
185+
impl Default for Point {
186+
fn default() -> Self { //~ ERROR function cannot return without recursing
187+
Point {
188+
x: Default::default(),
189+
..Default::default()
190+
}
191+
}
192+
}
193193

194194
fn main() {}

tests/ui/lint/lint-unconditional-recursion.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ LL | let x = Default::default();
122122
| ------------------ recursive call site
123123
|
124124
= help: a `loop` may express intention better if this is on purpose
125+
= help: ..default() in the Default impl does not apply a default for each struct field
125126

126127
error: function cannot return without recursing
127128
--> $DIR/lint-unconditional-recursion.rs:102:5
@@ -196,6 +197,7 @@ LL | ..Default::default()
196197
| ------------------ recursive call site
197198
|
198199
= help: a `loop` may express intention better if this is on purpose
200+
= help: ..default() in the Default impl does not apply a default for each struct field
199201

200202
error: aborting due to 18 previous errors
201203

0 commit comments

Comments
 (0)