Skip to content

Commit 937a18d

Browse files
authored
Rollup merge of #129344 - compiler-errors:less-option-unit-diagnostics, r=jieyouxu
Use `bool` in favor of `Option<()>` for diagnostics We originally only supported `Option<()>` for optional notes/labels, but we now support `bool`. Let's use that, since it usually leads to more readable code. I'm not removing the support from the derive macro, though I guess we could error on it... 🤔
2 parents be80216 + 25ff9b6 commit 937a18d

File tree

48 files changed

+106
-121
lines changed

Some content is hidden

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

48 files changed

+106
-121
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8686
// Multiple different abi names may actually be the same ABI
8787
// If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
8888
let source_map = self.tcx.sess.source_map();
89-
let equivalent = (source_map.span_to_snippet(*prev_sp)
90-
!= source_map.span_to_snippet(*abi_span))
91-
.then_some(());
89+
let equivalent = source_map.span_to_snippet(*prev_sp)
90+
!= source_map.span_to_snippet(*abi_span);
9291

9392
self.dcx().emit_err(AbiSpecifiedMultipleTimes {
9493
abi_span: *abi_span,

compiler/rustc_ast_lowering/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub struct AbiSpecifiedMultipleTimes {
184184
#[label]
185185
pub prev_span: Span,
186186
#[note]
187-
pub equivalent: Option<()>,
187+
pub equivalent: bool,
188188
}
189189

190190
#[derive(Diagnostic)]

compiler/rustc_ast_passes/src/ast_validation.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -963,14 +963,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
963963
self_ty,
964964
items,
965965
}) => {
966-
let error =
967-
|annotation_span, annotation, only_trait: bool| errors::InherentImplCannot {
968-
span: self_ty.span,
969-
annotation_span,
970-
annotation,
971-
self_ty: self_ty.span,
972-
only_trait: only_trait.then_some(()),
973-
};
966+
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
967+
span: self_ty.span,
968+
annotation_span,
969+
annotation,
970+
self_ty: self_ty.span,
971+
only_trait,
972+
};
974973

975974
self.with_in_trait_impl(None, |this| {
976975
this.visibility_not_permitted(
@@ -1195,7 +1194,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11951194
} else if where_clauses.after.has_where_token {
11961195
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
11971196
span: where_clauses.after.span,
1198-
help: self.session.is_nightly_build().then_some(()),
1197+
help: self.session.is_nightly_build(),
11991198
});
12001199
}
12011200
}

compiler/rustc_ast_passes/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ pub struct InherentImplCannot<'a> {
484484
#[label(ast_passes_type)]
485485
pub self_ty: Span,
486486
#[note(ast_passes_only_trait)]
487-
pub only_trait: Option<()>,
487+
pub only_trait: bool,
488488
}
489489

490490
#[derive(Diagnostic)]
@@ -528,7 +528,7 @@ pub struct WhereClauseAfterTypeAlias {
528528
#[primary_span]
529529
pub span: Span,
530530
#[help]
531-
pub help: Option<()>,
531+
pub help: bool,
532532
}
533533

