Skip to content

Commit

Permalink
Rollup merge of #129344 - compiler-errors:less-option-unit-diagnostic…
Browse files Browse the repository at this point in the history
…s, 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... 🤔
  • Loading branch information
matthiaskrgr authored Aug 21, 2024
2 parents be80216 + 25ff9b6 commit 937a18d
Show file tree
Hide file tree
Showing 48 changed files with 106 additions and 121 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Multiple different abi names may actually be the same ABI
// If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
let source_map = self.tcx.sess.source_map();
let equivalent = (source_map.span_to_snippet(*prev_sp)
!= source_map.span_to_snippet(*abi_span))
.then_some(());
let equivalent = source_map.span_to_snippet(*prev_sp)
!= source_map.span_to_snippet(*abi_span);

self.dcx().emit_err(AbiSpecifiedMultipleTimes {
abi_span: *abi_span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub struct AbiSpecifiedMultipleTimes {
#[label]
pub prev_span: Span,
#[note]
pub equivalent: Option<()>,
pub equivalent: bool,
}

#[derive(Diagnostic)]
Expand Down
17 changes: 8 additions & 9 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,14 +963,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self_ty,
items,
}) => {
let error =
|annotation_span, annotation, only_trait: bool| errors::InherentImplCannot {
span: self_ty.span,
annotation_span,
annotation,
self_ty: self_ty.span,
only_trait: only_trait.then_some(()),
};
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
span: self_ty.span,
annotation_span,
annotation,
self_ty: self_ty.span,
only_trait,
};

self.with_in_trait_impl(None, |this| {
this.visibility_not_permitted(
Expand Down Expand Up @@ -1195,7 +1194,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} else if where_clauses.after.has_where_token {
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
span: where_clauses.after.span,
help: self.session.is_nightly_build().then_some(()),
help: self.session.is_nightly_build(),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ pub struct InherentImplCannot<'a> {
#[label(ast_passes_type)]
pub self_ty: Span,
#[note(ast_passes_only_trait)]
pub only_trait: Option<()>,
pub only_trait: bool,
}

#[derive(Diagnostic)]
Expand Down Expand Up @@ -528,7 +528,7 @@ pub struct WhereClauseAfterTypeAlias {
#[primary_span]
pub span: Span,
#[help]
pub help: Option<()>,
pub help: bool,
}

#[derive(Diagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ pub fn find_deprecation(
sess.dcx().emit_err(
session_diagnostics::DeprecatedItemSuggestion {
span: mi.span,
is_nightly: sess.is_nightly_build().then_some(()),
is_nightly: sess.is_nightly_build(),
details: (),
},
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub(crate) struct DeprecatedItemSuggestion {
pub span: Span,

#[help]
pub is_nightly: Option<()>,
pub is_nightly: bool,

#[note]
pub details: (),
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0010).then_some(()),
teach: ccx.tcx.sess.teach(E0010),
})
}
}
Expand Down Expand Up @@ -444,16 +444,16 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
if let hir::ConstContext::Static(_) = ccx.const_kind() {
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
span,
opt_help: Some(()),
opt_help: true,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0492).then_some(()),
teach: ccx.tcx.sess.teach(E0492),
})
} else {
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
span,
opt_help: None,
opt_help: false,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0492).then_some(()),
teach: ccx.tcx.sess.teach(E0492),
})
}
}
Expand Down Expand Up @@ -481,12 +481,12 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::UnallowedMutableRaw {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764).then_some(()),
teach: ccx.tcx.sess.teach(E0764),
}),
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::UnallowedMutableRefs {
span,
kind: ccx.const_kind(),
teach: ccx.tcx.sess.teach(E0764).then_some(()),
teach: ccx.tcx.sess.teach(E0764),
}),
}
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub(crate) struct UnallowedMutableRefs {
pub span: Span,
pub kind: ConstContext,
#[note(const_eval_teach_note)]
pub teach: Option<()>,
pub teach: bool,
}

