Skip to content

Commit e6707df

Browse files
committed
Auto merge of #118982 - matthiaskrgr:rollup-xoraxf4, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - #118962 (Annotate some bugs) - #118969 (coverage: Use `Waker::noop` in async tests) - #118974 (Annotate panic! reasons during enum layout) Failed merges: - #111658 (Refactor pre-getopts command line argument handling) - #117449 (Avoid silencing relevant follow-up errors) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4d1bd0d + 88a9619 commit e6707df

File tree

51 files changed

+474
-589
lines changed

Some content is hidden

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

51 files changed

+474
-589
lines changed

compiler/rustc_abi/src/layout.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub trait LayoutCalculator {
382382
*offset += this_offset;
383383
}
384384
}
385-
_ => {
385+
FieldsShape::Primitive | FieldsShape::Array { .. } | FieldsShape::Union(..) => {
386386
panic!("Layout of fields should be Arbitrary for variants")
387387
}
388388
}
@@ -600,7 +600,9 @@ pub trait LayoutCalculator {
600600
variant.size = new_ity_size;
601601
}
602602
}
603-
_ => panic!(),
603+
FieldsShape::Primitive | FieldsShape::Array { .. } | FieldsShape::Union(..) => {
604+
panic!("encountered a non-arbitrary layout during enum layout")
605+
}
604606
}
605607
}
606608
}
@@ -628,7 +630,7 @@ pub trait LayoutCalculator {
628630
let mut common_prim_initialized_in_all_variants = true;
629631
for (field_layouts, layout_variant) in iter::zip(variants, &layout_variants) {
630632
let FieldsShape::Arbitrary { ref offsets, .. } = layout_variant.fields else {
631-
panic!();
633+
panic!("encountered a non-arbitrary layout during enum layout");
632634
};
633635
// We skip *all* ZST here and later check if we are good in terms of alignment.
634636
// This lets us handle some cases involving aligned ZST.
@@ -681,7 +683,7 @@ pub trait LayoutCalculator {
681683
assert_eq!(memory_index.raw, [0, 1]);
682684
offsets
683685
}
684-
_ => panic!(),
686+
_ => panic!("encountered a non-arbitrary layout during enum layout"),
685687
};
686688
if pair_offsets[FieldIdx::new(0)] == Size::ZERO
687689
&& pair_offsets[FieldIdx::new(1)] == *offset
@@ -758,7 +760,9 @@ pub trait LayoutCalculator {
758760
Variants::Multiple { tag, tag_encoding, tag_field, .. } => {
759761
Variants::Multiple { tag, tag_encoding, tag_field, variants: best_layout.variants }
760762
}
761-
_ => panic!(),
763+
Variants::Single { .. } => {
764+
panic!("encountered a single-variant enum during multi-variant layout")
765+
}
762766
};
763767
Some(best_layout.layout)
764768
}
@@ -1154,7 +1158,11 @@ fn univariant<
11541158
assert_eq!(memory_index.raw, [0, 1]);
11551159
offsets
11561160
}
1157-
_ => panic!(),
1161+
FieldsShape::Primitive
1162+
| FieldsShape::Array { .. }
1163+
| FieldsShape::Union(..) => {
1164+
panic!("encountered a non-arbitrary layout during enum layout")
1165+
}
11581166
};
11591167
if offsets[i] == pair_offsets[FieldIdx::new(0)]
11601168
&& offsets[j] == pair_offsets[FieldIdx::new(1)]

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
}

0 commit comments

Comments
 (0)