534534
#[derive(Diagnostic)]

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ pub fn find_deprecation(
846846
sess.dcx().emit_err(
847847
session_diagnostics::DeprecatedItemSuggestion {
848848
span: mi.span,
849-
is_nightly: sess.is_nightly_build().then_some(()),
849+
is_nightly: sess.is_nightly_build(),
850850
details: (),
851851
},
852852
);

compiler/rustc_attr/src/session_diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub(crate) struct DeprecatedItemSuggestion {
342342
pub span: Span,
343343

344344
#[help]
345-
pub is_nightly: Option<()>,
345+
pub is_nightly: bool,
346346

347347
#[note]
348348
pub details: (),

compiler/rustc_const_eval/src/check_consts/ops.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
384384
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
385385
span,
386386
kind: ccx.const_kind(),
387-
teach: ccx.tcx.sess.teach(E0010).then_some(()),
387+
teach: ccx.tcx.sess.teach(E0010),
388388
})
389389
}
390390
}
@@ -444,16 +444,16 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
444444
if let hir::ConstContext::Static(_) = ccx.const_kind() {
445445
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
446446
span,
447-
opt_help: Some(()),
447+
opt_help: true,
448448
kind: ccx.const_kind(),
449-
teach: ccx.tcx.sess.teach(E0492).then_some(()),
449+
teach: ccx.tcx.sess.teach(E0492),
450450
})
451451
} else {
452452
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
453453
span,
454-
opt_help: None,
454+
opt_help: false,
455455
kind: ccx.const_kind(),
456-
teach: ccx.tcx.sess.teach(E0492).then_some(()),
456+
teach: ccx.tcx.sess.teach(E0492),
457457
})
458458
}
459459
}
@@ -481,12 +481,12 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
481481
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::UnallowedMutableRaw {
482482
span,
483483
kind: ccx.const_kind(),
484-
teach: ccx.tcx.sess.teach(E0764).then_some(()),
484+
teach: ccx.tcx.sess.teach(E0764),
485485
}),
486486
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::UnallowedMutableRefs {
487487
span,
488488
kind: ccx.const_kind(),
489-
teach: ccx.tcx.sess.teach(E0764).then_some(()),
489+
teach: ccx.tcx.sess.teach(E0764),
490490
}),
491491
}
492492
}

compiler/rustc_const_eval/src/errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub(crate) struct UnallowedMutableRefs {
151151
pub span: Span,
152152
pub kind: ConstContext,
153153
#[note(const_eval_teach_note)]
154-
pub teach: Option<()>,
154+
pub teach: bool,
155155
}
156156

157157
#[derive(Diagnostic)]
@@ -161,7 +161,7 @@ pub(crate) struct UnallowedMutableRaw {
161161
pub span: Span,
162162
pub kind: ConstContext,
163163
#[note(const_eval_teach_note)]
164-
pub teach: Option<()>,
164+
pub teach: bool,
165165
}
166166
#[derive(Diagnostic)]
167167
#[diag(const_eval_non_const_fmt_macro_call, code = E0015)]
@@ -196,7 +196,7 @@ pub(crate) struct UnallowedHeapAllocations {
196196
pub span: Span,
197197
pub kind: ConstContext,
198198
#[note(const_eval_teach_note)]
199-
pub teach: Option<()>,
199+
pub teach: bool,
200200
}
201201

202202
#[derive(Diagnostic)]
@@ -214,10 +214,10 @@ pub(crate) struct InteriorMutableDataRefer {
214214
#[label]
215215
pub span: Span,
216216
#[help]
217-
pub opt_help: Option<()>,
217+
pub opt_help: bool,
218218
pub kind: ConstContext,
219219
#[note(const_eval_teach_note)]
220-
pub teach: Option<()>,
220+
pub teach: bool,
221221
}
222222

223223
#[derive(Diagnostic)]

compiler/rustc_expand/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub(crate) struct IncompleteParse<'a> {
281281
pub macro_path: &'a ast::Path,
282282
pub kind_name: &'a str,
283283
#[note(expand_macro_expands_to_match_arm)]
284-
pub expands_to_match_arm: Option<()>,
284+
pub expands_to_match_arm: bool,
285285

286286
#[suggestion(
287287
expand_suggestion_add_semi,

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ pub(crate) fn ensure_complete_parse<'a>(
10311031
label_span: span,
10321032
macro_path,
10331033
kind_name,
1034-
expands_to_match_arm: expands_to_match_arm.then_some(()),
1034+
expands_to_match_arm,
10351035
add_semicolon,
10361036
});
10371037
}

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
15641564
// * compare the param span to the pred span to detect lone user-written `Sized` bounds
15651565
let has_explicit_bounds = bounded_params.is_empty()
15661566
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
1567-
let const_param_help = (!has_explicit_bounds).then_some(());
1567+
let const_param_help = !has_explicit_bounds;
15681568

