Skip to content

Commit 2690737

Browse files
committed
Auto merge of #121937 - GuillaumeGomez:rollup-9684vg3, r=GuillaumeGomez
Rollup of 3 pull requests Successful merges: - #121917 (Add new `pattern_complexity` attribute to add possibility to limit and check recursion in pattern matching) - #121933 (Add missing get_name for wasm::thread.) - #121934 (rustc_log: expose tracing-tree "wraparound" in an env var) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 9e73597 + e634a0a commit 2690737

File tree

15 files changed

+219
-7
lines changed

15 files changed

+219
-7
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
929929
omit_gdb_pretty_printer_section, Normal, template!(Word), WarnFollowing,
930930
"the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
931931
),
932+
rustc_attr!(
933+
TEST, pattern_complexity, CrateLevel, template!(NameValueStr: "N"),
934+
ErrorFollowing, @only_local: true,
935+
),
932936
];
933937

934938
pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ declare_features! (
213213
(internal, negative_bounds, "1.71.0", None),
214214
/// Allows using `#[omit_gdb_pretty_printer_section]`.
215215
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
216+
/// Set the maximum pattern complexity allowed (not limited by default).
217+
(internal, pattern_complexity, "CURRENT_RUSTC_VERSION", None),
216218
/// Allows using `#[prelude_import]` on glob `use` items.
217219
(internal, prelude_import, "1.2.0", None),
218220
/// Used to identify crates that contain the profiler runtime.

compiler/rustc_log/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub struct LoggerConfig {
5757
pub verbose_entry_exit: Result<String, VarError>,
5858
pub verbose_thread_ids: Result<String, VarError>,
5959
pub backtrace: Result<String, VarError>,
60+
pub wraptree: Result<String, VarError>,
6061
}
6162

6263
impl LoggerConfig {
@@ -67,6 +68,7 @@ impl LoggerConfig {
6768
verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
6869
verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
6970
backtrace: env::var(format!("{env}_BACKTRACE")),
71+
wraptree: env::var(format!("{env}_WRAPTREE")),
7072
}
7173
}
7274
}
@@ -99,7 +101,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
99101
Err(_) => false,
100102
};
101103

102-
let layer = tracing_tree::HierarchicalLayer::default()
104+
let mut layer = tracing_tree::HierarchicalLayer::default()
103105
.with_writer(io::stderr)
104106
.with_indent_lines(true)
105107
.with_ansi(color_logs)
@@ -110,6 +112,16 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
110112
.with_thread_ids(verbose_thread_ids)
111113
.with_thread_names(verbose_thread_ids);
112114

115+
match cfg.wraptree {
116+
Ok(v) => match v.parse::<usize>() {
117+
Ok(v) => {
118+
layer = layer.with_wraparound(v);
119+
}
120+
Err(_) => return Err(Error::InvalidWraptree(v)),
121+
},
122+
Err(_) => {} // no wraptree
123+
}
124+
113125
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
114126
match cfg.backtrace {
115127
Ok(str) => {
@@ -164,6 +176,7 @@ pub fn stderr_isatty() -> bool {
164176
pub enum Error {
165177
InvalidColorValue(String),
166178
NonUnicodeColorValue,
179+
InvalidWraptree(String),
167180
}
168181

169182
impl std::error::Error for Error {}
@@ -179,6 +192,10 @@ impl Display for Error {
179192
formatter,
180193
"non-Unicode log color value: expected one of always, never, or auto",
181194
),
195+
Error::InvalidWraptree(value) => write!(
196+
formatter,
197+
"invalid log WRAPTREE value '{value}': expected a non-negative integer",
198+
),
182199
}
183200
}
184201
}

compiler/rustc_middle/src/middle/limits.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,21 @@ pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
4040
}
4141

4242
fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
43+
match get_limit_size(krate_attrs, sess, name) {
44+
Some(size) => Limit::new(size),
45+
None => Limit::new(default),
46+
}
47+
}
48+
49+
pub fn get_limit_size(krate_attrs: &[Attribute], sess: &Session, name: Symbol) -> Option<usize> {
4350
for attr in krate_attrs {
4451
if !attr.has_name(name) {
4552
continue;
4653
}
4754

4855
if let Some(s) = attr.value_str() {
4956
match s.as_str().parse() {
50-
Ok(n) => return Limit::new(n),
57+
Ok(n) => return Some(n),
5158
Err(e) => {
5259
let value_span = attr
5360
.meta()
@@ -69,5 +76,5 @@ fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: u
6976
}
7077
}
7178
}
72-
return Limit::new(default);
79+
None
7380
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_hir as hir;
1717
use rustc_hir::def::*;
1818
use rustc_hir::def_id::LocalDefId;
1919
use rustc_hir::HirId;
20+
use rustc_middle::middle::limits::get_limit_size;
2021
use rustc_middle::thir::visit::Visitor;
2122
use rustc_middle::thir::*;
2223
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -26,7 +27,7 @@ use rustc_session::lint::builtin::{
2627
};
2728
use rustc_session::Session;
2829
use rustc_span::hygiene::DesugaringKind;
29-
use rustc_span::Span;
30+
use rustc_span::{sym, Span};
3031

3132
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
3233
let typeck_results = tcx.typeck(def_id);
@@ -403,8 +404,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
403404
arms: &[MatchArm<'p, 'tcx>],
404405
scrut_ty: Ty<'tcx>,
405406
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
407+
let pattern_complexity_limit =
408+
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity);
406409
let report =
407-
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty).map_err(|err| {
410+
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty, pattern_complexity_limit)
411+
.map_err(|err| {
408412
self.error = Err(err);
409413
err
410414
})?;

compiler/rustc_pattern_analysis/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ pub trait TypeCx: Sized + fmt::Debug {
142142
_overlaps_with: &[&DeconstructedPat<Self>],
143143
) {
144144
}
145+
146+
/// The maximum pattern complexity limit was reached.
147+
fn complexity_exceeded(&self) -> Result<(), Self::Error>;
145148
}
146149

