Skip to content

Commit 2f8867e

Browse files
authored
Rollup merge of #118962 - compiler-errors:bugs, r=TaKO8Ki
Annotate some bugs Gives a semi-helpful message to some `bug!()`/`unreachable!()`/`panic!()`. This also works around some other bugs/panics/etc that weren't needed, and also makes some of them into `span_bug!`s so they also have a useful span. Note to reviewer: best to disable whitespace when comparing for some cases where indentation changed. cc #118955
2 parents 4d1bd0d + 1cc0d7d commit 2f8867e

File tree

41 files changed

+325
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+325
-229
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
336336
hir::InlineAsmOperand::Const { .. }
337337
| hir::InlineAsmOperand::SymFn { .. }
338338
| hir::InlineAsmOperand::SymStatic { .. } => {
339-
unreachable!()
339+
unreachable!("{op:?} is not a register operand");
340340
}
341341
};
342342

@@ -380,7 +380,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
380380
{
381381
reg_sym.as_str()
382382
} else {
383-
unreachable!();
383+
unreachable!("{op:?} is not a register operand");
384384
}
385385
};
386386

compiler/rustc_ast_lowering/src/item.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
421421
}
422422
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
423423
let body = P(self.lower_delim_args(body));
424-
let DefKind::Macro(macro_kind) = self.tcx.def_kind(self.local_def_id(id)) else {
425-
unreachable!()
424+
let def_id = self.local_def_id(id);
425+
let def_kind = self.tcx.def_kind(def_id);
426+
let DefKind::Macro(macro_kind) = def_kind else {
427+
unreachable!(
428+
"expected DefKind::Macro for macro item, found {}",
429+
def_kind.descr(def_id.to_def_id())
430+
);
426431
};
427432
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
428433
hir::ItemKind::Macro(macro_def, macro_kind)

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2815,7 +2815,7 @@ impl TraitRef<'_> {
28152815
match self.path.res {
28162816
Res::Def(DefKind::Trait | DefKind::TraitAlias, did) => Some(did),
28172817
Res::Err => None,
2818-
_ => unreachable!(),
2818+
res => panic!("{res:?} did not resolve to a trait or trait alias"),
28192819
}
28202820
}
28212821
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
477477
ty::Const::new_misc_error(tcx, ty).into()
478478
}
479479
}
480-
_ => unreachable!(),
480+
(kind, arg) => span_bug!(
481+
self.span,
482+
"mismatched path argument for kind {kind:?}: found arg {arg:?}"
483+
),
481484
}
482485
}
483486

@@ -1946,7 +1949,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19461949
"s",
19471950
),
19481951
[only] => (only.to_string(), ""),
1949-
[] => unreachable!(),
1952+
[] => unreachable!("expected at least one generic to prohibit"),
19501953
};
19511954
let last_span = *arg_spans.last().unwrap();
19521955
let span: MultiSpan = arg_spans.into();
@@ -2555,8 +2558,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25552558
if let Some(i) = (param.index as usize).checked_sub(generics.count() - lifetimes.len())
25562559
{
25572560
// Resolve our own lifetime parameters.
2558-
let GenericParamDefKind::Lifetime { .. } = param.kind else { bug!() };
2559-
let hir::GenericArg::Lifetime(lifetime) = &lifetimes[i] else { bug!() };
2561+
let GenericParamDefKind::Lifetime { .. } = param.kind else {
2562+
span_bug!(
2563+
tcx.def_span(param.def_id),
2564+
"only expected lifetime for opaque's own generics, got {:?}",
2565+
param.kind
2566+
);
2567+
};
2568+
let hir::GenericArg::Lifetime(lifetime) = &lifetimes[i] else {
2569+
bug!(
2570+
"expected lifetime argument for param {param:?}, found {:?}",
2571+
&lifetimes[i]
2572+
)
2573+
};
25602574
self.ast_region_to_region(lifetime, None).into()
25612575
} else {
25622576
tcx.mk_param_from_def(param)

compiler/rustc_hir_analysis/src/astconv/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
7373
| ty::ClauseKind::ConstArgHasType(..)
7474
| ty::ClauseKind::WellFormed(_)
7575
| ty::ClauseKind::ConstEvaluatable(_) => {
76-
bug!()
76+
span_bug!(span, "did not expect {pred} clause in object bounds");
7777
}
7878
}
7979
}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+93-84
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,14 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
10861086
_ => return Ok(region),
10871087
}
10881088

