From 67d762d896c8748009d1843ebf9e2e0760ed33a0 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 9 May 2017 10:04:24 +0200 Subject: Refactor suggestion diagnostic API to allow for multiple suggestions --- src/librustc_errors/diagnostic.rs | 18 ++++--- src/librustc_errors/diagnostic_builder.rs | 5 ++ src/librustc_errors/emitter.rs | 75 ++++++++++++++++----------- src/librustc_errors/lib.rs | 85 ++++++++++++++++++++----------- 4 files changed, 116 insertions(+), 67 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 0822f713499..e129e313626 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -23,7 +23,7 @@ pub struct Diagnostic { pub code: Option, pub span: MultiSpan, pub children: Vec, - pub suggestion: Option, + pub suggestions: Vec, } /// For example a note attached to an error. @@ -87,7 +87,7 @@ impl Diagnostic { code: code, span: MultiSpan::new(), children: vec![], - suggestion: None, + suggestions: vec![], } } @@ -204,10 +204,16 @@ impl Diagnostic { /// /// See `diagnostic::CodeSuggestion` for more information. pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self { - assert!(self.suggestion.is_none()); - self.suggestion = Some(CodeSuggestion { - msp: sp.into(), - substitutes: vec![suggestion], + self.suggestions.push(CodeSuggestion { + substitutes: vec![(sp, vec![suggestion])], + msg: msg.to_owned(), + }); + self + } + + pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec) -> &mut Self { + self.suggestions.push(CodeSuggestion { + substitutes: vec![(sp, suggestions)], msg: msg.to_owned(), }); self diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index a9c2bbeba2a..d03a4acb9fc 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -148,6 +148,11 @@ impl<'a> DiagnosticBuilder<'a> { msg: &str, suggestion: String) -> &mut Self); + forward!(pub fn span_suggestions(&mut self, + sp: Span, + msg: &str, + suggestions: Vec) + -> &mut Self); forward!(pub fn set_span>(&mut self, sp: S) -> &mut Self); forward!(pub fn code(&mut self, s: String) -> &mut Self); diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 53999eb9138..564c472305c 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -35,22 +35,37 @@ impl Emitter for EmitterWriter { let mut primary_span = db.span.clone(); let mut children = db.children.clone(); - if let Some(sugg) = db.suggestion.clone() { - assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len()); + if db.suggestions.len() == 1 { + let sugg = &db.suggestions[0]; // don't display multispans as labels if sugg.substitutes.len() == 1 && + // don't display multi-suggestions as labels + sugg.substitutes[0].1.len() == 1 && // don't display long messages as labels sugg.msg.split_whitespace().count() < 10 && // don't display multiline suggestions as labels - sugg.substitutes[0].find('\n').is_none() { - let msg = format!("help: {} `{}`", sugg.msg, sugg.substitutes[0]); - primary_span.push_span_label(sugg.msp.primary_spans()[0], msg); + sugg.substitutes[0].1[0].find('\n').is_none() { + let msg = format!("help: {} `{}`", sugg.msg, sugg.substitutes[0].1[0]); + primary_span.push_span_label(sugg.substitutes[0].0, msg); } else { children.push(SubDiagnostic { level: Level::Help, message: Vec::new(), span: MultiSpan::new(), - render_span: Some(Suggestion(sugg)), + render_span: Some(Suggestion(sugg.clone())), + }); + } + } else { + // if there are multiple suggestions, print them all in full + // to be consistent. We could try to figure out if we can + // make one (or the first one) inline, but that would give + // undue importance to a semi-random suggestion + for sugg in &db.suggestions { + children.push(SubDiagnostic { + level: Level::Help, + message: Vec::new(), + span: MultiSpan::new(), + render_span: Some(Suggestion(sugg.clone())), }); } } @@ -1054,38 +1069,38 @@ impl EmitterWriter { -> io::Result<()> { use std::borrow::Borrow; - let primary_span = suggestion.msp.primary_span().unwrap(); + let primary_span = suggestion.substitutes[0].0; if let Some(ref cm) = self.cm { let mut buffer = StyledBuffer::new(); - buffer.append(0, &level.to_string(), Style::Level(level.clone())); - buffer.append(0, ": ", Style::HeaderMsg); - self.msg_to_buffer(&mut buffer, - &[(suggestion.msg.to_owned(), Style::NoStyle)], - max_line_num_len, - "suggestion", - Some(Style::HeaderMsg)); - let lines = cm.span_to_lines(primary_span).unwrap(); assert!(!lines.lines.is_empty()); - let complete = suggestion.splice_lines(cm.borrow()); - - // print the suggestion without any line numbers, but leave - // space for them. This helps with lining up with previous - // snippets from the actual error being reported. - let mut lines = complete.lines(); - let mut row_num = 1; - for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) { - draw_col_separator(&mut buffer, row_num, max_line_num_len + 1); - buffer.append(row_num, line, Style::NoStyle); - row_num += 1; - } + for complete in suggestion.splice_lines(cm.borrow()) { + buffer.append(0, &level.to_string(), Style::Level(level.clone())); + buffer.append(0, ": ", Style::HeaderMsg); + self.msg_to_buffer(&mut buffer, + &[(suggestion.msg.to_owned(), Style::NoStyle)], + max_line_num_len, + "suggestion", + Some(Style::HeaderMsg)); + + // print the suggestion without any line numbers, but leave + // space for them. This helps with lining up with previous + // snippets from the actual error being reported. + let mut lines = complete.lines(); + let mut row_num = 1; + for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) { + draw_col_separator(&mut buffer, row_num, max_line_num_len + 1); + buffer.append(row_num, line, Style::NoStyle); + row_num += 1; + } - // if we elided some lines, add an ellipsis - if let Some(_) = lines.next() { - buffer.append(row_num, "...", Style::NoStyle); + // if we elided some lines, add an ellipsis + if let Some(_) = lines.next() { + buffer.append(row_num, "...", Style::NoStyle); + } } emit_to_destination(&buffer.render(), level, &mut self.dst)?; } diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index db8c9ac306b..8e378935094 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -65,8 +65,25 @@ pub enum RenderSpan { #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] pub struct CodeSuggestion { - pub msp: MultiSpan, - pub substitutes: Vec, + /// Each substitute can have multiple variants due to multiple + /// applicable suggestions + /// + /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing + /// `foo` and `bar` on their own: + /// + /// ``` + /// vec![ + /// (0..3, vec!["a", "x"]), + /// (4..7, vec!["b", "y"]), + /// ] + /// ``` + /// + /// or by replacing the entire span: + /// + /// ``` + /// vec![(0..7, vec!["a.b", "x.y"])] + /// ``` + pub substitutes: Vec<(Span, Vec)>, pub msg: String, } @@ -79,8 +96,8 @@ pub trait CodeMapper { } impl CodeSuggestion { - /// Returns the assembled code suggestion. - pub fn splice_lines(&self, cm: &CodeMapper) -> String { + /// Returns the assembled code suggestions. + pub fn splice_lines(&self, cm: &CodeMapper) -> Vec { use syntax_pos::{CharPos, Loc, Pos}; fn push_trailing(buf: &mut String, @@ -102,20 +119,22 @@ impl CodeSuggestion { } } - let mut primary_spans = self.msp.primary_spans().to_owned(); - - assert_eq!(primary_spans.len(), self.substitutes.len()); - if primary_spans.is_empty() { - return format!(""); + if self.substitutes.is_empty() { + return vec![String::new()]; } + let mut primary_spans: Vec<_> = self.substitutes + .iter() + .map(|&(sp, ref sub)| (sp, sub)) + .collect(); + // Assumption: all spans are in the same file, and all spans // are disjoint. Sort in ascending order. - primary_spans.sort_by_key(|sp| sp.lo); + primary_spans.sort_by_key(|sp| sp.0.lo); // Find the bounding span. - let lo = primary_spans.iter().map(|sp| sp.lo).min().unwrap(); - let hi = primary_spans.iter().map(|sp| sp.hi).min().unwrap(); + let lo = primary_spans.iter().map(|sp| sp.0.lo).min().unwrap(); + let hi = primary_spans.iter().map(|sp| sp.0.hi).min().unwrap(); let bounding_span = Span { lo: lo, hi: hi, @@ -138,33 +157,37 @@ impl CodeSuggestion { prev_hi.col = CharPos::from_usize(0); let mut prev_line = fm.get_line(lines.lines[0].line_index); - let mut buf = String::new(); + let mut bufs = vec![String::new(); self.substitutes[0].1.len()]; - for (sp, substitute) in primary_spans.iter().zip(self.substitutes.iter()) { + for (sp, substitutes) in primary_spans { let cur_lo = cm.lookup_char_pos(sp.lo); - if prev_hi.line == cur_lo.line { - push_trailing(&mut buf, prev_line, &prev_hi, Some(&cur_lo)); - } else { - push_trailing(&mut buf, prev_line, &prev_hi, None); - // push lines between the previous and current span (if any) - for idx in prev_hi.line..(cur_lo.line - 1) { - if let Some(line) = fm.get_line(idx) { - buf.push_str(line); - buf.push('\n'); + for (buf, substitute) in bufs.iter_mut().zip(substitutes) { + if prev_hi.line == cur_lo.line { + push_trailing(buf, prev_line, &prev_hi, Some(&cur_lo)); + } else { + push_trailing(buf, prev_line, &prev_hi, None); + // push lines between the previous and current span (if any) + for idx in prev_hi.line..(cur_lo.line - 1) { + if let Some(line) = fm.get_line(idx) { + buf.push_str(line); + buf.push('\n'); + } + } + if let Some(cur_line) = fm.get_line(cur_lo.line - 1) { + buf.push_str(&cur_line[..cur_lo.col.to_usize()]); } } - if let Some(cur_line) = fm.get_line(cur_lo.line - 1) { - buf.push_str(&cur_line[..cur_lo.col.to_usize()]); - } + buf.push_str(substitute); } - buf.push_str(substitute); prev_hi = cm.lookup_char_pos(sp.hi); prev_line = fm.get_line(prev_hi.line - 1); } - push_trailing(&mut buf, prev_line, &prev_hi, None); - // remove trailing newline - buf.pop(); - buf + for buf in &mut bufs { + push_trailing(buf, prev_line, &prev_hi, None); + // remove trailing newline + buf.pop(); + } + bufs } } -- cgit 1.4.1-3-g733a5 From e2f781c7ead3a9fe69020189decc6c3eebf6f25c Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 10 May 2017 13:19:29 +0200 Subject: Example usage of multiple suggestions --- src/librustc_errors/emitter.rs | 28 +++++++++---- src/librustc_errors/lib.rs | 5 ++- src/librustc_resolve/build_reduced_graph.rs | 37 +++++++++++------ src/librustc_resolve/lib.rs | 48 ++++++++++------------ src/librustc_resolve/macros.rs | 6 +-- src/test/ui/resolve/enums-are-namespaced-xc.stderr | 12 +++--- src/test/ui/resolve/issue-16058.stderr | 8 ++-- src/test/ui/resolve/issue-17518.stderr | 4 +- src/test/ui/resolve/issue-21221-1.stderr | 24 +++++------ src/test/ui/resolve/issue-21221-2.stderr | 4 +- src/test/ui/resolve/issue-21221-3.stderr | 4 +- src/test/ui/resolve/issue-21221-4.stderr | 4 +- src/test/ui/resolve/issue-3907.stderr | 4 +- src/test/ui/resolve/privacy-struct-ctor.stderr | 12 +++--- src/test/ui/span/issue-35987.stderr | 4 +- 15 files changed, 113 insertions(+), 91 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 564c472305c..cd72941146c 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -81,6 +81,10 @@ impl Emitter for EmitterWriter { /// maximum number of lines we will print for each error; arbitrary. pub const MAX_HIGHLIGHT_LINES: usize = 6; +/// maximum number of suggestions to be shown +/// +/// Arbitrary, but taken from trait import suggestion limit +pub const MAX_SUGGESTIONS: usize = 4; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ColorConfig { @@ -1077,20 +1081,22 @@ impl EmitterWriter { assert!(!lines.lines.is_empty()); - for complete in suggestion.splice_lines(cm.borrow()) { - buffer.append(0, &level.to_string(), Style::Level(level.clone())); - buffer.append(0, ": ", Style::HeaderMsg); - self.msg_to_buffer(&mut buffer, - &[(suggestion.msg.to_owned(), Style::NoStyle)], - max_line_num_len, - "suggestion", - Some(Style::HeaderMsg)); + buffer.append(0, &level.to_string(), Style::Level(level.clone())); + buffer.append(0, ": ", Style::HeaderMsg); + self.msg_to_buffer(&mut buffer, + &[(suggestion.msg.to_owned(), Style::NoStyle)], + max_line_num_len, + "suggestion", + Some(Style::HeaderMsg)); + + let suggestions = suggestion.splice_lines(cm.borrow()); + let mut row_num = 1; + for complete in suggestions.iter().take(MAX_SUGGESTIONS) { // print the suggestion without any line numbers, but leave // space for them. This helps with lining up with previous // snippets from the actual error being reported. let mut lines = complete.lines(); - let mut row_num = 1; for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) { draw_col_separator(&mut buffer, row_num, max_line_num_len + 1); buffer.append(row_num, line, Style::NoStyle); @@ -1102,6 +1108,10 @@ impl EmitterWriter { buffer.append(row_num, "...", Style::NoStyle); } } + if suggestions.len() > MAX_SUGGESTIONS { + let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS); + buffer.append(row_num, &msg, Style::NoStyle); + } emit_to_destination(&buffer.render(), level, &mut self.dst)?; } Ok(()) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 8e378935094..82d688d6ba6 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -183,7 +183,10 @@ impl CodeSuggestion { prev_line = fm.get_line(prev_hi.line - 1); } for buf in &mut bufs { - push_trailing(buf, prev_line, &prev_hi, None); + // if the replacement already ends with a newline, don't print the next line + if !buf.ends_with('\n') { + push_trailing(buf, prev_line, &prev_hi, None); + } // remove trailing newline buf.pop(); } diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index c797c151de6..d1f0cdedde8 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -150,7 +150,7 @@ impl<'a> Resolver<'a> { view_path.span, ResolutionError::SelfImportsOnlyAllowedWithin); } else if source_name == "$crate" && full_path.segments.len() == 1 { - let crate_root = self.resolve_crate_var(source.ctxt); + let crate_root = self.resolve_crate_var(source.ctxt, item.span); let crate_name = match crate_root.kind { ModuleKind::Def(_, name) => name, ModuleKind::Block(..) => unreachable!(), @@ -247,7 +247,7 @@ impl<'a> Resolver<'a> { // n.b. we don't need to look at the path option here, because cstore already did let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap(); - let module = self.get_extern_crate_root(crate_id); + let module = self.get_extern_crate_root(crate_id, item.span); self.populate_module_if_necessary(module); let used = self.process_legacy_macro_imports(item, module, expansion); let binding = @@ -279,7 +279,7 @@ impl<'a> Resolver<'a> { no_implicit_prelude: parent.no_implicit_prelude || { attr::contains_name(&item.attrs, "no_implicit_prelude") }, - ..ModuleData::new(Some(parent), module_kind, def_id) + ..ModuleData::new(Some(parent), module_kind, def_id, item.span) }); self.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.module_map.insert(def_id, module); @@ -314,7 +314,10 @@ impl<'a> Resolver<'a> { ItemKind::Enum(ref enum_definition, _) => { let def = Def::Enum(self.definitions.local_def_id(item.id)); let module_kind = ModuleKind::Def(def, ident.name); - let module = self.new_module(parent, module_kind, parent.normal_ancestor_id); + let module = self.new_module(parent, + module_kind, + parent.normal_ancestor_id, + item.span); self.define(parent, ident, TypeNS, (module, vis, sp, expansion)); for variant in &(*enum_definition).variants { @@ -370,7 +373,10 @@ impl<'a> Resolver<'a> { // Add all the items within to a new module. let module_kind = ModuleKind::Def(Def::Trait(def_id), ident.name); - let module = self.new_module(parent, module_kind, parent.normal_ancestor_id); + let module = self.new_module(parent, + module_kind, + parent.normal_ancestor_id, + item.span); self.define(parent, ident, TypeNS, (module, vis, sp, expansion)); self.current_module = module; } @@ -419,7 +425,7 @@ impl<'a> Resolver<'a> { let parent = self.current_module; if self.block_needs_anonymous_module(block) { let module = - self.new_module(parent, ModuleKind::Block(block.id), parent.normal_ancestor_id); + self.new_module(parent, ModuleKind::Block(block.id), parent.normal_ancestor_id, block.span); self.block_map.insert(block.id, module); self.current_module = module; // Descend into the block. } @@ -431,10 +437,14 @@ impl<'a> Resolver<'a> { let def = child.def; let def_id = def.def_id(); let vis = self.session.cstore.visibility(def_id); + let span = child.span; match def { Def::Mod(..) | Def::Enum(..) => { - let module = self.new_module(parent, ModuleKind::Def(def, ident.name), def_id); + let module = self.new_module(parent, + ModuleKind::Def(def, ident.name), + def_id, + span); self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, Mark::root())); } Def::Variant(..) | Def::TyAlias(..) => { @@ -454,7 +464,10 @@ impl<'a> Resolver<'a> { } Def::Trait(..) => { let module_kind = ModuleKind::Def(def, ident.name); - let module = self.new_module(parent, module_kind, parent.normal_ancestor_id); + let module = self.new_module(parent, + module_kind, + parent.normal_ancestor_id, + span); self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, Mark::root())); for child in self.session.cstore.item_children(def_id) { @@ -483,18 +496,18 @@ impl<'a> Resolver<'a> { } } - fn get_extern_crate_root(&mut self, cnum: CrateNum) -> Module<'a> { + fn get_extern_crate_root(&mut self, cnum: CrateNum, span: Span) -> Module<'a> { let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; let name = self.session.cstore.crate_name(cnum); let macros_only = self.session.cstore.dep_kind(cnum).macros_only(); let module_kind = ModuleKind::Def(Def::Mod(def_id), name); let arenas = self.arenas; *self.extern_crate_roots.entry((cnum, macros_only)).or_insert_with(|| { - arenas.alloc_module(ModuleData::new(None, module_kind, def_id)) + arenas.alloc_module(ModuleData::new(None, module_kind, def_id, span)) }) } - pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> { + pub fn macro_def_scope(&mut self, expansion: Mark, span: Span) -> Module<'a> { let def_id = self.macro_defs[&expansion]; if let Some(id) = self.definitions.as_local_node_id(def_id) { self.local_macro_def_scopes[&id] @@ -503,7 +516,7 @@ impl<'a> Resolver<'a> { self.graph_root } else { let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap(); - self.get_extern_crate_root(module_def_id.krate) + self.get_extern_crate_root(module_def_id.krate, span) } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index ac556270886..fd964c7d7d1 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -865,12 +865,15 @@ pub struct ModuleData<'a> { // access the children must be preceded with a // `populate_module_if_necessary` call. populated: Cell, + + /// Span of the module itself. Used for error reporting. + span: Span, } pub type Module<'a> = &'a ModuleData<'a>; impl<'a> ModuleData<'a> { - fn new(parent: Option>, kind: ModuleKind, normal_ancestor_id: DefId) -> Self { + fn new(parent: Option>, kind: ModuleKind, normal_ancestor_id: DefId, span: Span) -> Self { ModuleData { parent: parent, kind: kind, @@ -884,6 +887,7 @@ impl<'a> ModuleData<'a> { globs: RefCell::new((Vec::new())), traits: RefCell::new(None), populated: Cell::new(normal_ancestor_id.is_local()), + span: span, } } @@ -1298,7 +1302,7 @@ impl<'a> Resolver<'a> { let root_module_kind = ModuleKind::Def(Def::Mod(root_def_id), keywords::Invalid.name()); let graph_root = arenas.alloc_module(ModuleData { no_implicit_prelude: attr::contains_name(&krate.attrs, "no_implicit_prelude"), - ..ModuleData::new(None, root_module_kind, root_def_id) + ..ModuleData::new(None, root_module_kind, root_def_id, krate.span) }); let mut module_map = FxHashMap(); module_map.insert(DefId::local(CRATE_DEF_INDEX), graph_root); @@ -1430,9 +1434,9 @@ impl<'a> Resolver<'a> { self.crate_loader.postprocess(krate); } - fn new_module(&self, parent: Module<'a>, kind: ModuleKind, normal_ancestor_id: DefId) + fn new_module(&self, parent: Module<'a>, kind: ModuleKind, normal_ancestor_id: DefId, span: Span) -> Module<'a> { - self.arenas.alloc_module(ModuleData::new(Some(parent), kind, normal_ancestor_id)) + self.arenas.alloc_module(ModuleData::new(Some(parent), kind, normal_ancestor_id, span)) } fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>, span: Span) @@ -1535,12 +1539,12 @@ impl<'a> Resolver<'a> { None } - fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext) -> Module<'a> { + fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext, span: Span) -> Module<'a> { let mut ctxt_data = crate_var_ctxt.data(); while ctxt_data.prev_ctxt != SyntaxContext::empty() { ctxt_data = ctxt_data.prev_ctxt.data(); } - let module = self.macro_def_scope(ctxt_data.outer_mark); + let module = self.macro_def_scope(ctxt_data.outer_mark, span); if module.is_local() { self.graph_root } else { module } } @@ -2271,8 +2275,10 @@ impl<'a> Resolver<'a> { let name = path.last().unwrap().name; let candidates = this.lookup_import_candidates(name, ns, is_expected); if !candidates.is_empty() { + let mut module_span = this.current_module.span; + module_span.hi = module_span.lo; // Report import candidates as help and proceed searching for labels. - show_candidates(&mut err, &candidates, def.is_some()); + show_candidates(&mut err, module_span, &candidates, def.is_some()); } else if is_expected(Def::Enum(DefId::local(CRATE_DEF_INDEX))) { let enum_candidates = this.lookup_import_candidates(name, ns, is_enum_variant); let mut enum_candidates = enum_candidates.iter() @@ -2584,7 +2590,7 @@ impl<'a> Resolver<'a> { module = Some(self.graph_root); continue } else if i == 0 && ns == TypeNS && ident.name == "$crate" { - module = Some(self.resolve_crate_var(ident.ctxt)); + module = Some(self.resolve_crate_var(ident.ctxt, DUMMY_SP)); continue } @@ -3463,12 +3469,10 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St /// When an entity with a given name is not available in scope, we search for /// entities with that name in all crates. This method allows outputting the /// results of this search in a programmer-friendly way -fn show_candidates(session: &mut DiagnosticBuilder, +fn show_candidates(err: &mut DiagnosticBuilder, + span: Span, candidates: &[ImportSuggestion], better: bool) { - // don't show more than MAX_CANDIDATES results, so - // we're consistent with the trait suggestions - const MAX_CANDIDATES: usize = 4; // we want consistent results across executions, but candidates are produced // by iterating through a hash map, so make sure they are ordered: @@ -3481,21 +3485,13 @@ fn show_candidates(session: &mut DiagnosticBuilder, 1 => " is found in another module, you can import it", _ => "s are found in other modules, you can import them", }; + let msg = format!("possible {}candidate{} into scope", better, msg_diff); + + for candidate in &mut path_strings { + *candidate = format!("use {};\n", candidate); + } - let end = cmp::min(MAX_CANDIDATES, path_strings.len()); - session.help(&format!("possible {}candidate{} into scope:{}{}", - better, - msg_diff, - &path_strings[0..end].iter().map(|candidate| { - format!("\n `use {};`", candidate) - }).collect::(), - if path_strings.len() > MAX_CANDIDATES { - format!("\nand {} other candidates", - path_strings.len() - MAX_CANDIDATES) - } else { - "".to_owned() - } - )); + err.span_suggestions(span, &msg, path_strings); } /// A somewhat inefficient routine to obtain the name of a module. diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 106f421f39e..fffccada7d6 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -123,14 +123,14 @@ impl<'a> base::Resolver for Resolver<'a> { } fn eliminate_crate_var(&mut self, item: P) -> P { - struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>); + struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>, Span); impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> { fn fold_path(&mut self, mut path: ast::Path) -> ast::Path { let ident = path.segments[0].identifier; if ident.name == "$crate" { path.segments[0].identifier.name = keywords::CrateRoot.name(); - let module = self.0.resolve_crate_var(ident.ctxt); + let module = self.0.resolve_crate_var(ident.ctxt, self.1); if !module.is_local() { let span = path.segments[0].span; path.segments.insert(1, match module.kind { @@ -149,7 +149,7 @@ impl<'a> base::Resolver for Resolver<'a> { } } - EliminateCrateVar(self).fold_item(item).expect_one("") + EliminateCrateVar(self, item.span).fold_item(item).expect_one("") } fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool { diff --git a/src/test/ui/resolve/enums-are-namespaced-xc.stderr b/src/test/ui/resolve/enums-are-namespaced-xc.stderr index dd04c5ce356..17c5d5d15d4 100644 --- a/src/test/ui/resolve/enums-are-namespaced-xc.stderr +++ b/src/test/ui/resolve/enums-are-namespaced-xc.stderr @@ -4,8 +4,8 @@ error[E0425]: cannot find value `A` in module `namespaced_enums` 15 | let _ = namespaced_enums::A; | ^ not found in `namespaced_enums` | - = help: possible candidate is found in another module, you can import it into scope: - `use namespaced_enums::Foo::A;` +help: possible candidate is found in another module, you can import it into scope + | use namespaced_enums::Foo::A; error[E0425]: cannot find function `B` in module `namespaced_enums` --> $DIR/enums-are-namespaced-xc.rs:18:31 @@ -13,8 +13,8 @@ error[E0425]: cannot find function `B` in module `namespaced_enums` 18 | let _ = namespaced_enums::B(10); | ^ not found in `namespaced_enums` | - = help: possible candidate is found in another module, you can import it into scope: - `use namespaced_enums::Foo::B;` +help: possible candidate is found in another module, you can import it into scope + | use namespaced_enums::Foo::B; error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums` --> $DIR/enums-are-namespaced-xc.rs:21:31 @@ -22,8 +22,8 @@ error[E0422]: cannot find struct, variant or union type `C` in module `namespace 21 | let _ = namespaced_enums::C { a: 10 }; | ^ not found in `namespaced_enums` | - = help: possible candidate is found in another module, you can import it into scope: - `use namespaced_enums::Foo::C;` +help: possible candidate is found in another module, you can import it into scope + | use namespaced_enums::Foo::C; error: aborting due to 3 previous errors diff --git a/src/test/ui/resolve/issue-16058.stderr b/src/test/ui/resolve/issue-16058.stderr index 69c48cc1f32..63d2ce10914 100644 --- a/src/test/ui/resolve/issue-16058.stderr +++ b/src/test/ui/resolve/issue-16058.stderr @@ -4,10 +4,10 @@ error[E0574]: expected struct, variant or union type, found enum `Result` 19 | Result { | ^^^^^^ not a struct, variant or union type | - = help: possible better candidates are found in other modules, you can import them into scope: - `use std::fmt::Result;` - `use std::io::Result;` - `use std::thread::Result;` +help: possible better candidates are found in other modules, you can import them into scope + | use std::fmt::Result; + | use std::io::Result; + | use std::thread::Result; error: aborting due to previous error diff --git a/src/test/ui/resolve/issue-17518.stderr b/src/test/ui/resolve/issue-17518.stderr index ea6841e6009..c0438abfe43 100644 --- a/src/test/ui/resolve/issue-17518.stderr +++ b/src/test/ui/resolve/issue-17518.stderr @@ -4,8 +4,8 @@ error[E0422]: cannot find struct, variant or union type `E` in this scope 16 | E { name: "foobar" }; //~ ERROR unresolved struct, variant or union type `E` | ^ not found in this scope | - = help: possible candidate is found in another module, you can import it into scope: - `use SomeEnum::E;` +help: possible candidate is found in another module, you can import it into scope + | use SomeEnum::E; error: aborting due to previous error diff --git a/src/test/ui/resolve/issue-21221-1.stderr b/src/test/ui/resolve/issue-21221-1.stderr index f38491d5362..7315d295f7b 100644 --- a/src/test/ui/resolve/issue-21221-1.stderr +++ b/src/test/ui/resolve/issue-21221-1.stderr @@ -4,10 +4,10 @@ error[E0405]: cannot find trait `Mul` in this scope 53 | impl Mul for Foo { | ^^^ not found in this scope | - = help: possible candidates are found in other modules, you can import them into scope: - `use mul1::Mul;` - `use mul2::Mul;` - `use std::ops::Mul;` +help: possible candidates are found in other modules, you can import them into scope + | use mul1::Mul; + | use mul2::Mul; + | use std::ops::Mul; error[E0412]: cannot find type `Mul` in this scope --> $DIR/issue-21221-1.rs:72:16 @@ -15,12 +15,12 @@ error[E0412]: cannot find type `Mul` in this scope 72 | fn getMul() -> Mul { | ^^^ not found in this scope | - = help: possible candidates are found in other modules, you can import them into scope: - `use mul1::Mul;` - `use mul2::Mul;` - `use mul3::Mul;` - `use mul4::Mul;` - and 2 other candidates +help: possible candidates are found in other modules, you can import them into scope + | use mul1::Mul; + | use mul2::Mul; + | use mul3::Mul; + | use mul4::Mul; +and 2 other candidates error[E0405]: cannot find trait `ThisTraitReallyDoesntExistInAnyModuleReally` in this scope --> $DIR/issue-21221-1.rs:83:6 @@ -34,8 +34,8 @@ error[E0405]: cannot find trait `Div` in this scope 88 | impl Div for Foo { | ^^^ not found in this scope | - = help: possible candidate is found in another module, you can import it into scope: - `use std::ops::Div;` +help: possible candidate is found in another module, you can import it into scope + | use std::ops::Div; error: cannot continue compilation due to previous error diff --git a/src/test/ui/resolve/issue-21221-2.stderr b/src/test/ui/resolve/issue-21221-2.stderr index 14dac7de4b2..f0b22754e64 100644 --- a/src/test/ui/resolve/issue-21221-2.stderr +++ b/src/test/ui/resolve/issue-21221-2.stderr @@ -4,8 +4,8 @@ error[E0405]: cannot find trait `T` in this scope 28 | impl T for Foo { } | ^ not found in this scope | - = help: possible candidate is found in another module, you can import it into scope: - `use foo::bar::T;` +help: possible candidate is found in another module, you can import it into scope + | use foo::bar::T; error: main function not found diff --git a/src/test/ui/resolve/issue-21221-3.stderr b/src/test/ui/resolve/issue-21221-3.stderr index e1e00571e5d..a4a2496b19a 100644 --- a/src/test/ui/resolve/issue-21221-3.stderr +++ b/src/test/ui/resolve/issue-21221-3.stderr @@ -4,8 +4,8 @@ error[E0405]: cannot find trait `OuterTrait` in this scope 25 | impl OuterTrait for Foo {} | ^^^^^^^^^^ not found in this scope | - = help: possible candidate is found in another module, you can import it into scope: - `use issue_21221_3::outer::OuterTrait;` +help: possible candidate is found in another module, you can import it into scope + | use issue_21221_3::outer::OuterTrait; error: cannot continue compilation due to previous error diff --git a/src/test/ui/resolve/issue-21221-4.stderr b/src/test/ui/resolve/issue-21221-4.stderr index 569315a59cf..dc2f2271731 100644 --- a/src/test/ui/resolve/issue-21221-4.stderr +++ b/src/test/ui/resolve/issue-21221-4.stderr @@ -4,8 +4,8 @@ error[E0405]: cannot find trait `T` in this scope 20 | impl T for Foo {} | ^ not found in this scope | - = help: possible candidate is found in another module, you can import it into scope: - `use issue_21221_4::T;` +help: possible candidate is found in another module, you can import it into scope + | use issue_21221_4::T; error: cannot continue compilation due to previous error diff --git a/src/test/ui/resolve/issue-3907.stderr b/src/test/ui/resolve/issue-3907.stderr index a7dd494d75b..0bf39dc55ce 100644 --- a/src/test/ui/resolve/issue-3907.stderr +++ b/src/test/ui/resolve/issue-3907.stderr @@ -4,8 +4,8 @@ error[E0404]: expected trait, found type alias `Foo` 20 | impl Foo for S { //~ ERROR expected trait, found type alias `Foo` | ^^^ type aliases cannot be used for traits | - = help: possible better candidate is found in another module, you can import it into scope: - `use issue_3907::Foo;` +help: possible better candidate is found in another module, you can import it into scope + | use issue_3907::Foo; error: cannot continue compilation due to previous error diff --git a/src/test/ui/resolve/privacy-struct-ctor.stderr b/src/test/ui/resolve/privacy-struct-ctor.stderr index 940e4acabb2..19940ff4586 100644 --- a/src/test/ui/resolve/privacy-struct-ctor.stderr +++ b/src/test/ui/resolve/privacy-struct-ctor.stderr @@ -8,8 +8,8 @@ error[E0423]: expected value, found struct `Z` | did you mean `S`? | constructor is not visible here due to private fields | - = help: possible better candidate is found in another module, you can import it into scope: - `use m::n::Z;` +help: possible better candidate is found in another module, you can import it into scope + | use m::n::Z; error[E0423]: expected value, found struct `S` --> $DIR/privacy-struct-ctor.rs:36:5 @@ -20,8 +20,8 @@ error[E0423]: expected value, found struct `S` | did you mean `S { /* fields */ }`? | constructor is not visible here due to private fields | - = help: possible better candidate is found in another module, you can import it into scope: - `use m::S;` +help: possible better candidate is found in another module, you can import it into scope + | use m::S; error[E0423]: expected value, found struct `xcrate::S` --> $DIR/privacy-struct-ctor.rs:42:5 @@ -32,8 +32,8 @@ error[E0423]: expected value, found struct `xcrate::S` | did you mean `xcrate::S { /* fields */ }`? | constructor is not visible here due to private fields | - = help: possible better candidate is found in another module, you can import it into scope: - `use m::S;` +help: possible better candidate is found in another module, you can import it into scope + | use m::S; error: tuple struct `Z` is private --> $DIR/privacy-struct-ctor.rs:25:9 diff --git a/src/test/ui/span/issue-35987.stderr b/src/test/ui/span/issue-35987.stderr index 9dab2f77898..e53ea6a55af 100644 --- a/src/test/ui/span/issue-35987.stderr +++ b/src/test/ui/span/issue-35987.stderr @@ -4,8 +4,8 @@ error[E0404]: expected trait, found type parameter `Add` 15 | impl Add for Foo { | ^^^ not a trait | - = help: possible better candidate is found in another module, you can import it into scope: - `use std::ops::Add;` +help: possible better candidate is found in another module, you can import it into scope + | use std::ops::Add; error: main function not found -- cgit 1.4.1-3-g733a5 From 644ce5e535f74be304a77dfadb9ff46c743554c7 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 11 May 2017 15:26:22 +0200 Subject: Address PR reviews --- src/librustc_errors/diagnostic.rs | 11 +++++++-- src/librustc_errors/emitter.rs | 50 +++++++++++++++++---------------------- src/librustc_errors/lib.rs | 28 ++++++++++++++++++---- src/libsyntax/json.rs | 10 ++++---- 4 files changed, 59 insertions(+), 40 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index e129e313626..861880aa265 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -9,6 +9,7 @@ // except according to those terms. use CodeSuggestion; +use Substitution; use Level; use RenderSpan; use std::fmt; @@ -205,7 +206,10 @@ impl Diagnostic { /// See `diagnostic::CodeSuggestion` for more information. pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self { self.suggestions.push(CodeSuggestion { - substitutes: vec![(sp, vec![suggestion])], + substitution_parts: vec![Substitution { + span: sp, + substitutions: vec![suggestion], + }], msg: msg.to_owned(), }); self @@ -213,7 +217,10 @@ impl Diagnostic { pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec) -> &mut Self { self.suggestions.push(CodeSuggestion { - substitutes: vec![(sp, suggestions)], + substitution_parts: vec![Substitution { + span: sp, + substitutions: suggestions, + }], msg: msg.to_owned(), }); self diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index cd72941146c..d1ec1be47b8 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -35,38 +35,32 @@ impl Emitter for EmitterWriter { let mut primary_span = db.span.clone(); let mut children = db.children.clone(); - if db.suggestions.len() == 1 { - let sugg = &db.suggestions[0]; - // don't display multispans as labels - if sugg.substitutes.len() == 1 && + if let Some((sugg, rest)) = db.suggestions.split_first() { + if rest.is_empty() && + // don't display multipart suggestions as labels + sugg.substitution_parts.len() == 1 && // don't display multi-suggestions as labels - sugg.substitutes[0].1.len() == 1 && + sugg.substitutions() == 1 && // don't display long messages as labels sugg.msg.split_whitespace().count() < 10 && // don't display multiline suggestions as labels - sugg.substitutes[0].1[0].find('\n').is_none() { - let msg = format!("help: {} `{}`", sugg.msg, sugg.substitutes[0].1[0]); - primary_span.push_span_label(sugg.substitutes[0].0, msg); + sugg.substitution_parts[0].substitutions[0].find('\n').is_none() { + let substitution = &sugg.substitution_parts[0].substitutions[0]; + let msg = format!("help: {} `{}`", sugg.msg, substitution); + primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg); } else { - children.push(SubDiagnostic { - level: Level::Help, - message: Vec::new(), - span: MultiSpan::new(), - render_span: Some(Suggestion(sugg.clone())), - }); - } - } else { - // if there are multiple suggestions, print them all in full - // to be consistent. We could try to figure out if we can - // make one (or the first one) inline, but that would give - // undue importance to a semi-random suggestion - for sugg in &db.suggestions { - children.push(SubDiagnostic { - level: Level::Help, - message: Vec::new(), - span: MultiSpan::new(), - render_span: Some(Suggestion(sugg.clone())), - }); + // if there are multiple suggestions, print them all in full + // to be consistent. We could try to figure out if we can + // make one (or the first one) inline, but that would give + // undue importance to a semi-random suggestion + for sugg in &db.suggestions { + children.push(SubDiagnostic { + level: Level::Help, + message: Vec::new(), + span: MultiSpan::new(), + render_span: Some(Suggestion(sugg.clone())), + }); + } } } @@ -1073,7 +1067,7 @@ impl EmitterWriter { -> io::Result<()> { use std::borrow::Borrow; - let primary_span = suggestion.substitutes[0].0; + let primary_span = suggestion.substitution_spans().next().unwrap(); if let Some(ref cm) = self.cm { let mut buffer = StyledBuffer::new(); diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 82d688d6ba6..e1ec23479ab 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -23,6 +23,7 @@ #![feature(staged_api)] #![feature(range_contains)] #![feature(libc)] +#![feature(conservative_impl_trait)] extern crate term; extern crate libc; @@ -83,10 +84,17 @@ pub struct CodeSuggestion { /// ``` /// vec![(0..7, vec!["a.b", "x.y"])] /// ``` - pub substitutes: Vec<(Span, Vec)>, + pub substitution_parts: Vec, pub msg: String, } +#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] +/// See the docs on `CodeSuggestion::substitutions` +pub struct Substitution { + pub span: Span, + pub substitutions: Vec, +} + pub trait CodeMapper { fn lookup_char_pos(&self, pos: BytePos) -> Loc; fn span_to_lines(&self, sp: Span) -> FileLinesResult; @@ -96,6 +104,16 @@ pub trait CodeMapper { } impl CodeSuggestion { + /// Returns the number of substitutions + fn substitutions(&self) -> usize { + self.substitution_parts[0].substitutions.len() + } + + /// Returns the number of substitutions + pub fn substitution_spans<'a>(&'a self) -> impl Iterator + 'a { + self.substitution_parts.iter().map(|sub| sub.span) + } + /// Returns the assembled code suggestions. pub fn splice_lines(&self, cm: &CodeMapper) -> Vec { use syntax_pos::{CharPos, Loc, Pos}; @@ -119,13 +137,13 @@ impl CodeSuggestion { } } - if self.substitutes.is_empty() { + if self.substitution_parts.is_empty() { return vec![String::new()]; } - let mut primary_spans: Vec<_> = self.substitutes + let mut primary_spans: Vec<_> = self.substitution_parts .iter() - .map(|&(sp, ref sub)| (sp, sub)) + .map(|sub| (sub.span, &sub.substitutions)) .collect(); // Assumption: all spans are in the same file, and all spans @@ -157,7 +175,7 @@ impl CodeSuggestion { prev_hi.col = CharPos::from_usize(0); let mut prev_line = fm.get_line(lines.lines[0].line_index); - let mut bufs = vec![String::new(); self.substitutes[0].1.len()]; + let mut bufs = vec![String::new(); self.substitutions()]; for (sp, substitutes) in primary_spans { let cur_lo = cm.lookup_char_pos(sp.lo); diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index 3d0b0b228a8..06335584c96 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -279,12 +279,12 @@ impl DiagnosticSpan { fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter) -> Vec { - suggestion.substitutes + suggestion.substitution_parts .iter() - .flat_map(|&(span, ref suggestion)| { - suggestion.iter().map(move |suggestion| { + .flat_map(|substitution| { + substitution.substitutions.iter().map(move |suggestion| { let span_label = SpanLabel { - span, + span: substitution.span, is_primary: true, label: None, }; @@ -301,7 +301,7 @@ impl DiagnosticSpan { RenderSpan::FullSpan(ref msp) => DiagnosticSpan::from_multispan(msp, je), // regular diagnostics don't produce this anymore - // will be removed in a later commit + // FIXME(oli_obk): remove it entirely RenderSpan::Suggestion(_) => unreachable!(), } } -- cgit 1.4.1-3-g733a5 From 80891f6e4725efc72c27e4f224123ec292fdd7d4 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 12 May 2017 08:21:00 +0200 Subject: Remove some unused macros from the rust codebase Removes unused macros from: * libcore * libcollections The last use of these two macros was removed in commit b64c9d56700e2c41207166fe8709711ff02488ff when the char_range_at_reverse function was been removed. * librustc_errors Their last use was removed by commits 2f2c3e178325dc1837badcd7573c2c0905fab979 and 11dc974a38fd533aa692cea213305056cd3a6902. * libsyntax_ext * librustc_trans Also, put the otry macro in back/msvc/mod.rs under the same cfg argument as the places that use it. --- src/libcollections/str.rs | 12 ------------ src/libcore/lib.rs | 4 ---- src/libcore/num/float_macros.rs | 20 -------------------- src/libcore/num/mod.rs | 7 ------- src/librustc_errors/emitter.rs | 15 --------------- src/librustc_trans/back/msvc/mod.rs | 1 + src/librustc_trans/lib.rs | 3 --- src/librustc_trans/macros.rs | 29 ----------------------------- src/libsyntax_ext/deriving/mod.rs | 6 ------ 9 files changed, 1 insertion(+), 96 deletions(-) delete mode 100644 src/libcore/num/float_macros.rs delete mode 100644 src/librustc_trans/macros.rs (limited to 'src/librustc_errors') diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 964660183e7..5f4578bbeb3 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -176,18 +176,6 @@ impl<'a> Iterator for EncodeUtf16<'a> { #[unstable(feature = "fused", issue = "35602")] impl<'a> FusedIterator for EncodeUtf16<'a> {} -// Return the initial codepoint accumulator for the first byte. -// The first byte is special, only want bottom 5 bits for width 2, 4 bits -// for width 3, and 3 bits for width 4 -macro_rules! utf8_first_byte { - ($byte:expr, $width:expr) => (($byte & (0x7F >> $width)) as u32) -} - -// return the value of $ch updated with continuation byte $byte -macro_rules! utf8_acc_cont_byte { - ($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63) as u32) -} - #[stable(feature = "rust1", since = "1.0.0")] impl Borrow for String { #[inline] diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 80c2221ce64..b6ab1ecaf4e 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -104,10 +104,6 @@ mod macros; #[macro_use] mod internal_macros; -#[path = "num/float_macros.rs"] -#[macro_use] -mod float_macros; - #[path = "num/int_macros.rs"] #[macro_use] mod int_macros; diff --git a/src/libcore/num/float_macros.rs b/src/libcore/num/float_macros.rs deleted file mode 100644 index b3adef53dab..00000000000 --- a/src/libcore/num/float_macros.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![doc(hidden)] - -macro_rules! assert_approx_eq { - ($a:expr, $b:expr) => ({ - use num::Float; - let (a, b) = (&$a, &$b); - assert!((*a - *b).abs() < 1.0e-6, - "{} is not approximately equal to {}", *a, *b); - }) -} diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 18e2c1d5c73..8b4002fe9af 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -96,13 +96,6 @@ pub mod dec2flt; pub mod bignum; pub mod diy_float; -macro_rules! checked_op { - ($U:ty, $op:path, $x:expr, $y:expr) => {{ - let (result, overflowed) = unsafe { $op($x as $U, $y as $U) }; - if overflowed { None } else { Some(result as Self) } - }} -} - // `Int` + `SignedInt` implemented for signed integers macro_rules! int_impl { ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 53999eb9138..34c138eca9e 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -95,21 +95,6 @@ struct FileWithAnnotatedLines { multiline_depth: usize, } - -/// Do not use this for messages that end in `\n` – use `println_maybe_styled` instead. See -/// `EmitterWriter::print_maybe_styled` for details. -macro_rules! print_maybe_styled { - ($dst: expr, $style: expr, $($arg: tt)*) => { - $dst.print_maybe_styled(format_args!($($arg)*), $style, false) - } -} - -macro_rules! println_maybe_styled { - ($dst: expr, $style: expr, $($arg: tt)*) => { - $dst.print_maybe_styled(format_args!($($arg)*), $style, true) - } -} - impl EmitterWriter { pub fn stderr(color_config: ColorConfig, code_map: Option>) -> EmitterWriter { if color_config.use_color() { diff --git a/src/librustc_trans/back/msvc/mod.rs b/src/librustc_trans/back/msvc/mod.rs index 16aef6ee8ca..31f3415b1ec 100644 --- a/src/librustc_trans/back/msvc/mod.rs +++ b/src/librustc_trans/back/msvc/mod.rs @@ -32,6 +32,7 @@ //! comments can also be found below leading through the various code paths. // A simple macro to make this option mess easier to read +#[cfg(windows)] macro_rules! otry { ($expr:expr) => (match $expr { Some(val) => val, diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index 111c2547721..14b6650c493 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -85,9 +85,6 @@ pub mod back { pub mod diagnostics; -#[macro_use] -mod macros; - mod abi; mod adt; mod asm; diff --git a/src/librustc_trans/macros.rs b/src/librustc_trans/macros.rs deleted file mode 100644 index 77efcc6fb00..00000000000 --- a/src/librustc_trans/macros.rs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -macro_rules! unpack_datum { - ($bcx: ident, $inp: expr) => ( - { - let db = $inp; - $bcx = db.bcx; - db.datum - } - ) -} - -macro_rules! unpack_result { - ($bcx: ident, $inp: expr) => ( - { - let db = $inp; - $bcx = db.bcx; - db.val - } - ) -} diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index b2bb43e41ed..31c7cc33676 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -25,12 +25,6 @@ macro_rules! pathvec { ) } -macro_rules! path { - ($($x:tt)*) => ( - ::ext::deriving::generic::ty::Path::new( pathvec![ $($x)* ] ) - ) -} - macro_rules! path_local { ($x:ident) => ( ::deriving::generic::ty::Path::new_local(stringify!($x)) -- cgit 1.4.1-3-g733a5 From 7a03b4c75ad985812467742faa8d76a14a4bf601 Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Fri, 12 May 2017 12:48:18 +0100 Subject: Fix unexpected panic with the -Z treat-err-as-bug option This fix an issue where the compiler panics even if there is no error when passed with the `-Z treat-err-as-bug` option. Fixes #35886. --- src/librustc_errors/diagnostic_builder.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index a9c2bbeba2a..84fb4b19275 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -99,7 +99,10 @@ impl<'a> DiagnosticBuilder<'a> { self.handler.emitter.borrow_mut().emit(&self); self.cancel(); - self.handler.panic_if_treat_err_as_bug(); + + if self.level == Level::Error { + self.handler.panic_if_treat_err_as_bug(); + } // if self.is_fatal() { // panic!(FatalError); -- cgit 1.4.1-3-g733a5