15691569
let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
15701570
span,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1972,8 +1972,7 @@ fn report_bivariance<'tcx>(
19721972
}
19731973

19741974
let const_param_help =
1975-
matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds)
1976-
.then_some(());
1975+
matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds);
19771976

19781977
let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
19791978
span: param.span,

compiler/rustc_hir_analysis/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ pub(crate) struct UnusedGenericParameter {
16061606
#[subdiagnostic]
16071607
pub help: UnusedGenericParameterHelp,
16081608
#[help(hir_analysis_const_param_help)]
1609-
pub const_param_help: Option<()>,
1609+
pub const_param_help: bool,
16101610
}
16111611

16121612
#[derive(Diagnostic)]
@@ -1643,9 +1643,9 @@ pub(crate) struct UnconstrainedGenericParameter {
16431643
pub param_name: Symbol,
16441644
pub param_def_kind: &'static str,
16451645
#[note(hir_analysis_const_param_note)]
1646-
pub const_param_note: Option<()>,
1646+
pub const_param_note: bool,
16471647
#[note(hir_analysis_const_param_note2)]
1648-
pub const_param_note2: Option<()>,
1648+
pub const_param_note2: bool,
16491649
}
16501650

16511651
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ fn enforce_impl_params_are_constrained(
137137
}
138138
};
139139
if err {
140-
let const_param_note =
141-
matches!(param.kind, ty::GenericParamDefKind::Const { .. }).then_some(());
140+
let const_param_note = matches!(param.kind, ty::GenericParamDefKind::Const { .. });
142141
let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter {
143142
span: tcx.def_span(param.def_id),
144143
param_name: param.name,

compiler/rustc_hir_typeck/src/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
505505
span: self.span,
506506
expr_ty: self.expr_ty,
507507
cast_ty: fcx.ty_to_string(self.cast_ty),
508-
teach: fcx.tcx.sess.teach(E0607).then_some(()),
508+
teach: fcx.tcx.sess.teach(E0607),
509509
});
510510
}
511511
CastError::IntToFatCast(known_metadata) => {

compiler/rustc_hir_typeck/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ pub(crate) struct CastThinPointerToFatPointer<'tcx> {
706706
pub expr_ty: Ty<'tcx>,
707707
pub cast_ty: String,
708708
#[note(hir_typeck_teach_help)]
709-
pub(crate) teach: Option<()>,
709+
pub(crate) teach: bool,
710710
}
711711

712712
#[derive(Diagnostic)]
@@ -720,7 +720,7 @@ pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
720720
pub sugg_span: Option<Span>,
721721
pub replace: String,
722722
#[help]
723-
pub help: Option<()>,
723+
pub help: bool,
724724
#[note(hir_typeck_teach_help)]
725-
pub(crate) teach: Option<()>,
725+
pub(crate) teach: bool,
726726
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
406406
) {
407407
let (sugg_span, replace, help) =
408408
if let Ok(snippet) = sess.source_map().span_to_snippet(span) {
409-
(Some(span), format!("{snippet} as {cast_ty}"), None)
409+
(Some(span), format!("{snippet} as {cast_ty}"), false)
410410
} else {
411-
(None, "".to_string(), Some(()))
411+
(None, "".to_string(), true)
412412
};
413413

414414
sess.dcx().emit_err(errors::PassToVariadicFunction {
@@ -418,7 +418,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
418418
help,
419419
replace,
420420
sugg_span,
421-
teach: sess.teach(E0617).then_some(()),
421+
teach: sess.teach(E0617),
422422
});
423423
}
424424

compiler/rustc_incremental/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ pub struct CreateLock<'a> {
189189
pub lock_err: std::io::Error,
190190
pub session_dir: &'a Path,
191191
#[note(incremental_lock_unsupported)]
192-
pub is_unsupported_lock: Option<()>,
192+
pub is_unsupported_lock: bool,
193193
#[help(incremental_cargo_help_1)]
194194
#[help(incremental_cargo_help_2)]
195-
pub is_cargo: Option<()>,
195+
pub is_cargo: bool,
196196
}
197197