1089-
let e = if let Some(region) = self.map.get(&region) {
1090-
if let ty::ReEarlyParam(e) = region.kind() { e } else { bug!() }
1089+
let e = if let Some(id_region) = self.map.get(&region) {
1090+
if let ty::ReEarlyParam(e) = id_region.kind() {
1091+
e
1092+
} else {
1093+
bug!(
1094+
"expected to map region {region} to early-bound identity region, but got {id_region}"
1095+
);
1096+
}
10911097
} else {
10921098
let guar = match region.kind() {
10931099
ty::ReEarlyParam(ty::EarlyParamRegion { def_id, .. })
@@ -1710,92 +1716,87 @@ fn compare_synthetic_generics<'tcx>(
17101716
trait_m.name
17111717
);
17121718
err.span_label(trait_span, "declaration in trait here");
1713-
match (impl_synthetic, trait_synthetic) {
1719+
if impl_synthetic {
17141720
// The case where the impl method uses `impl Trait` but the trait method uses
17151721
// explicit generics
1716-
(true, false) => {
1717-
err.span_label(impl_span, "expected generic parameter, found `impl Trait`");
1718-
let _: Option<_> = try {
1719-
// try taking the name from the trait impl
1720-
// FIXME: this is obviously suboptimal since the name can already be used
1721-
// as another generic argument
1722-
let new_name = tcx.opt_item_name(trait_def_id)?;
1723-
let trait_m = trait_m.def_id.as_local()?;
1724-
let trait_m = tcx.hir().expect_trait_item(trait_m);
1725-
1726-
let impl_m = impl_m.def_id.as_local()?;
1727-
let impl_m = tcx.hir().expect_impl_item(impl_m);
1728-
1729-
// in case there are no generics, take the spot between the function name
1730-
// and the opening paren of the argument list
1731-
let new_generics_span = tcx.def_ident_span(impl_def_id)?.shrink_to_hi();
1732-
// in case there are generics, just replace them
1733-
let generics_span =
1734-
impl_m.generics.span.substitute_dummy(new_generics_span);
1735-
// replace with the generics from the trait
1736-
let new_generics =
1737-
tcx.sess.source_map().span_to_snippet(trait_m.generics.span).ok()?;
1738-
1739-
err.multipart_suggestion(
1740-
"try changing the `impl Trait` argument to a generic parameter",
1741-
vec![
1742-
// replace `impl Trait` with `T`
1743-
(impl_span, new_name.to_string()),
1744-
// replace impl method generics with trait method generics
1745-
// This isn't quite right, as users might have changed the names
1746-
// of the generics, but it works for the common case
1747-
(generics_span, new_generics),
1748-
],
1749-
Applicability::MaybeIncorrect,
1750-
);
1751-
};
1752-
}
1722+
err.span_label(impl_span, "expected generic parameter, found `impl Trait`");
1723+
let _: Option<_> = try {
1724+
// try taking the name from the trait impl
1725+
// FIXME: this is obviously suboptimal since the name can already be used
1726+
// as another generic argument
1727+
let new_name = tcx.opt_item_name(trait_def_id)?;
1728+
let trait_m = trait_m.def_id.as_local()?;
1729+
let trait_m = tcx.hir().expect_trait_item(trait_m);
1730+
1731+
let impl_m = impl_m.def_id.as_local()?;
1732+
let impl_m = tcx.hir().expect_impl_item(impl_m);
1733+
1734+
// in case there are no generics, take the spot between the function name
1735+
// and the opening paren of the argument list
1736+
let new_generics_span = tcx.def_ident_span(impl_def_id)?.shrink_to_hi();
1737+
// in case there are generics, just replace them
1738+
let generics_span = impl_m.generics.span.substitute_dummy(new_generics_span);
1739+
// replace with the generics from the trait
1740+
let new_generics =
1741+
tcx.sess.source_map().span_to_snippet(trait_m.generics.span).ok()?;
1742+
1743+
err.multipart_suggestion(
1744+
"try changing the `impl Trait` argument to a generic parameter",
1745+
vec![
1746+
// replace `impl Trait` with `T`
1747+
(impl_span, new_name.to_string()),
1748+
// replace impl method generics with trait method generics
1749+
// This isn't quite right, as users might have changed the names
1750+
// of the generics, but it works for the common case
1751+
(generics_span, new_generics),
1752+
],
1753+
Applicability::MaybeIncorrect,
1754+
);
1755+
};
1756+
} else {
17531757
// The case where the trait method uses `impl Trait`, but the impl method uses
17541758
// explicit generics.
1755-
(false, true) => {
1756-
err.span_label(impl_span, "expected `impl Trait`, found generic parameter");
1757-
let _: Option<_> = try {
1758-
let impl_m = impl_m.def_id.as_local()?;
1759-
let impl_m = tcx.hir().expect_impl_item(impl_m);
1760-
let (sig, _) = impl_m.expect_fn();
1761-
let input_tys = sig.decl.inputs;
1762-
1763-
struct Visitor(Option<Span>, hir::def_id::LocalDefId);
1764-
impl<'v> intravisit::Visitor<'v> for Visitor {
1765-
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
1766-
intravisit::walk_ty(self, ty);
1767-
if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ty.kind
1768-
&& let Res::Def(DefKind::TyParam, def_id) = path.res
1769-
&& def_id == self.1.to_def_id()
1770-
{
1771-
self.0 = Some(ty.span);
1772-
}
1759+
err.span_label(impl_span, "expected `impl Trait`, found generic parameter");
1760+
let _: Option<_> = try {
1761+
let impl_m = impl_m.def_id.as_local()?;
1762+
let impl_m = tcx.hir().expect_impl_item(impl_m);
1763+
let (sig, _) = impl_m.expect_fn();
1764+
let input_tys = sig.decl.inputs;
1765+
1766+
struct Visitor(Option<Span>, hir::def_id::LocalDefId);
1767+
impl<'v> intravisit::Visitor<'v> for Visitor {
1768+
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
1769+
intravisit::walk_ty(self, ty);
1770+
if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ty.kind
1771+
&& let Res::Def(DefKind::TyParam, def_id) = path.res
1772+
&& def_id == self.1.to_def_id()
1773+
{
1774+
self.0 = Some(ty.span);
17731775
}
17741776
}
1777+
}
17751778

1776-
let mut visitor = Visitor(None, impl_def_id);
1777-
for ty in input_tys {
1778-
intravisit::Visitor::visit_ty(&mut visitor, ty);
1779-
}
1780-
let span = visitor.0?;
1781-
1782-
let bounds = impl_m.generics.bounds_for_param(impl_def_id).next()?.bounds;
1783-
let bounds = bounds.first()?.span().to(bounds.last()?.span());
1784-
let bounds = tcx.sess.source_map().span_to_snippet(bounds).ok()?;
1785-
1786-
err.multipart_suggestion(
1787-
"try removing the generic parameter and using `impl Trait` instead",
1788-
vec![
1789-
// delete generic parameters
1790-
(impl_m.generics.span, String::new()),
1791-
// replace param usage with `impl Trait`
1792-
(span, format!("impl {bounds}")),
1793-
],
1794-
Applicability::MaybeIncorrect,
1795-
);
1796-
};
1797-
}
1798-
_ => unreachable!(),
1779+
let mut visitor = Visitor(None, impl_def_id);
1780+
for ty in input_tys {
1781+
intravisit::Visitor::visit_ty(&mut visitor, ty);
1782+
}
1783+
let span = visitor.0?;
1784+
1785+
let bounds = impl_m.generics.bounds_for_param(impl_def_id).next()?.bounds;
1786+
let bounds = bounds.first()?.span().to(bounds.last()?.span());
1787+
let bounds = tcx.sess.source_map().span_to_snippet(bounds).ok()?;
1788+
1789+
err.multipart_suggestion(
1790+
"try removing the generic parameter and using `impl Trait` instead",
1791+
vec![
1792+
// delete generic parameters
1793+
(impl_m.generics.span, String::new()),
1794+
// replace param usage with `impl Trait`
1795+
(span, format!("impl {bounds}")),
1796+
],
1797+
Applicability::MaybeIncorrect,
1798+
);
1799+
};
17991800
}
18001801
error_found = Some(err.emit_unless(delay));
18011802
}
@@ -1859,7 +1860,9 @@ fn compare_generic_param_kinds<'tcx>(
18591860
// this is exhaustive so that anyone adding new generic param kinds knows
18601861
// to make sure this error is reported for them.
18611862
(Const { .. }, Const { .. }) | (Type { .. }, Type { .. }) => false,
1862-
(Lifetime { .. }, _) | (_, Lifetime { .. }) => unreachable!(),
1863+
(Lifetime { .. }, _) | (_, Lifetime { .. }) => {
1864+
bug!("lifetime params are expected to be filtered by `ty_const_params_of`")
1865+
}
18631866
} {
18641867
let param_impl_span = tcx.def_span(param_impl.def_id);
18651868
let param_trait_span = tcx.def_span(param_trait.def_id);
@@ -1883,7 +1886,10 @@ fn compare_generic_param_kinds<'tcx>(
18831886
)
18841887
}
18851888
Type { .. } => format!("{prefix} type parameter"),
1886-
Lifetime { .. } => unreachable!(),
1889+
Lifetime { .. } => span_bug!(
1890+
tcx.def_span(param.def_id),
1891+
"lifetime params are expected to be filtered by `ty_const_params_of`"
1892+
),
18871893
};
18881894