#[derive(Diagnostic)]
Expand All @@ -161,7 +161,7 @@ pub(crate) struct UnallowedMutableRaw {
pub span: Span,
pub kind: ConstContext,
#[note(const_eval_teach_note)]
pub teach: Option<()>,
pub teach: bool,
}
#[derive(Diagnostic)]
#[diag(const_eval_non_const_fmt_macro_call, code = E0015)]
Expand Down Expand Up @@ -196,7 +196,7 @@ pub(crate) struct UnallowedHeapAllocations {
pub span: Span,
pub kind: ConstContext,
#[note(const_eval_teach_note)]
pub teach: Option<()>,
pub teach: bool,
}

#[derive(Diagnostic)]
Expand All @@ -214,10 +214,10 @@ pub(crate) struct InteriorMutableDataRefer {
#[label]
pub span: Span,
#[help]
pub opt_help: Option<()>,
pub opt_help: bool,
pub kind: ConstContext,
#[note(const_eval_teach_note)]
pub teach: Option<()>,
pub teach: bool,
}

#[derive(Diagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub(crate) struct IncompleteParse<'a> {
pub macro_path: &'a ast::Path,
pub kind_name: &'a str,
#[note(expand_macro_expands_to_match_arm)]
pub expands_to_match_arm: Option<()>,
pub expands_to_match_arm: bool,

#[suggestion(
expand_suggestion_add_semi,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ pub(crate) fn ensure_complete_parse<'a>(
label_span: span,
macro_path,
kind_name,
expands_to_match_arm: expands_to_match_arm.then_some(()),
expands_to_match_arm,
add_semicolon,
});
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
// * compare the param span to the pred span to detect lone user-written `Sized` bounds
let has_explicit_bounds = bounded_params.is_empty()
|| (*bounded_params).get(&param.index).is_some_and(|&&pred_sp| pred_sp != span);
let const_param_help = (!has_explicit_bounds).then_some(());
let const_param_help = !has_explicit_bounds;

let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
span,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,8 +1972,7 @@ fn report_bivariance<'tcx>(
}

let const_param_help =
matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds)
.then_some(());
matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds);

let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter {
span: param.span,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ pub(crate) struct UnusedGenericParameter {
#[subdiagnostic]
pub help: UnusedGenericParameterHelp,
#[help(hir_analysis_const_param_help)]
pub const_param_help: Option<()>,
pub const_param_help: bool,
}

#[derive(Diagnostic)]
Expand Down Expand Up @@ -1643,9 +1643,9 @@ pub(crate) struct UnconstrainedGenericParameter {
pub param_name: Symbol,
pub param_def_kind: &'static str,
#[note(hir_analysis_const_param_note)]
pub const_param_note: Option<()>,
pub const_param_note: bool,
#[note(hir_analysis_const_param_note2)]
pub const_param_note2: Option<()>,
pub const_param_note2: bool,
}

#[derive(Diagnostic)]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir_analysis/src/impl_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ fn enforce_impl_params_are_constrained(
}
};
if err {
let const_param_note =
matches!(param.kind, ty::GenericParamDefKind::Const { .. }).then_some(());
let const_param_note = matches!(param.kind, ty::GenericParamDefKind::Const { .. });
let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter {
span: tcx.def_span(param.def_id),
param_name: param.name,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
span: self.span,
expr_ty: self.expr_ty,
cast_ty: fcx.ty_to_string(self.cast_ty),
teach: fcx.tcx.sess.teach(E0607).then_some(()),
teach: fcx.tcx.sess.teach(E0607),
});
}
CastError::IntToFatCast(known_metadata) => {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ pub(crate) struct CastThinPointerToFatPointer<'tcx> {
pub expr_ty: Ty<'tcx>,
pub cast_ty: String,
#[note(hir_typeck_teach_help)]
pub(crate) teach: Option<()>,
pub(crate) teach: bool,
}

