Skip to content

Commit 7effb31

Browse files
committed
Rollup merge of rust-lang#26452 - michaelsproul:the-second-coming, r=pnkfelix
As per rust-lang#26009 this PR implements a new collation system for extended-error metadata. I've tried to keep it as simple as possible for now, so there's no uniqueness checking and minimal modularity. Although using a lint was discussed in rust-lang#26009 I decided against this because it would require converting the AST output from the plugin back into an internal data-structure. Emitting the metadata from within the plugin prevents this double-handling. We also didn't identify this as the source of the failures last time, although something untoward was definitely happening... With that in mind I would like as much feedback as possible on this before it's merged, I don't want to break the bots again! I've successfully built for my host architecture and I'm building an ARM cross-compiler now to test my assumptions about the various `CFG` variables. Despite the confusing name of `CFG_COMPILER_HOST_TRIPLE` it is actually the compile time target triple, as explained in `mk/target.mk`. ``` # This is the compile-time target-triple for the compiler. For the compiler at # runtime, this should be considered the host-triple. More explanation for why # this exists can be found on issue rust-lang#2400 export CFG_COMPILER_HOST_TRIPLE ``` CC @pnkfelix @brson @nrc @alexcrichton Closes rust-lang#25705, closes rust-lang#26009.
2 parents 6bff14f + 634fced commit 7effb31

File tree

5 files changed

+54
-93
lines changed

5 files changed

+54
-93
lines changed

mk/docs.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ ERR_IDX_GEN = $(RPATH_VAR2_T_$(CFG_BUILD)_H_$(CFG_BUILD)) $(ERR_IDX_GEN_EXE)
7777

7878
D := $(S)src/doc
7979

80-
# FIXME (#25705) eventually may want to put error-index target back here.
81-
DOC_TARGETS := trpl style
80+
DOC_TARGETS := trpl style error-index
8281
COMPILER_DOC_TARGETS :=
8382
DOC_L10N_TARGETS :=
8483

src/error-index-generator/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate serialize as rustc_serialize;
1717
use std::collections::BTreeMap;
1818
use std::fs::{read_dir, File};
1919
use std::io::{Read, Write};
20+
use std::env;
2021
use std::path::Path;
2122
use std::error::Error;
2223

@@ -106,7 +107,8 @@ r##"<!DOCTYPE html>
106107
}
107108