18891895
let trait_header_span = tcx.def_ident_span(tcx.parent(trait_item.def_id)).unwrap();
@@ -2187,7 +2193,10 @@ pub(super) fn check_type_bounds<'tcx>(
21872193
..
21882194
}) => ty.span,
21892195
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span,
2190-
_ => bug!(),
2196+
item => span_bug!(
2197+
tcx.def_span(impl_ty_def_id),
2198+
"cannot call `check_type_bounds` on item: {item:?}",
2199+
),
21912200
}
21922201
};
21932202
let assumed_wf_types = ocx.assumed_wf_types_and_report_errors(param_env, impl_ty_def_id)?;

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ fn report_mismatched_rpitit_signature<'tcx>(
262262

263263
if tcx.asyncness(impl_m_def_id).is_async() && tcx.asyncness(trait_m_def_id).is_async() {
264264
let ty::Alias(ty::Projection, future_ty) = return_ty.kind() else {
265-
bug!();
265+
span_bug!(
266+
tcx.def_span(trait_m_def_id),
267+
"expected return type of async fn in trait to be a AFIT projection"
268+
);
266269
};
267270
let Some(future_output_ty) = tcx
268271
.explicit_item_bounds(future_ty.def_id)
@@ -272,7 +275,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
272275
_ => None,
273276
})
274277
else {
275-
bug!()
278+
span_bug!(tcx.def_span(trait_m_def_id), "expected `Future` projection bound in AFIT");
276279
};
277280
return_ty = future_output_ty;
278281
}

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
4949
16 => InlineAsmType::I16,
5050
32 => InlineAsmType::I32,
5151
64 => InlineAsmType::I64,
52-
_ => unreachable!(),
52+
width => bug!("unsupported pointer width: {width}"),
5353
};
5454

