Skip to content

Commit 5d48b7b

Browse files
committed
Move mode to comment defaults
1 parent 25e2b53 commit 5d48b7b

File tree

14 files changed

+140
-117
lines changed

14 files changed

+140
-117
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
* `Config::edition` (replaced by `config.comment_defaults.base().edition`)
2525
* `Config::filter_stdout` (replaced by `config.comment_defaults.base().normalize_stdout`)
2626
* `Config::filter_stderr` (replaced by `config.comment_defaults.base().normalize_stderr`)
27+
* `Config::mode` (replaced by `config.comment_defaults.base().mode`)
2728

2829
## [0.21.2] - 2023-09-27

src/config.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use regex::bytes::Regex;
2-
use spanned::Span;
2+
use spanned::{Span, Spanned};
33

44
use crate::{
55
dependencies::build_dependencies, per_test_config::Comments, CommandBuilder, Match, Mode,
@@ -25,8 +25,6 @@ pub struct Config {
2525
pub target: Option<String>,
2626
/// The folder in which to start searching for .rs files
2727
pub root_dir: PathBuf,
28-
/// The mode in which to run the tests.
29-
pub mode: Mode,
3028
/// The binary to actually execute.
3129
pub program: CommandBuilder,
3230
/// The command to run to obtain the cfgs that the output is supposed to
@@ -73,16 +71,17 @@ impl Config {
7371
#[cfg(windows)]
7472
(Match::Exact(br"\\?\".to_vec()), b"".to_vec()),
7573
];
76-
let _ = comment_defaults.base().normalize_stderr = filters.clone();
77-
let _ = comment_defaults.base().normalize_stdout = filters;
74+
comment_defaults.base().normalize_stderr = filters.clone();
75+
comment_defaults.base().normalize_stdout = filters;
76+
comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
77+
require_patterns: true,
78+
rustfix: RustfixMode::MachineApplicable,
79+
})
80+
.into();
7881
Self {
7982
host: None,
8083
target: None,
8184
root_dir: root_dir.into(),
82-
mode: Mode::Fail {
83-
require_patterns: true,
84-
rustfix: RustfixMode::MachineApplicable,
85-
},
8685
program: CommandBuilder::rustc(),
8786
cfgs: CommandBuilder::cfgs(),
8887
output_conflict_handling: OutputConflictHandling::Bless,
@@ -107,13 +106,14 @@ impl Config {
107106
pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
108107
let mut this = Self {
109108
program: CommandBuilder::cargo(),
110-
mode: Mode::Fail {
111-
require_patterns: true,
112-
rustfix: RustfixMode::Disabled,
113-
},
114109
..Self::rustc(root_dir)
115110
};
116111
this.comment_defaults.base().edition = Default::default();
112+
this.comment_defaults.base().mode = Spanned::dummy(Mode::Fail {
113+
require_patterns: true,
114+
rustfix: RustfixMode::Disabled,
115+
})
116+
.into();
117117
this
118118
}
119119

src/dependencies.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ pub(crate) fn build_dependencies(config: &Config) -> Result<Dependencies> {
5959
}
6060

6161
// Reusable closure for setting up the environment both for artifact generation and `cargo_metadata`
62-
let set_locking = |cmd: &mut Command| match (&config.output_conflict_handling, &config.mode) {
62+
let set_locking = |cmd: &mut Command| match (
63+
&config.output_conflict_handling,
64+
config
65+
.comment_defaults
66+
.base_immut()
67+
.mode
68+
.as_deref()
69+
.unwrap(),
70+
) {
6371
(_, Mode::Yolo { .. }) => {}
6472
(OutputConflictHandling::Error(_), _) => {
6573
cmd.arg("--locked");

src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub enum Error {
3030
/// A ui test checking for success has failure patterns
3131
PatternFoundInPassTest {
3232
/// Span of a flag changing the mode (if changed from default).
33-
mode: Option<Span>,
33+
mode: Span,
3434
/// Span of the pattern
3535
span: Span,
3636
},
@@ -59,6 +59,8 @@ pub enum Error {
5959
/// The character range in which it was defined.
6060
span: Span,
6161
},
62+
/// An invalid setting was used.
63+
ConfigError(String),
6264
/// Conflicting comments
6365
MultipleRevisionsWithResults {
6466
/// The comment being looked for

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use color_eyre::eyre::{eyre, Result};
1414
use crossbeam_channel::{unbounded, Receiver, Sender};
1515
use dependencies::{Build, BuildManager};
1616
use lazy_static::lazy_static;
17-
use parser::{ErrorMatch, MaybeSpanned, OptWithLine, Revisioned, Spanned};
17+
use parser::{ErrorMatch, OptWithLine, Revisioned, Spanned};
1818
use regex::bytes::{Captures, Regex};
1919
use rustc_stderr::{Level, Message};
2020
use spanned::Span;
@@ -48,6 +48,8 @@ pub use config::*;
4848
pub use error::*;
4949
pub use mode::*;
5050

51+
pub use spanned;
52+
5153
/// A filter's match rule.
5254
#[derive(Clone, Debug)]
5355
pub enum Match {
@@ -657,7 +659,7 @@ impl dyn TestStatus {
657659

658660
let (cmd, status, stderr, stdout) = self.run_command(cmd)?;
659661

660-
let mode = config.mode.maybe_override(comments, revision)?;
662+
let mode = comments.mode(revision)?;
661663
let cmd = check_test_result(
662664
cmd,
663665
match *mode {
@@ -767,7 +769,7 @@ fn build_aux_files(
767769
}
768770

769771
fn run_test_binary(
770-
mode: MaybeSpanned<Mode>,
772+
mode: Spanned<Mode>,
771773
path: &Path,
772774
revision: &str,
773775
comments: &Comments,
@@ -1009,7 +1011,6 @@ fn check_test_result(
10091011
diagnostics.messages_from_unknown_file_or_line,
10101012
path,
10111013
&mut errors,
1012-
config,
10131014
revision,
10141015
comments,
10151016
)?;
@@ -1045,7 +1046,6 @@ fn check_annotations(
10451046
mut messages_from_unknown_file_or_line: Vec<Message>,
10461047
path: &Path,
10471048
errors: &mut Errors,
1048-
config: &Config,
10491049
revision: &str,
10501050
comments: &Comments,
10511051
) -> Result<(), Errored> {
@@ -1122,9 +1122,9 @@ fn check_annotations(
11221122
msgs
11231123
};
11241124

1125-
let mode = config.mode.maybe_override(comments, revision)?;
1125+
let mode = comments.mode(revision)?;
11261126

1127-
if !matches!(config.mode, Mode::Yolo { .. }) {
1127+
if !matches!(*mode, Mode::Yolo { .. }) {
11281128
let messages_from_unknown_file_or_line = filter(messages_from_unknown_file_or_line);
11291129
if !messages_from_unknown_file_or_line.is_empty() {
11301130
errors.push(Error::ErrorsWithoutPattern {

src/mode.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use super::Error;
2-
use crate::parser::Comments;
3-
use crate::parser::MaybeSpanned;
4-
use crate::Errored;
52
use std::fmt::Display;
63
use std::process::ExitStatus;
74

@@ -68,16 +65,6 @@ impl Mode {
6865
})
6966
}
7067
}
71-
pub(crate) fn maybe_override(
72-
self,
73-
comments: &Comments,
74-
revision: &str,
75-
) -> Result<MaybeSpanned<Self>, Errored> {
76-
let mode = comments.find_one_for_revision(revision, "mode changes", |r| r.mode.clone())?;
77-
Ok(mode
78-
.into_inner()
79-
.map_or(MaybeSpanned::new_config(self), Into::into))
80-
}
8168
}
8269

8370
impl Display for Mode {

src/parser.rs

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ impl Comments {
5353
) -> Result<OptWithLine<T>, Errored> {
5454
let mut result = None;
5555
let mut errors = vec![];
56-
for rev in self.for_revision(revision) {
56+
for (k, rev) in &self.revisioned {
57+
if !k.iter().any(|r| r == revision) {
58+
continue;
59+
}
5760
if let Some(found) = f(rev).into_inner() {
5861
if result.is_some() {
5962
errors.push(found.line());
@@ -62,6 +65,9 @@ impl Comments {
6265
}
6366
}
6467
}
68+
if result.is_none() {
69+
result = f(&self.revisioned[&[][..]]).into_inner();
70+
}
6571
if errors.is_empty() {
6672
Ok(result.into())
6773
} else {
@@ -99,6 +105,26 @@ impl Comments {
99105
pub fn base(&mut self) -> &mut Revisioned {
100106
self.revisioned.get_mut(&[][..]).unwrap()
101107
}
108+
109+
/// The comments set for all revisions
110+
pub fn base_immut(&self) -> &Revisioned {
111+
self.revisioned.get(&[][..]).unwrap()
112+
}
113+
114+
pub(crate) fn mode(&self, revision: &str) -> Result<Spanned<Mode>, Errored> {
115+
let mode = self
116+
.find_one_for_revision(revision, "`mode` annotations", |r| r.mode.clone())?
117+
.into_inner()
118+
.ok_or_else(|| Errored {
119+
command: Command::new(format!("<finding mode for revision `{revision}`>")),
120+
errors: vec![Error::ConfigError(
121+
"no mode set up in Config::comment_defaults".into(),
122+
)],
123+
stderr: vec![],
124+
stdout: vec![],
125+
})?;
126+
Ok(mode)
127+
}
102128
}
103129

104130
#[derive(Debug, Clone, Default)]
@@ -133,7 +159,7 @@ pub struct Revisioned {
133159
pub aux_builds: Vec<Spanned<PathBuf>>,
134160
/// Set the `--edition` flag on the test.
135161
pub edition: OptWithLine<String>,
136-
/// Overwrites the mode from `Config`.
162+
/// The mode this test is being run in.
137163
pub mode: OptWithLine<Mode>,
138164
pub(crate) needs_asm_support: bool,
139165
/// Don't run [`rustfix`] for this test
@@ -238,6 +264,8 @@ impl Comments {
238264
commands: CommentParser::<_>::commands(),
239265
};
240266

267+
let defaults = std::mem::take(parser.comments.revisioned.get_mut(&[][..]).unwrap());
268+
241269
let mut fallthrough_to = None; // The line that a `|` will refer to.
242270
for (l, line) in content.as_ref().lines().enumerate() {
243271
let l = NonZeroUsize::new(l + 1).unwrap(); // enumerate starts at 0, but line numbers start at 1
@@ -274,6 +302,51 @@ impl Comments {
274302
}
275303
}
276304
}
305+
let Revisioned {
306+
span,
307+
ignore,
308+
only,
309+
stderr_per_bitwidth,
310+
compile_flags,
311+
env_vars,
312+
normalize_stderr,
313+
normalize_stdout,
314+
error_in_other_files,
315+
error_matches,
316+
require_annotations_for_level,
317+
aux_builds,
318+
edition,
319+
mode,
320+
needs_asm_support,
321+
no_rustfix,
322+
} = parser.comments.base();
323+
if span.is_dummy() {
324+
*span = defaults.span;
325+
}
326+
ignore.extend(defaults.ignore);
327+
only.extend(defaults.only);
328+
*stderr_per_bitwidth |= defaults.stderr_per_bitwidth;
329+
compile_flags.extend(defaults.compile_flags);
330+
env_vars.extend(defaults.env_vars);
331+
normalize_stderr.extend(defaults.normalize_stderr);
332+
normalize_stdout.extend(defaults.normalize_stdout);
333+
error_in_other_files.extend(defaults.error_in_other_files);
334+
error_matches.extend(defaults.error_matches);
335+
aux_builds.extend(defaults.aux_builds);
336+
if require_annotations_for_level.is_none() {
337+
*require_annotations_for_level = defaults.require_annotations_for_level;
338+
}
339+
if edition.is_none() {
340+
*edition = defaults.edition;
341+
}
342+
if mode.is_none() {
343+
*mode = defaults.mode;
344+
}
345+
if no_rustfix.is_none() {
346+
*no_rustfix = defaults.no_rustfix;
347+
}
348+
*needs_asm_support |= defaults.needs_asm_support;
349+
277350
if parser.errors.is_empty() {
278351
Ok(parser.comments)
279352
} else {
@@ -418,21 +491,7 @@ impl CommentParser<Comments> {
418491
.entry(revisions)
419492
.or_insert_with(|| Revisioned {
420493
span,
421-
ignore: Default::default(),
422-
only: Default::default(),
423-
stderr_per_bitwidth: Default::default(),
424-
compile_flags: Default::default(),
425-
env_vars: Default::default(),
426-
normalize_stderr: Default::default(),
427-
normalize_stdout: Default::default(),
428-
error_in_other_files: Default::default(),
429-
error_matches: Default::default(),
430-
require_annotations_for_level: Default::default(),
431-
aux_builds: Default::default(),
432-
edition: Default::default(),
433-
mode: Default::default(),
434-
needs_asm_support: Default::default(),
435-
no_rustfix: Default::default(),
494+
..Default::default()
436495
}),
437496
};
438497
f(&mut this);

src/parser/spanned.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
pub use spanned::*;
22

3-
#[derive(Default, Debug, Clone)]
4-
pub struct MaybeSpanned<T> {
5-
data: T,
6-
span: Option<Span>,
7-
}
8-
9-
impl<T> std::ops::Deref for MaybeSpanned<T> {
10-
type Target = T;
11-
12-
fn deref(&self) -> &Self::Target {
13-
&self.data
14-
}
15-
}
16-
17-
impl<T> MaybeSpanned<T> {
18-
/// Values from the `Config` struct don't have lines.
19-
pub fn new_config(data: T) -> Self {
20-
Self { data, span: None }
21-
}
22-
23-
pub fn span(&self) -> Option<Span> {
24-
self.span.clone()
25-
}
26-
}
27-
28-
impl<T> From<Spanned<T>> for MaybeSpanned<T> {
29-
fn from(value: Spanned<T>) -> Self {
30-
Self {
31-
data: value.content,
32-
span: Some(value.span),
33-
}
34-
}
35-
}
36-
373
#[derive(Debug, Clone)]
384
pub struct OptWithLine<T>(Option<Spanned<T>>);
395

0 commit comments

Comments
 (0)