#[derive(Diagnostic)]
Expand All @@ -720,7 +720,7 @@ pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
pub sugg_span: Option<Span>,
pub replace: String,
#[help]
pub help: Option<()>,
pub help: bool,
#[note(hir_typeck_teach_help)]
pub(crate) teach: Option<()>,
pub(crate) teach: bool,
}
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
let (sugg_span, replace, help) =
if let Ok(snippet) = sess.source_map().span_to_snippet(span) {
(Some(span), format!("{snippet} as {cast_ty}"), None)
(Some(span), format!("{snippet} as {cast_ty}"), false)
} else {
(None, "".to_string(), Some(()))
(None, "".to_string(), true)
};

sess.dcx().emit_err(errors::PassToVariadicFunction {
Expand All @@ -418,7 +418,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
help,
replace,
sugg_span,
teach: sess.teach(E0617).then_some(()),
teach: sess.teach(E0617),
});
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub struct CreateLock<'a> {
pub lock_err: std::io::Error,
pub session_dir: &'a Path,
#[note(incremental_lock_unsupported)]
pub is_unsupported_lock: Option<()>,
pub is_unsupported_lock: bool,
#[help(incremental_cargo_help_1)]
#[help(incremental_cargo_help_2)]
pub is_cargo: Option<()>,
pub is_cargo: bool,
}

#[derive(Diagnostic)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,12 @@ fn lock_directory(
// the lock should be exclusive
Ok(lock) => Ok((lock, lock_file_path)),
Err(lock_err) => {
let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err).then_some(());
let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err);
Err(sess.dcx().emit_err(errors::CreateLock {
lock_err,
session_dir,
is_unsupported_lock,
is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
is_cargo: rustc_session::utils::was_invoked_from_cargo(),
}))
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ impl UnreachablePub {
BuiltinUnreachablePub {
what,
suggestion: (vis_span, applicability),
help: exportable.then_some(()),
help: exportable,
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct UnknownToolInScopedLint {
pub tool_name: Symbol,
pub lint_name: String,
#[help]
pub is_nightly_build: Option<()>,
pub is_nightly_build: bool,
}

#[derive(Diagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
&& tool_filter.map_or(true, |filter| expectation.lint_tool == Some(filter))
{
let rationale = expectation.reason.map(|rationale| ExpectationNote { rationale });
let note = expectation.is_unfulfilled_lint_expectations.then_some(());
let note = expectation.is_unfulfilled_lint_expectations;
tcx.emit_node_span_lint(
UNFULFILLED_LINT_EXPECTATIONS,
*hir_id,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
span: tool_ident.map(|ident| ident.span),
tool_name: tool_name.unwrap(),
lint_name: pprust::path_to_string(&meta_item.path),
is_nightly_build: sess.is_nightly_build().then_some(()),
is_nightly_build: sess.is_nightly_build(),
});
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub struct BuiltinUnreachablePub<'a> {
#[suggestion(code = "pub(crate)")]
pub suggestion: (Span, Applicability),
#[help]
pub help: Option<()>,
pub help: bool,
}

#[derive(LintDiagnostic)]
Expand Down Expand Up @@ -572,7 +572,7 @@ pub struct Expectation {
#[subdiagnostic]
pub rationale: Option<ExpectationNote>,
#[note]
pub note: Option<()>,
pub note: bool,
}

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -756,15 +756,15 @@ pub enum InvalidReferenceCastingDiag<'tcx> {
#[label]
orig_cast: Option<Span>,
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
ty_has_interior_mutability: Option<()>,
ty_has_interior_mutability: bool,
},
#[diag(lint_invalid_reference_casting_assign_to_ref)]
#[note(lint_invalid_reference_casting_note_book)]
AssignToRef {
#[label]
orig_cast: Option<Span>,
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
ty_has_interior_mutability: Option<()>,
ty_has_interior_mutability: bool,
},
#[diag(lint_invalid_reference_casting_bigger_layout)]
#[note(lint_layout)]
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
&& let Some(ty_has_interior_mutability) =
is_cast_from_ref_to_mut_ptr(cx, init, &mut peel_casts)
{
let ty_has_interior_mutability = ty_has_interior_mutability.then_some(());

cx.emit_span_lint(
INVALID_REFERENCE_CASTING,
expr.span,
Expand Down
Loading

0 comments on commit 937a18d

Please sign in to comment.