Skip to content

Commit 059cc12

Browse files
committed
Refactor rustc_on_unimplemented's filter parser
1 parent d2d844c commit 059cc12

File tree

8 files changed

+404
-123
lines changed

8 files changed

+404
-123
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4520,7 +4520,6 @@ dependencies = [
45204520
"itertools",
45214521
"rustc_abi",
45224522
"rustc_ast",
4523-
"rustc_attr_parsing",
45244523
"rustc_data_structures",
45254524
"rustc_errors",
45264525
"rustc_fluent_macro",

compiler/rustc_trait_selection/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = "2024"
88
itertools = "0.12"
99
rustc_abi = { path = "../rustc_abi" }
1010
rustc_ast = { path = "../rustc_ast" }
11-
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1211
rustc_data_structures = { path = "../rustc_data_structures" }
1312
rustc_errors = { path = "../rustc_errors" }
1413
rustc_fluent_macro = { path = "../rustc_fluent_macro" }

compiler/rustc_trait_selection/messages.ftl

+16-10
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ trait_selection_dtcs_has_req_note = the used `impl` has a `'static` requirement
148148
trait_selection_dtcs_introduces_requirement = calling this method introduces the `impl`'s `'static` requirement
149149
trait_selection_dtcs_suggestion = consider relaxing the implicit `'static` requirement
150150
151-
trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]`
152-
.label = empty on-clause here
153-
154151
trait_selection_explicit_lifetime_required_sugg_with_ident = add explicit lifetime `{$named}` to the type of `{$simple_ident}`
155152
156153
trait_selection_explicit_lifetime_required_sugg_with_param_type = add explicit lifetime `{$named}` to type
@@ -187,9 +184,6 @@ trait_selection_inherent_projection_normalization_overflow = overflow evaluating
187184
trait_selection_invalid_format_specifier = invalid format specifier
188185
.help = no format specifier are supported in this position
189186
190-
trait_selection_invalid_on_clause_in_rustc_on_unimplemented = invalid `on`-clause in `#[rustc_on_unimplemented]`
191-
.label = invalid on-clause here
192-
193187
trait_selection_label_bad = {$bad_kind ->
194188
*[other] cannot infer type
195189
[more_info] cannot infer {$prefix_kind ->
@@ -237,10 +231,6 @@ trait_selection_negative_positive_conflict = found both positive and negative im
237231
.positive_implementation_here = positive implementation here
238232
.positive_implementation_in_crate = positive implementation in crate `{$positive_impl_cname}`
239233
240-
trait_selection_no_value_in_rustc_on_unimplemented = this attribute must have a valid value
241-
.label = expected value here
242-
.note = eg `#[rustc_on_unimplemented(message="foo")]`
243-
244234
trait_selection_nothing = {""}
245235
246236
trait_selection_oc_cant_coerce_force_inline =
@@ -339,6 +329,22 @@ trait_selection_ril_introduced_by = requirement introduced by this return type
339329
trait_selection_ril_introduced_here = `'static` requirement introduced here
340330
trait_selection_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type
341331
332+
trait_selection_rustc_on_unimplemented_empty_on_clause = empty `on`-clause in `#[rustc_on_unimplemented]`
333+
.label = empty `on`-clause here
334+
trait_selection_rustc_on_unimplemented_expected_identifier = expected an ident inside this `on`-clause
335+
.label = expected an ident here, not `{$predicate}`
336+
trait_selection_rustc_on_unimplemented_expected_one_predicate_in_not = expected a single predicate in `not(..)`
337+
.label = unexpected quantity of predicates here
338+
trait_selection_rustc_on_unimplemented_invalid_boolean = invalid boolean flag
339+
.label = expected one of `crate_local`, `direct` or `from_desugaring`, not `{$invalid_flag}`
340+
trait_selection_rustc_on_unimplemented_invalid_predicate = this predicate is invalid
341+
.label = expected one of `any`, `all` or `not` here, not `{$invalid_pred}`
342+
trait_selection_rustc_on_unimplemented_no_value = this attribute must have a valid value
343+
.label = expected value here
344+
.note = eg `#[rustc_on_unimplemented(message="foo")]`
345+
trait_selection_rustc_on_unimplemented_on_clause_literal = literals inside `on`-clauses are not supported
346+
.label = unexpected literal here
347+
342348
trait_selection_source_kind_closure_return =
343349
try giving this closure an explicit return type
344350

compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::PathBuf;
44
use rustc_ast::{LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit};
55
use rustc_errors::codes::*;
66
use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
7+
use rustc_hir as hir;
78
use rustc_hir::def_id::{DefId, LocalDefId};
89
use rustc_hir::{AttrArgs, Attribute};
910
use rustc_macros::LintDiagnostic;
@@ -13,17 +14,14 @@ use rustc_middle::ty::{self, GenericArgsRef, GenericParamDef, GenericParamDefKin
1314
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
1415
use rustc_span::{Span, Symbol, sym};
1516
use tracing::{debug, info};
16-
use {rustc_attr_parsing as attr, rustc_hir as hir};
1717

1818
use super::{ObligationCauseCode, PredicateObligation};
1919
use crate::error_reporting::TypeErrCtxt;
2020
use crate::error_reporting::traits::on_unimplemented_condition::{Condition, ConditionOptions};
2121
use crate::error_reporting::traits::on_unimplemented_format::{
2222
Ctx, FormatArgs, FormatString, FormatWarning,
2323
};
24-
use crate::errors::{
25-
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
26-
};
24+
use crate::errors::{InvalidOnClause, NoValueInOnUnimplemented};
2725
use crate::infer::InferCtxtExt;
2826

2927
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
@@ -427,18 +425,12 @@ impl<'tcx> OnUnimplementedDirective {
427425
} else {
428426
let cond = item_iter
429427
.next()
430-
.ok_or_else(|| tcx.dcx().emit_err(EmptyOnClauseInOnUnimplemented { span }))?
431-
.meta_item_or_bool()
432-
.ok_or_else(|| tcx.dcx().emit_err(InvalidOnClauseInOnUnimplemented { span }))?;
433-
attr::eval_condition(cond, &tcx.sess, Some(tcx.features()), &mut |cfg| {
434-
if let Some(value) = cfg.value
435-
&& let Err(guar) = parse_value(value, cfg.span)
436-
{
437-
errored = Some(guar);
438-
}
439-
true
440-
});
441-
Some(Condition { inner: cond.clone() })
428+
.ok_or_else(|| tcx.dcx().emit_err(InvalidOnClause::Empty { span }))?;
429+
430+
match Condition::parse(cond) {
431+
Ok(condition) => Some(condition),
432+
Err(e) => return Err(tcx.dcx().emit_err(e)),
433+
}
442434
};
443435

444436
let mut message = None;
@@ -744,7 +736,7 @@ impl<'tcx> OnUnimplementedDirective {
744736
for command in self.subcommands.iter().chain(Some(self)).rev() {
745737
debug!(?command);
746738
if let Some(ref condition) = command.condition
747-
&& !condition.matches_predicate(tcx, condition_options)
739+
&& !condition.matches_predicate(condition_options)
748740
{
749741
debug!("evaluate: skipping {:?} due to condition", command);
750742
continue;

0 commit comments

Comments
 (0)