5555
match *ty.kind() {
@@ -101,15 +101,15 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
101101
16 => InlineAsmType::VecI16(size),
102102
32 => InlineAsmType::VecI32(size),
103103
64 => InlineAsmType::VecI64(size),
104-
_ => unreachable!(),
104+
width => bug!("unsupported pointer width: {width}"),
105105
})
106106
}
107107
ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(size)),
108108
ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(size)),
109109
_ => None,
110110
}
111111
}
112-
ty::Infer(_) => unreachable!(),
112+
ty::Infer(_) => bug!("unexpected infer ty in asm operand"),
113113
_ => None,
114114
}
115115
}
@@ -136,8 +136,15 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
136136
ty::Adt(adt, args) if Some(adt.did()) == self.tcx.lang_items().maybe_uninit() => {
137137
let fields = &adt.non_enum_variant().fields;
138138
let ty = fields[FieldIdx::from_u32(1)].ty(self.tcx, args);
139-
let ty::Adt(ty, args) = ty.kind() else { unreachable!() };
140-
assert!(ty.is_manually_drop());
139+
// FIXME: Are we just trying to map to the `T` in `MaybeUninit<T>`?
140+
// If so, just get it from the args.
141+
let ty::Adt(ty, args) = ty.kind() else {
142+
unreachable!("expected first field of `MaybeUninit` to be an ADT")
143+
};
144+
assert!(
145+
ty.is_manually_drop(),
146+
"expected first field of `MaybeUnit` to be `ManuallyDrop`"
147+
);
141148
let fields = &ty.non_enum_variant().fields;
142149
let ty = fields[FieldIdx::from_u32(0)].ty(self.tcx, args);
143150
self.get_asm_ty(ty)

0 commit comments

Comments
 (0)