From 50f2884dcdc0c9ba8f24b0c2014f60cb85b0e717 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Fri, 16 Mar 2018 12:43:22 -0500 Subject: Replace many of the last references to readmes --- src/libsyntax/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/README.md b/src/libsyntax/README.md index 3bf735ee868..7214203830e 100644 --- a/src/libsyntax/README.md +++ b/src/libsyntax/README.md @@ -1,7 +1,9 @@ -NB: This crate is part of the Rust compiler. For an overview of the -compiler as a whole, see -[the README.md file found in `librustc`](../librustc/README.md). - The `syntax` crate contains those things concerned purely with syntax – that is, the AST ("abstract syntax tree"), parser, pretty-printer, lexer, macro expander, and utilities for traversing ASTs. + +For more information about how these things work in rustc, see the +rustc guide: + +- [Parsing](https://rust-lang-nursery.github.io/rustc-guide/the-parser.html) +- [Macro Expansion](https://rust-lang-nursery.github.io/rustc-guide/macro-expansion.html) -- cgit 1.4.1-3-g733a5 From 9f5a356c1d01e971b1fe33f7afb2b81d0eec2b1c Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sat, 17 Mar 2018 13:46:45 +0800 Subject: improve attribute trailing semicolon error --- src/libsyntax/parse/attr.rs | 11 +---------- src/test/ui/issue-49040.rs | 12 ++++++++++++ src/test/ui/issue-49040.stderr | 8 ++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/issue-49040.rs create mode 100644 src/test/ui/issue-49040.stderr (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 053746b579d..4c3f42d9c6b 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -90,7 +90,7 @@ impl<'a> Parser<'a> { debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}", inner_parse_policy, self.token); - let (span, path, tokens, mut style) = match self.token { + let (span, path, tokens, style) = match self.token { token::Pound => { let lo = self.span; self.bump(); @@ -129,15 +129,6 @@ impl<'a> Parser<'a> { } }; - if inner_parse_policy == InnerAttributeParsePolicy::Permitted && - self.token == token::Semi { - self.bump(); - self.span_warn(span, - "this inner attribute syntax is deprecated. The new syntax is \ - `#![foo]`, with a bang and no semicolon"); - style = ast::AttrStyle::Inner; - } - Ok(ast::Attribute { id: attr::mk_attr_id(), style, diff --git a/src/test/ui/issue-49040.rs b/src/test/ui/issue-49040.rs new file mode 100644 index 00000000000..866ecd9e1d9 --- /dev/null +++ b/src/test/ui/issue-49040.rs @@ -0,0 +1,12 @@ +// Copyright 2015 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. + +#![allow(unused_variables)]; //~ ERROR expected item, found `;` +fn main() {} diff --git a/src/test/ui/issue-49040.stderr b/src/test/ui/issue-49040.stderr new file mode 100644 index 00000000000..b6f624dac7d --- /dev/null +++ b/src/test/ui/issue-49040.stderr @@ -0,0 +1,8 @@ +error: expected item, found `;` + --> $DIR/issue-49040.rs:11:28 + | +LL | #![allow(unused_variables)]; //~ ERROR expected item, found `;` + | ^ help: consider removing this semicolon + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5 From 74a4928ed49d000dee9827c21f68148ad3aa271e Mon Sep 17 00:00:00 2001 From: Yukio Siraichi Date: Sat, 17 Mar 2018 15:41:46 -0300 Subject: Review fixes. - `span_suggestion` changed to `span_suggestion_short`; - `Span` used changed to contain only `&` refs; - Tests passing. --- src/librustc/traits/error_reporting.rs | 11 ++++++----- src/libsyntax/codemap.rs | 16 ++++++++++++++++ src/test/ui/suggest-remove-refs-1.stderr | 4 ++-- src/test/ui/suggest-remove-refs-2.stderr | 4 ++-- src/test/ui/suggest-remove-refs-3.stderr | 21 +++++++++------------ 5 files changed, 35 insertions(+), 21 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 267d84cc531..9b01f8899b5 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -914,13 +914,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { new_trait_ref.to_predicate()); if selcx.evaluate_obligation(&new_obligation) { - let remove_refs = refs_remaining + 1; + let sp = self.tcx.sess.codemap() + .span_take_while(span, |c| c.is_whitespace() || *c == '&'); - err.span_suggestion_short(span, - &format!("consider removing {} leading `&`-references", - remove_refs), - String::from("")); + let remove_refs = refs_remaining + 1; + let format_str = format!("consider removing {} leading `&`-references", + remove_refs); + err.span_suggestion_short(sp, &format_str, String::from("")); break; } } else { diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index a1aec052088..324d57c1194 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -697,6 +697,22 @@ impl CodeMap { sp } + /// Given a `Span`, get a shorter one until `predicate` yields false. + pub fn span_take_while

(&self, sp: Span, predicate: P) -> Span + where P: for <'r> FnMut(&'r char) -> bool + { + if let Ok(snippet) = self.span_to_snippet(sp) { + let offset = snippet.chars() + .take_while(predicate) + .map(|c| c.len_utf8()) + .sum::(); + + sp.with_hi(BytePos(sp.lo().0 + (offset as u32))) + } else { + sp + } + } + pub fn def_span(&self, sp: Span) -> Span { self.span_until_char(sp, '{') } diff --git a/src/test/ui/suggest-remove-refs-1.stderr b/src/test/ui/suggest-remove-refs-1.stderr index fe4ab2c4ee0..c47b4d283d7 100644 --- a/src/test/ui/suggest-remove-refs-1.stderr +++ b/src/test/ui/suggest-remove-refs-1.stderr @@ -2,10 +2,10 @@ error[E0277]: the trait bound `&std::iter::Enumerate $DIR/suggest-remove-refs-1.rs:14:19 | LL | for (i, n) in &v.iter().enumerate() { - | ^^^^^^^^^^^^^^^^^^^^^ + | -^^^^^^^^^^^^^^^^^^^^ | | | `&std::iter::Enumerate>` is not an iterator; maybe try calling `.iter()` or a similar method - | help: consider removing 1 references `&`: `v.iter().enumerate()` + | help: consider removing 1 leading `&`-references | = help: the trait `std::iter::Iterator` is not implemented for `&std::iter::Enumerate>` = note: required by `std::iter::IntoIterator::into_iter` diff --git a/src/test/ui/suggest-remove-refs-2.stderr b/src/test/ui/suggest-remove-refs-2.stderr index 243ddcfe125..fdd654ea392 100644 --- a/src/test/ui/suggest-remove-refs-2.stderr +++ b/src/test/ui/suggest-remove-refs-2.stderr @@ -2,10 +2,10 @@ error[E0277]: the trait bound `&&&&&std::iter::Enumerate $DIR/suggest-remove-refs-2.rs:14:19 | LL | for (i, n) in & & & & &v.iter().enumerate() { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ---------^^^^^^^^^^^^^^^^^^^^ | | | `&&&&&std::iter::Enumerate>` is not an iterator; maybe try calling `.iter()` or a similar method - | help: consider removing 5 references `&`: `v.iter().enumerate()` + | help: consider removing 5 leading `&`-references | = help: the trait `std::iter::Iterator` is not implemented for `&&&&&std::iter::Enumerate>` = note: required by `std::iter::IntoIterator::into_iter` diff --git a/src/test/ui/suggest-remove-refs-3.stderr b/src/test/ui/suggest-remove-refs-3.stderr index 83f3826642b..b0920a0fa52 100644 --- a/src/test/ui/suggest-remove-refs-3.stderr +++ b/src/test/ui/suggest-remove-refs-3.stderr @@ -1,21 +1,18 @@ error[E0277]: the trait bound `&&&&&std::iter::Enumerate>: std::iter::Iterator` is not satisfied --> $DIR/suggest-remove-refs-3.rs:14:19 | -LL | for (i, n) in & & & - | ___________________^ -LL | | & &v -LL | | .iter() -LL | | .enumerate() { - | |____________________^ `&&&&&std::iter::Enumerate>` is not an iterator; maybe try calling `.iter()` or a similar method +LL | for (i, n) in & & & + | ___________________^ + | |___________________| + | || +LL | || & &v + | ||___________- help: consider removing 5 leading `&`-references +LL | | .iter() +LL | | .enumerate() { + | |_____________________^ `&&&&&std::iter::Enumerate>` is not an iterator; maybe try calling `.iter()` or a similar method | = help: the trait `std::iter::Iterator` is not implemented for `&&&&&std::iter::Enumerate>` = note: required by `std::iter::IntoIterator::into_iter` -help: consider removing 5 references `&` - | -LL | for (i, n) in v -LL | .iter() -LL | .enumerate() { - | error: aborting due to previous error -- cgit 1.4.1-3-g733a5 From 0b36b20651704cf7051f468aeb3b84babf940e63 Mon Sep 17 00:00:00 2001 From: Yukio Siraichi Date: Sun, 18 Mar 2018 10:05:20 -0300 Subject: CodeMap functions refactored. - Using `span_take_while` to implement others. --- src/libsyntax/codemap.rs | 88 +++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 50 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 324d57c1194..73924c4270e 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -597,21 +597,6 @@ impl CodeMap { self.span_to_source(sp, |src, start_index, _| src[..start_index].to_string()) } - /// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char` - pub fn span_until_char(&self, sp: Span, c: char) -> Span { - match self.span_to_snippet(sp) { - Ok(snippet) => { - let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right(); - if !snippet.is_empty() && !snippet.contains('\n') { - sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32)) - } else { - sp - } - } - _ => sp, - } - } - /// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span /// if no character could be found or if an error occurred while retrieving the code snippet. pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span { @@ -646,26 +631,50 @@ impl CodeMap { sp } + /// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char` + pub fn span_until_char(&self, sp: Span, c: char) -> Span { + match self.span_to_snippet(sp) { + Ok(snippet) => { + let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right(); + if !snippet.is_empty() && !snippet.contains('\n') { + sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32)) + } else { + sp + } + } + _ => sp, + } + } + + /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char` + /// `c`. + pub fn span_through_char(&self, sp: Span, c: char) -> Span { + if let Ok(snippet) = self.span_to_snippet(sp) { + if let Some(offset) = snippet.find(c) { + return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32)); + } + } + sp + } + /// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or /// the original `Span`. /// /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned. pub fn span_until_non_whitespace(&self, sp: Span) -> Span { - if let Ok(snippet) = self.span_to_snippet(sp) { - let mut offset = 0; - // get the bytes width of all the non-whitespace characters - for c in snippet.chars().take_while(|c| !c.is_whitespace()) { - offset += c.len_utf8(); - } - // get the bytes width of all the whitespace characters after that - for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) { - offset += c.len_utf8(); + let mut whitespace_found = false; + + self.span_take_while(sp, |c| { + if !whitespace_found && c.is_whitespace() { + whitespace_found = true; } - if offset > 1 { - return sp.with_hi(BytePos(sp.lo().0 + offset as u32)); + + if whitespace_found && !c.is_whitespace() { + false + } else { + true } - } - sp + }) } /// Given a `Span`, get a new `Span` covering the first token without its trailing whitespace or @@ -673,28 +682,7 @@ impl CodeMap { /// /// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned. pub fn span_until_whitespace(&self, sp: Span) -> Span { - if let Ok(snippet) = self.span_to_snippet(sp) { - let mut offset = 0; - // Get the bytes width of all the non-whitespace characters - for c in snippet.chars().take_while(|c| !c.is_whitespace()) { - offset += c.len_utf8(); - } - if offset > 1 { - return sp.with_hi(BytePos(sp.lo().0 + offset as u32)); - } - } - sp - } - - /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char` - /// `c`. - pub fn span_through_char(&self, sp: Span, c: char) -> Span { - if let Ok(snippet) = self.span_to_snippet(sp) { - if let Some(offset) = snippet.find(c) { - return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32)); - } - } - sp + self.span_take_while(sp, |c| !c.is_whitespace()) } /// Given a `Span`, get a shorter one until `predicate` yields false. -- cgit 1.4.1-3-g733a5