198198
#[derive(Diagnostic)]

compiler/rustc_incremental/src/persist/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,12 @@ fn lock_directory(
486486
// the lock should be exclusive
487487
Ok(lock) => Ok((lock, lock_file_path)),
488488
Err(lock_err) => {
489-
let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err).then_some(());
489+
let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err);
490490
Err(sess.dcx().emit_err(errors::CreateLock {
491491
lock_err,
492492
session_dir,
493493
is_unsupported_lock,
494-
is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
494+
is_cargo: rustc_session::utils::was_invoked_from_cargo(),
495495
}))
496496
}
497497
}

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ impl UnreachablePub {
13301330
BuiltinUnreachablePub {
13311331
what,
13321332
suggestion: (vis_span, applicability),
1333-
help: exportable.then_some(()),
1333+
help: exportable,
13341334
},
13351335
);
13361336
}

compiler/rustc_lint/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub struct UnknownToolInScopedLint {
7777
pub tool_name: Symbol,
7878
pub lint_name: String,
7979
#[help]
80-
pub is_nightly_build: Option<()>,
80+
pub is_nightly_build: bool,
8181
}
8282

8383
#[derive(Diagnostic)]

compiler/rustc_lint/src/expect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
2424
&& tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
2525
{
2626
let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
27-
let note = expectation.is_unfulfilled_lint_expectations.then_some(());
27+
let note = expectation.is_unfulfilled_lint_expectations;
2828
tcx.emit_node_span_lint(
2929
UNFULFILLED_LINT_EXPECTATIONS,
3030
*hir_id,

compiler/rustc_lint/src/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
936936
span: tool_ident.map(|ident| ident.span),
937937
tool_name: tool_name.unwrap(),
938938
lint_name: pprust::path_to_string(&meta_item.path),
939-
is_nightly_build: sess.is_nightly_build().then_some(()),
939+
is_nightly_build: sess.is_nightly_build(),
940940
});
941941
continue;
942942
}

compiler/rustc_lint/src/lints.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub struct BuiltinUnreachablePub<'a> {
257257
#[suggestion(code = "pub(crate)")]
258258
pub suggestion: (Span, Applicability),
259259
#[help]
260-
pub help: Option<()>,
260+
pub help: bool,
261261
}
262262

263263
#[derive(LintDiagnostic)]
@@ -572,7 +572,7 @@ pub struct Expectation {
572572
#[subdiagnostic]
573573
pub rationale: Option<ExpectationNote>,
574574
#[note]
575-
pub note: Option<()>,
575+
pub note: bool,
576576
}
577577

578578
#[derive(Subdiagnostic)]
@@ -756,15 +756,15 @@ pub enum InvalidReferenceCastingDiag<'tcx> {
756756
#[label]
757757
orig_cast: Option<Span>,
758758
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
759-
ty_has_interior_mutability: Option<()>,
759+
ty_has_interior_mutability: bool,
760760
},
761761
#[diag(lint_invalid_reference_casting_assign_to_ref)]
762762
#[note(lint_invalid_reference_casting_note_book)]
763763
AssignToRef {
764764
#[label]
765765
orig_cast: Option<Span>,
766766
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
767-
ty_has_interior_mutability: Option<()>,
767+
ty_has_interior_mutability: bool,
768768
},
769769
#[diag(lint_invalid_reference_casting_bigger_layout)]
770770
#[note(lint_layout)]

compiler/rustc_lint/src/reference_casting.rs

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
5454
&& let Some(ty_has_interior_mutability) =
5555
is_cast_from_ref_to_mut_ptr(cx, init, &mut peel_casts)
5656
{
57-
let ty_has_interior_mutability = ty_has_interior_mutability.then_some(());
58-
5957
cx.emit_span_lint(
6058
INVALID_REFERENCE_CASTING,
6159
expr.span,

0 commit comments

Comments
 (0)