Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e9a50b8

Browse filesBrowse files
authored
Rollup merge of rust-lang#140307 - mejrs:condition_parser, r=nnethercote
Refactor rustc_on_unimplemented's filter parser Followup to rust-lang#139091; I plan on moving most of this code into `rustc_attr_parsing` at some point, but want to land this separately first. I have taken care to preserve the original behavior as much as I could: - All but one of the new error variants are replacements for the ones originally emitted by the cfg parsing machinery; so these errors are not "new". - the `InvalidFlag` variant is new, this PR turns this (from being ignored and silently doing nothing) into an error: ```rust #[rustc_on_unimplemented(on(something, message = "y"))] //~^ ERROR invalid boolean flag //~^^ NOTE expected one of `crate_local`, `direct` or `from_desugaring`, not `something` trait InvalidFlag {} ``` This does not occur anywhere except in this test. I couldn't find a way that I liked to keep allowing this or to do nothing, erroring was the cleanest solution. - There are a bunch of FIXME throughout this and the previous PR, I plan on addressing those in follow up prs.. Finally, this gets rid of the "longest" dependency in rustc: ![image](https://github.com/user-attachments/assets/3c3eb3a0-b7b3-40d9-aada-a752e28c8678)
2 parents c44e1d6 + 4671081 commit e9a50b8
Copy full SHA for e9a50b8

File tree

Expand file treeCollapse file tree

8 files changed

+510
-194
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+510
-194
lines changed

‎Cargo.lock

Copy file name to clipboardExpand all lines: Cargo.lock
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4497,7 +4497,6 @@ dependencies = [
44974497
"itertools",
44984498
"rustc_abi",
44994499
"rustc_ast",
4500-
"rustc_attr_parsing",
45014500
"rustc_data_structures",
45024501
"rustc_errors",
45034502
"rustc_fluent_macro",

‎compiler/rustc_trait_selection/Cargo.toml

Copy file name to clipboardExpand all lines: compiler/rustc_trait_selection/Cargo.toml
-1Lines changed: 0 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: compiler/rustc_trait_selection/messages.ftl
+16-10Lines changed: 16 additions & 10 deletions
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 identifier inside this `on`-clause
335+
.label = expected an identifier here, not `{$path}`
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_flag = invalid flag in `on`-clause
339+
.label = expected one of the `crate_local`, `direct` or `from_desugaring` flags, 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_missing_value = this attribute must have a value
343+
.label = expected value here
344+
.note = e.g. `#[rustc_on_unimplemented(message="foo")]`
345+
trait_selection_rustc_on_unimplemented_unsupported_literal_in_on = 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

Copy file name to clipboardExpand all lines: compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs
+23-29Lines changed: 23 additions & 29 deletions
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,16 @@ 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;
20-
use crate::error_reporting::traits::on_unimplemented_condition::{Condition, ConditionOptions};
20+
use crate::error_reporting::traits::on_unimplemented_condition::{
21+
ConditionOptions, OnUnimplementedCondition,
22+
};
2123
use crate::error_reporting::traits::on_unimplemented_format::{
2224
Ctx, FormatArgs, FormatString, FormatWarning,
2325
};
24-
use crate::errors::{
25-
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
26-
};
26+
use crate::errors::{InvalidOnClause, NoValueInOnUnimplemented};
2727
use crate::infer::InferCtxtExt;
2828

2929
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
@@ -306,21 +306,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
306306
#[derive(Clone, Debug)]
307307
pub struct OnUnimplementedFormatString {
308308
/// Symbol of the format string, i.e. `"content"`
309-
pub symbol: Symbol,
309+
symbol: Symbol,
310310
///The span of the format string, i.e. `"content"`
311-
pub span: Span,
312-
pub is_diagnostic_namespace_variant: bool,
311+
span: Span,
312+
is_diagnostic_namespace_variant: bool,
313313
}
314314

315315
#[derive(Debug)]
316316
pub struct OnUnimplementedDirective {
317-
pub condition: Option<Condition>,
318-
pub subcommands: Vec<OnUnimplementedDirective>,
319-
pub message: Option<(Span, OnUnimplementedFormatString)>,
320-
pub label: Option<(Span, OnUnimplementedFormatString)>,
321-
pub notes: Vec<OnUnimplementedFormatString>,
322-
pub parent_label: Option<OnUnimplementedFormatString>,
323-
pub append_const_msg: Option<AppendConstMessage>,
317+
condition: Option<OnUnimplementedCondition>,
318+
subcommands: Vec<OnUnimplementedDirective>,
319+
message: Option<(Span, OnUnimplementedFormatString)>,
320+
label: Option<(Span, OnUnimplementedFormatString)>,
321+
notes: Vec<OnUnimplementedFormatString>,
322+
parent_label: Option<OnUnimplementedFormatString>,
323+
append_const_msg: Option<AppendConstMessage>,
324324
}
325325

326326
/// For the `#[rustc_on_unimplemented]` attribute
@@ -427,18 +427,12 @@ impl<'tcx> OnUnimplementedDirective {
427427
} else {
428428
let cond = item_iter
429429
.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() })
430+
.ok_or_else(|| tcx.dcx().emit_err(InvalidOnClause::Empty { span }))?;
431+
432+
match OnUnimplementedCondition::parse(cond) {
433+
Ok(condition) => Some(condition),
434+
Err(e) => return Err(tcx.dcx().emit_err(e)),
435+
}
442436
};
443437

444438
let mut message = None;
@@ -724,7 +718,7 @@ impl<'tcx> OnUnimplementedDirective {
724718
result
725719
}
726720

727-
pub fn evaluate(
721+
pub(crate) fn evaluate(
728722
&self,
729723
tcx: TyCtxt<'tcx>,
730724
trait_ref: ty::TraitRef<'tcx>,
@@ -744,7 +738,7 @@ impl<'tcx> OnUnimplementedDirective {
744738
for command in self.subcommands.iter().chain(Some(self)).rev() {
745739
debug!(?command);
746740
if let Some(ref condition) = command.condition
747-
&& !condition.matches_predicate(tcx, condition_options)
741+
&& !condition.matches_predicate(condition_options)
748742
{
749743
debug!("evaluate: skipping {:?} due to condition", command);
750744
continue;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.