108109
fn main_with_result() -> Result<(), Box<Error>> {
109-
let metadata_dir = get_metadata_dir();
110+
let build_arch = try!(env::var("CFG_BUILD"));
111+
let metadata_dir = get_metadata_dir(&build_arch);
110112
let err_map = try!(load_all_errors(&metadata_dir));
111113
try!(render_error_page(&err_map, Path::new("doc/error-index.html")));
112114
Ok(())

src/librustc_typeck/diagnostics.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,11 @@ struct ListNode {
730730
This type cannot have a well-defined size, because it needs to be arbitrarily
731731
large (since we would be able to nest `ListNode`s to any depth). Specifically,
732732
733-
size of `ListNode` = 1 byte for `head`
734-
+ 1 byte for the discriminant of the `Option`
735-
+ size of `ListNode`
733+
```plain
734+
size of `ListNode` = 1 byte for `head`
735+
+ 1 byte for the discriminant of the `Option`
736+
+ size of `ListNode`
737+
```
736738
737739
One way to fix this is by wrapping `ListNode` in a `Box`, like so:
738740

src/libsyntax/diagnostics/metadata.rs

Lines changed: 29 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,18 @@
1414
//! currently always a crate name.
1515
1616
use std::collections::BTreeMap;
17-
use std::env;
1817
use std::path::PathBuf;
19-
use std::fs::{read_dir, create_dir_all, OpenOptions, File};
20-
use std::io::{Read, Write};
18+
use std::fs::{remove_file, create_dir_all, File};
19+
use std::io::Write;
2120
use std::error::Error;
22-
use rustc_serialize::json::{self, as_json};
21+
use rustc_serialize::json::as_json;
2322

2423
use codemap::Span;
2524
use ext::base::ExtCtxt;
2625
use diagnostics::plugin::{ErrorMap, ErrorInfo};
2726

28-
pub use self::Uniqueness::*;
29-
3027
// Default metadata directory to use for extended error JSON.
31-
const ERROR_METADATA_DIR_DEFAULT: &'static str = "tmp/extended-errors";
32-
33-
// The name of the environment variable that sets the metadata dir.
34-
const ERROR_METADATA_VAR: &'static str = "ERROR_METADATA_DIR";
28+
const ERROR_METADATA_PREFIX: &'static str = "tmp/extended-errors";
3529

3630
/// JSON encodable/decodable version of `ErrorInfo`.
3731
#[derive(PartialEq, RustcDecodable, RustcEncodable)]
@@ -61,84 +55,32 @@ impl ErrorLocation {
6155
}
6256
}
6357

64-
/// Type for describing the uniqueness of a set of error codes, as returned by `check_uniqueness`.
65-
pub enum Uniqueness {
66-
/// All errors in the set checked are unique according to the metadata files checked.
67-
Unique,
68-
/// One or more errors in the set occur in another metadata file.
69-
/// This variant contains the first duplicate error code followed by the name
70-
/// of the metadata file where the duplicate appears.
71-
Duplicate(String, String)
72-
}
73-
74-
/// Get the directory where metadata files should be stored.
75-
pub fn get_metadata_dir() -> PathBuf {
76-
match env::var(ERROR_METADATA_VAR) {
77-
Ok(v) => From::from(v),
78-
Err(_) => From::from(ERROR_METADATA_DIR_DEFAULT)
79-
}
80-
}
81-
82-
/// Get the path where error metadata for the set named by `name` should be stored.
83-
fn get_metadata_path(name: &str) -> PathBuf {
84-
get_metadata_dir().join(format!("{}.json", name))
58+
/// Get the directory where metadata for a given `prefix` should be stored.
59+
///
60+
/// See `output_metadata`.
61+
pub fn get_metadata_dir(prefix: &str) -> PathBuf {
62+
PathBuf::from(ERROR_METADATA_PREFIX).join(prefix)
8563
}
8664

87-
/// Check that the errors in `err_map` aren't present in any metadata files in the
88-
/// metadata directory except the metadata file corresponding to `name`.
89-
pub fn check_uniqueness(name: &str, err_map: &ErrorMap) -> Result<Uniqueness, Box<Error>> {
90-
let metadata_dir = get_metadata_dir();
91-
let metadata_path = get_metadata_path(name);
92-
93-
// Create the error directory if it does not exist.
94-
try!(create_dir_all(&metadata_dir));
95-
96-
// Check each file in the metadata directory.
97-
for entry in try!(read_dir(&metadata_dir)) {
98-
let path = try!(entry).path();
99-
100-
// Skip any existing file for this set.
101-
if path == metadata_path {
102-
continue;
103-
}
104-
105-
// Read the metadata file into a string.
106-
let mut metadata_str = String::new();
107-
try!(
108-
File::open(&path).and_then(|mut f|
109-
f.read_to_string(&mut metadata_str))
110-
);
111-
112-
// Parse the JSON contents.
113-
let metadata: ErrorMetadataMap = try!(json::decode(&metadata_str));
114-
115-
// Check for duplicates.
116-
for err in err_map.keys() {
117-
let err_code = err.as_str();
118-
if metadata.contains_key(err_code) {
119-
return Ok(Duplicate(
120-
err_code.to_string(),
121-
path.to_string_lossy().into_owned()
122-
));
123-
}
124-
}
125-
}
126-
127-
Ok(Unique)
65+
/// Map `name` to a path in the given directory: <directory>/<name>.json
66+
fn get_metadata_path(directory: PathBuf, name: &str) -> PathBuf {
67+
directory.join(format!("{}.json", name))
12868
}
12969

130-
/// Write metadata for the errors in `err_map` to disk, to a file corresponding to `name`.
131-
pub fn output_metadata(ecx: &ExtCtxt, name: &str, err_map: &ErrorMap)
70+
/// Write metadata for the errors in `err_map` to disk, to a file corresponding to `prefix/name`.
71+
///
72+
/// For our current purposes the prefix is the target architecture and the name is a crate name.
73+
/// If an error occurs steps will be taken to ensure that no file is created.
74+
pub fn output_metadata(ecx: &ExtCtxt, prefix: &str, name: &str, err_map: &ErrorMap)
13275
-> Result<(), Box<Error>>
13376
{
134-
let metadata_path = get_metadata_path(name);
77+
// Create the directory to place the file in.
78+
let metadata_dir = get_metadata_dir(prefix);
79+
try!(create_dir_all(&metadata_dir));
13580

136-
// Open the dump file.
137-
let mut dump_file = try!(OpenOptions::new()
138-
.write(true)
139-
.create(true)
140-
.open(&metadata_path)
141-
);
81+
// Open the metadata file.
82+
let metadata_path = get_metadata_path(metadata_dir, name);
83+
let mut metadata_file = try!(File::create(&metadata_path));
14284

14385
// Construct a serializable map.
14486
let json_map = err_map.iter().map(|(k, &ErrorInfo { description, use_site })| {
@@ -150,6 +92,10 @@ pub fn output_metadata(ecx: &ExtCtxt, name: &str, err_map: &ErrorMap)
15092
(key, value)
15193
}).collect::<ErrorMetadataMap>();
15294

153-
try!(write!(&mut dump_file, "{}", as_json(&json_map)));
154-
Ok(())
95+
// Write the data to the file, deleting it if the write fails.
96+
let result = write!(&mut metadata_file, "{}", as_json(&json_map));
97+
if result.is_err() {
98+
try!(remove_file(&metadata_path));
99+
}
100+
Ok(try!(result))
155101
}

src/libsyntax/diagnostics/plugin.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use std::cell::RefCell;
1212
use std::collections::BTreeMap;
13+
use std::env;
1314

1415
use ast;
1516
use ast::{Ident, Name, TokenTree};
@@ -20,6 +21,8 @@ use parse::token;
2021
use ptr::P;
2122
use util::small_vector::SmallVector;
2223

24+
use diagnostics::metadata::output_metadata;
25+
2326
// Maximum width of any line in an extended error description (inclusive).
2427
const MAX_DESCRIPTION_WIDTH: usize = 80;
2528

@@ -154,7 +157,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
154157
token_tree: &[TokenTree])
155158
-> Box<MacResult+'cx> {
156159
assert_eq!(token_tree.len(), 3);
157-
let (_crate_name, name) = match (&token_tree[0], &token_tree[2]) {
160+
let (crate_name, name) = match (&token_tree[0], &token_tree[2]) {
158161
(
159162
// Crate name.
160163
&ast::TtToken(_, token::Ident(ref crate_name, _)),
@@ -164,9 +167,18 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
164167
_ => unreachable!()
165168
};
166169

167-
// FIXME (#25705): we used to ensure error code uniqueness and
168-
// output error description JSON metadata here, but the approach
169-
// employed was too brittle.
170+
// Output error metadata to `tmp/extended-errors/<target arch>/<crate name>.json`
171+
let target_triple = env::var("CFG_COMPILER_HOST_TRIPLE")
172+
.ok().expect("unable to determine target arch from $CFG_COMPILER_HOST_TRIPLE");
173+
174+
with_registered_diagnostics(|diagnostics| {
175+
if let Err(e) = output_metadata(ecx, &target_triple, crate_name, &diagnostics) {
176+
ecx.span_bug(span, &format!(
177+
"error writing metadata for triple `{}` and crate `{}`, error: {}, cause: {:?}",
178+
target_triple, crate_name, e.description(), e.cause()
179+
));
180+
}
181+
});
170182

171183
// Construct the output expression.
172184
let (count, expr) =

0 commit comments

Comments
 (0)