Skip to content

Commit

Permalink
Hide diagnostics emitted during --cfg parsing
Browse files Browse the repository at this point in the history
The early error is more than sufficient for fixing the problem.
  • Loading branch information
Mark-Simulacrum committed Sep 15, 2019
1 parent 4576668 commit a678e31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::session::{early_error, early_warn, Session};
use crate::session::search_paths::SearchPath;

use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;

use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
use rustc_target::spec::{Target, TargetTriple};
Expand All @@ -19,6 +20,7 @@ use syntax::parse::{ParseSess, new_parser_from_source_str};
use syntax::parse::token;
use syntax::symbol::{sym, Symbol};
use syntax::feature_gate::UnstableFeatures;
use syntax::source_map::SourceMap;

use errors::emitter::HumanReadableErrorType;
use errors::{ColorConfig, FatalError, Handler};
Expand Down Expand Up @@ -1850,11 +1852,20 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
opts
}

struct NullEmitter;

impl errors::emitter::Emitter for NullEmitter {
fn emit_diagnostic(&mut self, _: &errors::DiagnosticBuilder<'_>) {}
}

// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
syntax::with_default_globals(move || {
let cfg = cfgspecs.into_iter().map(|s| {
let sess = ParseSess::new(FilePathMapping::empty());

let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
let sess = ParseSess::with_span_handler(handler, cm);
let filename = FileName::cfg_spec_source_code(&s);
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/conditional-compilation/cfg-arg-invalid-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// compile-flags: --cfg a{
// error-pattern: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`)
fn main() {}
2 changes: 2 additions & 0 deletions src/test/ui/conditional-compilation/cfg-arg-invalid-6.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: invalid `--cfg` argument: `a{` (expected `key` or `key="value"`)

0 comments on commit a678e31

Please sign in to comment.