handle errors based on parse_sess

This commit is contained in:
csmoe
2018-10-09 19:49:18 +08:00
parent 2ed2d1a7e6
commit 30c6698193
7 changed files with 48 additions and 43 deletions

View File

@@ -134,11 +134,11 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
if self.tcx.features().staged_api { if self.tcx.features().staged_api {
// This crate explicitly wants staged API. // This crate explicitly wants staged API.
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs); debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
if let Some(..) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) { if let Some(..) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
self.tcx.sess.span_err(item_sp, "`#[deprecated]` cannot be used in staged api, \ self.tcx.sess.span_err(item_sp, "`#[deprecated]` cannot be used in staged api, \
use `#[rustc_deprecated]` instead"); use `#[rustc_deprecated]` instead");
} }
if let Some(mut stab) = attr::find_stability(self.tcx.sess.diagnostic(), if let Some(mut stab) = attr::find_stability(&self.tcx.sess.parse_sess,
attrs, item_sp) { attrs, item_sp) {
// Error if prohibited, or can't inherit anything from a container // Error if prohibited, or can't inherit anything from a container
if kind == AnnotationKind::Prohibited || if kind == AnnotationKind::Prohibited ||
@@ -224,7 +224,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
} }
} }
if let Some(depr) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) { if let Some(depr) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
if kind == AnnotationKind::Prohibited { if kind == AnnotationKind::Prohibited {
self.tcx.sess.span_err(item_sp, "This deprecation annotation is useless"); self.tcx.sess.span_err(item_sp, "This deprecation annotation is useless");
} }

View File

@@ -1927,7 +1927,7 @@ impl ReprOptions {
let mut max_align = 0; let mut max_align = 0;
let mut min_pack = 0; let mut min_pack = 0;
for attr in tcx.get_attrs(did).iter() { for attr in tcx.get_attrs(did).iter() {
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) { for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
flags.insert(match r { flags.insert(match r {
attr::ReprC => ReprFlags::IS_C, attr::ReprC => ReprFlags::IS_C,
attr::ReprPacked(pack) => { attr::ReprPacked(pack) => {

View File

@@ -121,7 +121,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes {
let has_repr_c = it.attrs let has_repr_c = it.attrs
.iter() .iter()
.any(|attr| { .any(|attr| {
attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr) attr::find_repr_attrs(&cx.tcx.sess.parse_sess, attr)
.iter() .iter()
.any(|r| r == &attr::ReprC) .any(|r| r == &attr::ReprC)
}); });

View File

@@ -1713,7 +1713,7 @@ fn check_packed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: DefId)
let repr = tcx.adt_def(def_id).repr; let repr = tcx.adt_def(def_id).repr;
if repr.packed() { if repr.packed() {
for attr in tcx.get_attrs(def_id).iter() { for attr in tcx.get_attrs(def_id).iter() {
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) { for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
if let attr::ReprPacked(pack) = r { if let attr::ReprPacked(pack) = r {
if pack != repr.pack { if pack != repr.pack {
struct_span_err!(tcx.sess, sp, E0634, struct_span_err!(tcx.sess, sp, E0634,

View File

@@ -27,7 +27,8 @@ enum AttrError {
UnsupportedLiteral UnsupportedLiteral
} }
fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool) { fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: bool) {
let diag = &sess.span_diagnostic;
match error { match error {
AttrError::MultipleItem(item) => span_err!(diag, span, E0538, AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
"multiple '{}' items", item), "multiple '{}' items", item),
@@ -52,11 +53,11 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool)
"unsupported literal", "unsupported literal",
); );
if is_bytestr { if is_bytestr {
if let Ok(lint_str) = sess.source_map.span_to_snippet(span) { if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
err.span_suggestion_with_applicability( err.span_suggestion_with_applicability(
span, span,
"consider removing the prefix", "consider removing the prefix",
format!("{}", lint_str[1..]), format!("{}", &lint_str[1..]),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }
@@ -179,12 +180,12 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
} }
/// Find the first stability attribute. `None` if none exists. /// Find the first stability attribute. `None` if none exists.
pub fn find_stability(diagnostic: &Handler, attrs: &[Attribute], pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
item_sp: Span) -> Option<Stability> { item_sp: Span) -> Option<Stability> {
find_stability_generic(diagnostic, attrs.iter(), item_sp) find_stability_generic(sess, attrs.iter(), item_sp)
} }
fn find_stability_generic<'a, I>(diagnostic: &Handler, fn find_stability_generic<'a, I>(sess: &ParseSess,
attrs_iter: I, attrs_iter: I,
item_sp: Span) item_sp: Span)
-> Option<Stability> -> Option<Stability>
@@ -196,6 +197,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let mut rustc_depr: Option<RustcDeprecation> = None; let mut rustc_depr: Option<RustcDeprecation> = None;
let mut rustc_const_unstable: Option<Symbol> = None; let mut rustc_const_unstable: Option<Symbol> = None;
let mut promotable = false; let mut promotable = false;
let diagnostic = &sess.span_diagnostic;
'outer: for attr in attrs_iter { 'outer: for attr in attrs_iter {
if ![ if ![
@@ -220,7 +222,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let meta = meta.as_ref().unwrap(); let meta = meta.as_ref().unwrap();
let get = |meta: &MetaItem, item: &mut Option<Symbol>| { let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() { if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false); handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
return false return false
} }
if let Some(v) = meta.value_str() { if let Some(v) = meta.value_str() {
@@ -247,16 +249,16 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
_ => { _ => {
let expected = &[ $( stringify!($name) ),+ ]; let expected = &[ $( stringify!($name) ),+ ];
handle_errors( handle_errors(
diagnostic, sess,
mi.span, mi.span,
AttrError::UnknownMetaItem(mi.name(), expected), AttrError::UnknownMetaItem(mi.name(), expected),
false false,
); );
continue 'outer continue 'outer
} }
} }
} else { } else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false); handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer continue 'outer
} }
} }
@@ -281,7 +283,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _) => { (None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false); handle_errors(sess, attr.span(), AttrError::MissingSince, false);
continue continue
} }
_ => { _ => {
@@ -307,7 +309,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
} }
"unstable" => { "unstable" => {
if stab.is_some() { if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false); handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
break break
} }
@@ -322,7 +324,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
"issue" => if !get(mi, &mut issue) { continue 'outer }, "issue" => if !get(mi, &mut issue) { continue 'outer },
_ => { _ => {
handle_errors( handle_errors(
diagnostic, sess,
meta.span, meta.span,
AttrError::UnknownMetaItem( AttrError::UnknownMetaItem(
mi.name(), mi.name(),
@@ -334,7 +336,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
} }
} }
} else { } else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false); handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer continue 'outer
} }
} }
@@ -361,7 +363,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _, _) => { (None, _, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false); handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
continue continue
} }
_ => { _ => {
@@ -372,7 +374,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
} }
"stable" => { "stable" => {
if stab.is_some() { if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false); handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
break break
} }
@@ -386,7 +388,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
"since" => if !get(mi, &mut since) { continue 'outer }, "since" => if !get(mi, &mut since) { continue 'outer },
_ => { _ => {
handle_errors( handle_errors(
diagnostic, sess,
meta.span, meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false, false,
@@ -394,9 +396,10 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
continue 'outer continue 'outer
} }
} }
},
NestedMetaItemKind::Literal(lit) => { NestedMetaItemKind::Literal(lit) => {
handle_errors( handle_errors(
diagnostic, sess,
meta.span, meta.span,
AttrError::UnsupportedLiteral, AttrError::UnsupportedLiteral,
lit.node.is_bytestr() lit.node.is_bytestr()
@@ -419,11 +422,11 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _) => { (None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false); handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
continue continue
} }
_ => { _ => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false); handle_errors(sess, attr.span(), AttrError::MissingSince, false);
continue continue
} }
} }
@@ -490,9 +493,9 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
MetaItemKind::List(..) => { MetaItemKind::List(..) => {
error(cfg.span, "unexpected parentheses after `cfg` predicate key") error(cfg.span, "unexpected parentheses after `cfg` predicate key")
} }
MetaItemKind::NameValue(lit) => if !lit.node.is_str() { MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
handle_errors( handle_errors(
&sess.span_diagnostic, sess,
lit.span, AttrError::UnsupportedLiteral, lit.span, AttrError::UnsupportedLiteral,
lit.node.is_bytestr(), lit.node.is_bytestr(),
); );
@@ -515,7 +518,7 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
ast::MetaItemKind::List(ref mis) => { ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() { for mi in mis.iter() {
if !mi.is_meta_item() { if !mi.is_meta_item() {
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral, false); handle_errors(sess, mi.span, AttrError::UnsupportedLiteral, false);
return false; return false;
} }
} }
@@ -557,18 +560,19 @@ pub struct Deprecation {
} }
/// Find the deprecation attribute. `None` if none exists. /// Find the deprecation attribute. `None` if none exists.
pub fn find_deprecation(diagnostic: &Handler, attrs: &[Attribute], pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
item_sp: Span) -> Option<Deprecation> { item_sp: Span) -> Option<Deprecation> {
find_deprecation_generic(diagnostic, attrs.iter(), item_sp) find_deprecation_generic(sess, attrs.iter(), item_sp)
} }
fn find_deprecation_generic<'a, I>(diagnostic: &Handler, fn find_deprecation_generic<'a, I>(sess: &ParseSess,
attrs_iter: I, attrs_iter: I,
item_sp: Span) item_sp: Span)
-> Option<Deprecation> -> Option<Deprecation>
where I: Iterator<Item = &'a Attribute> where I: Iterator<Item = &'a Attribute>
{ {
let mut depr: Option<Deprecation> = None; let mut depr: Option<Deprecation> = None;
let diagnostic = &sess.span_diagnostic;
'outer: for attr in attrs_iter { 'outer: for attr in attrs_iter {
if attr.path != "deprecated" { if attr.path != "deprecated" {
@@ -585,7 +589,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
depr = if let Some(metas) = attr.meta_item_list() { depr = if let Some(metas) = attr.meta_item_list() {
let get = |meta: &MetaItem, item: &mut Option<Symbol>| { let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() { if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false); handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
return false return false
} }
if let Some(v) = meta.value_str() { if let Some(v) = meta.value_str() {
@@ -607,7 +611,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
"note" => if !get(mi, &mut note) { continue 'outer }, "note" => if !get(mi, &mut note) { continue 'outer },
_ => { _ => {
handle_errors( handle_errors(
diagnostic, sess,
meta.span, meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false, false,
@@ -618,7 +622,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
} }
NestedMetaItemKind::Literal(lit) => { NestedMetaItemKind::Literal(lit) => {
let is_bytestr = lit.node.is_bytestr(); let is_bytestr = lit.node.is_bytestr();
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, is_bytestr); handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, is_bytestr);
continue 'outer continue 'outer
} }
} }
@@ -668,16 +672,17 @@ impl IntType {
/// the same discriminant size that the corresponding C enum would or C /// the same discriminant size that the corresponding C enum would or C
/// structure layout, `packed` to remove padding, and `transparent` to elegate representation /// structure layout, `packed` to remove padding, and `transparent` to elegate representation
/// concerns to the only non-ZST field. /// concerns to the only non-ZST field.
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> { pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
use self::ReprAttr::*; use self::ReprAttr::*;
let mut acc = Vec::new(); let mut acc = Vec::new();
let diagnostic = &sess.span_diagnostic;
if attr.path == "repr" { if attr.path == "repr" {
if let Some(items) = attr.meta_item_list() { if let Some(items) = attr.meta_item_list() {
mark_used(attr); mark_used(attr);
for item in items { for item in items {
if !item.is_meta_item() { if !item.is_meta_item() {
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral, false); handle_errors(sess, item.span, AttrError::UnsupportedLiteral, false);
continue continue
} }

View File

@@ -340,7 +340,7 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
} }
} }
let unstable_feature = attr::find_stability(&sess.span_diagnostic, let unstable_feature = attr::find_stability(&sess,
&def.attrs, def.span).and_then(|stability| { &def.attrs, def.span).and_then(|stability| {
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level { if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
Some((stability.feature, issue)) Some((stability.feature, issue))

View File

@@ -202,8 +202,8 @@ use syntax::source_map::{self, respan};
use syntax::util::move_map::MoveMap; use syntax::util::move_map::MoveMap;
use syntax::ptr::P; use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords}; use syntax::symbol::{Symbol, keywords};
use syntax::parse::ParseSess;
use syntax_pos::{DUMMY_SP, Span}; use syntax_pos::{DUMMY_SP, Span};
use errors::Handler;
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty}; use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
@@ -412,7 +412,7 @@ impl<'a> TraitDef<'a> {
match *item { match *item {
Annotatable::Item(ref item) => { Annotatable::Item(ref item) => {
let is_packed = item.attrs.iter().any(|attr| { let is_packed = item.attrs.iter().any(|attr| {
for r in attr::find_repr_attrs(&cx.parse_sess.span_diagnostic, attr) { for r in attr::find_repr_attrs(&cx.parse_sess, attr) {
if let attr::ReprPacked(_) = r { if let attr::ReprPacked(_) = r {
return true; return true;
} }
@@ -811,10 +811,10 @@ impl<'a> TraitDef<'a> {
} }
} }
fn find_repr_type_name(diagnostic: &Handler, type_attrs: &[ast::Attribute]) -> &'static str { fn find_repr_type_name(sess: &ParseSess, type_attrs: &[ast::Attribute]) -> &'static str {
let mut repr_type_name = "isize"; let mut repr_type_name = "isize";
for a in type_attrs { for a in type_attrs {
for r in &attr::find_repr_attrs(diagnostic, a) { for r in &attr::find_repr_attrs(sess, a) {
repr_type_name = match *r { repr_type_name = match *r {
attr::ReprPacked(_) | attr::ReprSimd | attr::ReprAlign(_) | attr::ReprTransparent => attr::ReprPacked(_) | attr::ReprSimd | attr::ReprAlign(_) | attr::ReprTransparent =>
continue, continue,
@@ -1390,7 +1390,7 @@ impl<'a> MethodDef<'a> {
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ... // discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
let mut discriminant_test = cx.expr_bool(sp, true); let mut discriminant_test = cx.expr_bool(sp, true);
let target_type_name = find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs); let target_type_name = find_repr_type_name(&cx.parse_sess, type_attrs);
let mut first_ident = None; let mut first_ident = None;
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) { for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {