Skip to content

Commit

Permalink
handle errors based on parse_sess
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Oct 20, 2018
1 parent 2ed2d1a commit 30c6698
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/librustc/middle/stability.rs
Expand Up @@ -134,11 +134,11 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
if self.tcx.features().staged_api {
// This crate explicitly wants staged API.
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, \
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) {
// Error if prohibited, or can't inherit anything from a container
if kind == AnnotationKind::Prohibited ||
Expand Down Expand Up @@ -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 {
self.tcx.sess.span_err(item_sp, "This deprecation annotation is useless");
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -1927,7 +1927,7 @@ impl ReprOptions {
let mut max_align = 0;
let mut min_pack = 0;
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 {
attr::ReprC => ReprFlags::IS_C,
attr::ReprPacked(pack) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/nonstandard_style.rs
Expand Up @@ -121,7 +121,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes {
let has_repr_c = it.attrs
.iter()
.any(|attr| {
attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr)
attr::find_repr_attrs(&cx.tcx.sess.parse_sess, attr)
.iter()
.any(|r| r == &attr::ReprC)
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -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;
if repr.packed() {
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 pack != repr.pack {
struct_span_err!(tcx.sess, sp, E0634,
Expand Down
67 changes: 36 additions & 31 deletions src/libsyntax/attr/builtin.rs
Expand Up @@ -27,7 +27,8 @@ enum AttrError {
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 {
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
"multiple '{}' items", item),
Expand All @@ -52,11 +53,11 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool)
"unsupported literal",
);
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(
span,
"consider removing the prefix",
format!("{}", lint_str[1..]),
format!("{}", &lint_str[1..]),
Applicability::MaybeIncorrect,
);
}
Expand Down Expand Up @@ -179,12 +180,12 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
}

/// 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> {
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,
item_sp: Span)
-> Option<Stability>
Expand All @@ -196,6 +197,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let mut rustc_depr: Option<RustcDeprecation> = None;
let mut rustc_const_unstable: Option<Symbol> = None;
let mut promotable = false;
let diagnostic = &sess.span_diagnostic;

'outer: for attr in attrs_iter {
if ![
Expand All @@ -220,7 +222,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let meta = meta.as_ref().unwrap();
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
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
}
if let Some(v) = meta.value_str() {
Expand All @@ -247,16 +249,16 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
_ => {
let expected = &[ $( stringify!($name) ),+ ];
handle_errors(
diagnostic,
sess,
mi.span,
AttrError::UnknownMetaItem(mi.name(), expected),
false
false,
);
continue 'outer
}
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer
}
}
Expand All @@ -281,7 +283,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
continue
}
_ => {
Expand All @@ -307,7 +309,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
"unstable" => {
if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
break
}

Expand All @@ -322,7 +324,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
"issue" => if !get(mi, &mut issue) { continue 'outer },
_ => {
handle_errors(
diagnostic,
sess,
meta.span,
AttrError::UnknownMetaItem(
mi.name(),
Expand All @@ -334,7 +336,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer
}
}
Expand All @@ -361,7 +363,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
continue
}
_ => {
Expand All @@ -372,7 +374,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
"stable" => {
if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
break
}

Expand All @@ -386,17 +388,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
"since" => if !get(mi, &mut since) { continue 'outer },
_ => {
handle_errors(
diagnostic,
sess,
meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false,
);
continue 'outer
}
}
},
NestedMetaItemKind::Literal(lit) => {
handle_errors(
diagnostic,
sess,
meta.span,
AttrError::UnsupportedLiteral,
lit.node.is_bytestr()
Expand All @@ -419,11 +422,11 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
continue
}
_ => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
continue
}
}
Expand Down Expand Up @@ -490,9 +493,9 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
MetaItemKind::List(..) => {
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(
&sess.span_diagnostic,
sess,
lit.span, AttrError::UnsupportedLiteral,
lit.node.is_bytestr(),
);
Expand All @@ -515,7 +518,7 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() {
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;
}
}
Expand Down Expand Up @@ -557,18 +560,19 @@ pub struct Deprecation {
}

/// 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> {
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,
item_sp: Span)
-> Option<Deprecation>
where I: Iterator<Item = &'a Attribute>
{
let mut depr: Option<Deprecation> = None;
let diagnostic = &sess.span_diagnostic;

'outer: for attr in attrs_iter {
if attr.path != "deprecated" {
Expand All @@ -585,7 +589,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
depr = if let Some(metas) = attr.meta_item_list() {
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
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
}
if let Some(v) = meta.value_str() {
Expand All @@ -607,7 +611,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
"note" => if !get(mi, &mut note) { continue 'outer },
_ => {
handle_errors(
diagnostic,
sess,
meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false,
Expand All @@ -618,7 +622,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
}
NestedMetaItemKind::Literal(lit) => {
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
}
}
Expand Down Expand Up @@ -668,16 +672,17 @@ impl IntType {
/// the same discriminant size that the corresponding C enum would or C
/// structure layout, `packed` to remove padding, and `transparent` to elegate representation
/// 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::*;

let mut acc = Vec::new();
let diagnostic = &sess.span_diagnostic;
if attr.path == "repr" {
if let Some(items) = attr.meta_item_list() {
mark_used(attr);
for item in items {
if !item.is_meta_item() {
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral, false);
handle_errors(sess, item.span, AttrError::UnsupportedLiteral, false);
continue
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Expand Up @@ -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| {
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
Some((stability.feature, issue))
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax_ext/deriving/generic/mod.rs
Expand Up @@ -202,8 +202,8 @@ use syntax::source_map::{self, respan};
use syntax::util::move_map::MoveMap;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::parse::ParseSess;
use syntax_pos::{DUMMY_SP, Span};
use errors::Handler;

use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};

Expand Down Expand Up @@ -412,7 +412,7 @@ impl<'a> TraitDef<'a> {
match *item {
Annotatable::Item(ref item) => {
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 {
return true;
}
Expand Down Expand Up @@ -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";
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 {
attr::ReprPacked(_) | attr::ReprSimd | attr::ReprAlign(_) | attr::ReprTransparent =>
continue,
Expand Down Expand Up @@ -1390,7 +1390,7 @@ impl<'a> MethodDef<'a> {
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
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;
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
Expand Down

0 comments on commit 30c6698

Please sign in to comment.