147150
/// The arm of a match expression.
@@ -167,10 +170,12 @@ pub fn analyze_match<'p, 'tcx>(
167170
tycx: &RustcMatchCheckCtxt<'p, 'tcx>,
168171
arms: &[rustc::MatchArm<'p, 'tcx>],
169172
scrut_ty: Ty<'tcx>,
173+
pattern_complexity_limit: Option<usize>,
170174
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
171175
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
172176
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);
173-
let report = compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity)?;
177+
let report =
178+
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
174179

175180
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
176181
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,11 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
895895
errors::OverlappingRangeEndpoints { overlap: overlaps, range: pat_span },
896896
);
897897
}
898+
899+
fn complexity_exceeded(&self) -> Result<(), Self::Error> {
900+
let span = self.whole_match_span.unwrap_or(self.scrut_span);
901+
Err(self.tcx.dcx().span_err(span, "reached pattern complexity limit"))
902+
}
898903
}
899904

900905
/// Recursively expand this pattern into its subpatterns. Only useful for or-patterns.

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,21 @@ struct UsefulnessCtxt<'a, Cx: TypeCx> {
734734
/// Collect the patterns found useful during usefulness checking. This is used to lint
735735
/// unreachable (sub)patterns.
736736
useful_subpatterns: FxHashSet<PatId>,
737+
complexity_limit: Option<usize>,
738+
complexity_level: usize,
739+
}
740+
741+
impl<'a, Cx: TypeCx> UsefulnessCtxt<'a, Cx> {
742+
fn increase_complexity_level(&mut self, complexity_add: usize) -> Result<(), Cx::Error> {
743+
self.complexity_level += complexity_add;
744+
if self
745+
.complexity_limit
746+
.is_some_and(|complexity_limit| complexity_limit < self.complexity_level)
747+
{
748+
return self.tycx.complexity_exceeded();
749+
}
750+
Ok(())
751+
}
737752
}
738753

739754
/// Context that provides information local to a place under investigation.
@@ -1552,6 +1567,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15521567
}
15531568

15541569
let Some(place) = matrix.head_place() else {
1570+
mcx.increase_complexity_level(matrix.rows().len())?;
15551571
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
15561572
// A row is useful iff it has no (unguarded) rows above it.
15571573
let mut useful = true; // Whether the next row is useful.
@@ -1690,8 +1706,14 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>(
16901706
arms: &[MatchArm<'p, Cx>],
16911707
scrut_ty: Cx::Ty,
16921708
scrut_validity: ValidityConstraint,
1709+
complexity_limit: Option<usize>,
16931710
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
1694-
let mut cx = UsefulnessCtxt { tycx, useful_subpatterns: FxHashSet::default() };
1711+
let mut cx = UsefulnessCtxt {
1712+
tycx,
1713+
useful_subpatterns: FxHashSet::default(),
1714+
complexity_limit,
1715+
complexity_level: 0,
1716+
};
16951717
let mut matrix = Matrix::new(arms, scrut_ty, scrut_validity);
16961718
let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness(&mut cx, &mut matrix)?;
16971719

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,7 @@ symbols! {
13031303
pat,
13041304
pat_param,
13051305
path,
1306+
pattern_complexity,
13061307
pattern_parentheses,
13071308
phantom_data,
13081309
pic,

library/std/src/sys/pal/wasm/atomics/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::ffi::CStr;
2+
use crate::ffi::CString;
23
use crate::io;
34
use crate::num::NonZero;
45
use crate::sys::unsupported;
@@ -17,6 +18,9 @@ impl Thread {
1718
pub fn yield_now() {}
1819

1920
pub fn set_name(_name: &CStr) {}
21+
pub fn get_name() -> Option<CString> {
22+
None
23+
}
2024

2125
pub fn sleep(dur: Duration) {
2226
use crate::arch::wasm32;

0 commit comments

Comments
 (0)