Skip to content

Commit 30c6698

Browse files
committed
handle errors based on parse_sess
1 parent 2ed2d1a commit 30c6698

File tree

7 files changed

+48
-43
lines changed

7 files changed

+48
-43
lines changed

src/librustc/middle/stability.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
134134
if self.tcx.features().staged_api {
135135
// This crate explicitly wants staged API.
136136
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
137-
if let Some(..) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) {
137+
if let Some(..) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
138138
self.tcx.sess.span_err(item_sp, "`#[deprecated]` cannot be used in staged api, \
139139
use `#[rustc_deprecated]` instead");
140140
}
141-
if let Some(mut stab) = attr::find_stability(self.tcx.sess.diagnostic(),
141+
if let Some(mut stab) = attr::find_stability(&self.tcx.sess.parse_sess,
142142
attrs, item_sp) {
143143
// Error if prohibited, or can't inherit anything from a container
144144
if kind == AnnotationKind::Prohibited ||
@@ -224,7 +224,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
224224
}
225225
}
226226

227-
if let Some(depr) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) {
227+
if let Some(depr) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
228228
if kind == AnnotationKind::Prohibited {
229229
self.tcx.sess.span_err(item_sp, "This deprecation annotation is useless");
230230
}

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ impl ReprOptions {
19271927
let mut max_align = 0;
19281928
let mut min_pack = 0;
19291929
for attr in tcx.get_attrs(did).iter() {
1930-
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) {
1930+
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
19311931
flags.insert(match r {
19321932
attr::ReprC => ReprFlags::IS_C,
19331933
attr::ReprPacked(pack) => {

src/librustc_lint/nonstandard_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes {
121121
let has_repr_c = it.attrs
122122
.iter()
123123
.any(|attr| {
124-
attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr)
124+
attr::find_repr_attrs(&cx.tcx.sess.parse_sess, attr)
125125
.iter()
126126
.any(|r| r == &attr::ReprC)
127127
});

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ fn check_packed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: DefId)
17131713
let repr = tcx.adt_def(def_id).repr;
17141714
if repr.packed() {
17151715
for attr in tcx.get_attrs(def_id).iter() {
1716-
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) {
1716+
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
17171717
if let attr::ReprPacked(pack) = r {
17181718
if pack != repr.pack {
17191719
struct_span_err!(tcx.sess, sp, E0634,

src/libsyntax/attr/builtin.rs

+36-31
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ enum AttrError {
2727
UnsupportedLiteral
2828
}
2929

30-
fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool) {
30+
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: bool) {
31+
let diag = &sess.span_diagnostic;
3132
match error {
3233
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
3334
"multiple '{}' items", item),
@@ -52,11 +53,11 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool)
5253
"unsupported literal",
5354
);
5455
if is_bytestr {
55-
if let Ok(lint_str) = sess.source_map.span_to_snippet(span) {
56+
if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
5657
err.span_suggestion_with_applicability(
5758
span,
5859
"consider removing the prefix",
59-
format!("{}", lint_str[1..]),
60+
format!("{}", &lint_str[1..]),
6061
Applicability::MaybeIncorrect,
6162
);
6263
}
@@ -179,12 +180,12 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
179180
}
180181

181182
/// Find the first stability attribute. `None` if none exists.
182-
pub fn find_stability(diagnostic: &Handler, attrs: &[Attribute],
183+
pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
183184
item_sp: Span) -> Option<Stability> {
184-
find_stability_generic(diagnostic, attrs.iter(), item_sp)
185+
find_stability_generic(sess, attrs.iter(), item_sp)
185186
}
186187

187-
fn find_stability_generic<'a, I>(diagnostic: &Handler,
188+
fn find_stability_generic<'a, I>(sess: &ParseSess,
188189
attrs_iter: I,
189190
item_sp: Span)
190191
-> Option<Stability>
@@ -196,6 +197,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
196197
let mut rustc_depr: Option<RustcDeprecation> = None;
197198
let mut rustc_const_unstable: Option<Symbol> = None;
198199
let mut promotable = false;
200+
let diagnostic = &sess.span_diagnostic;
199201

200202
'outer: for attr in attrs_iter {
201203
if ![
@@ -220,7 +222,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
220222
let meta = meta.as_ref().unwrap();
221223
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
222224
if item.is_some() {
223-
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false);
225+
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
224226
return false
225227
}
226228
if let Some(v) = meta.value_str() {
@@ -247,16 +249,16 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
247249
_ => {
248250
let expected = &[ $( stringify!($name) ),+ ];
249251
handle_errors(
250-
diagnostic,
252+
sess,
251253
mi.span,
252254
AttrError::UnknownMetaItem(mi.name(), expected),
253-
false
255+
false,
254256
);
255257
continue 'outer
256258
}
257259
}
258260
} else {
259-
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
261+
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
260262
continue 'outer
261263
}
262264
}
@@ -281,7 +283,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
281283
})
282284
}
283285
(None, _) => {
284-
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
286+
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
285287
continue
286288
}
287289
_ => {
@@ -307,7 +309,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
307309
}
308310
"unstable" => {
309311
if stab.is_some() {
310-
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
312+
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
311313
break
312314
}
313315

@@ -322,7 +324,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
322324
"issue" => if !get(mi, &mut issue) { continue 'outer },
323325
_ => {
324326
handle_errors(
325-
diagnostic,
327+
sess,
326328
meta.span,
327329
AttrError::UnknownMetaItem(
328330
mi.name(),
@@ -334,7 +336,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
334336
}
335337
}
336338
} else {
337-
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
339+
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
338340
continue 'outer
339341
}
340342
}
@@ -361,7 +363,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
361363
})
362364
}
363365
(None, _, _) => {
364-
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
366+
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
365367
continue
366368
}
367369
_ => {
@@ -372,7 +374,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
372374
}
373375
"stable" => {
374376
if stab.is_some() {
375-
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
377+
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
376378
break
377379
}
378380

@@ -386,17 +388,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
386388
"since" => if !get(mi, &mut since) { continue 'outer },
387389
_ => {
388390
handle_errors(
389-
diagnostic,
391+
sess,
390392
meta.span,
391393
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
392394
false,
393395
);
394396
continue 'outer
395397
}
396398
}
399+
},
397400
NestedMetaItemKind::Literal(lit) => {
398401
handle_errors(
399-
diagnostic,
402+
sess,
400403
meta.span,
401404
AttrError::UnsupportedLiteral,
402405
lit.node.is_bytestr()
@@ -419,11 +422,11 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
419422
})
420423
}
421424
(None, _) => {
422-
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
425+
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
423426
continue
424427
}
425428
_ => {
426-
handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
429+
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
427430
continue
428431
}
429432
}
@@ -490,9 +493,9 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
490493
MetaItemKind::List(..) => {
491494
error(cfg.span, "unexpected parentheses after `cfg` predicate key")
492495
}
493-
MetaItemKind::NameValue(lit) => if !lit.node.is_str() {
496+
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
494497
handle_errors(
495-
&sess.span_diagnostic,
498+
sess,
496499
lit.span, AttrError::UnsupportedLiteral,
497500
lit.node.is_bytestr(),
498501
);
@@ -515,7 +518,7 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
515518
ast::MetaItemKind::List(ref mis) => {
516519
for mi in mis.iter() {
517520
if !mi.is_meta_item() {
518-
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral, false);
521+
handle_errors(sess, mi.span, AttrError::UnsupportedLiteral, false);
519522
return false;
520523
}
521524
}
@@ -557,18 +560,19 @@ pub struct Deprecation {
557560
}
558561

559562
/// Find the deprecation attribute. `None` if none exists.
560-
pub fn find_deprecation(diagnostic: &Handler, attrs: &[Attribute],
563+
pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
561564
item_sp: Span) -> Option<Deprecation> {
562-
find_deprecation_generic(diagnostic, attrs.iter(), item_sp)
565+
find_deprecation_generic(sess, attrs.iter(), item_sp)
563566
}
564567

565-
fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
568+
fn find_deprecation_generic<'a, I>(sess: &ParseSess,
566569
attrs_iter: I,
567570
item_sp: Span)
568571
-> Option<Deprecation>
569572
where I: Iterator<Item = &'a Attribute>
570573
{
571574
let mut depr: Option<Deprecation> = None;
575+
let diagnostic = &sess.span_diagnostic;
572576

573577
'outer: for attr in attrs_iter {
574578
if attr.path != "deprecated" {
@@ -585,7 +589,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
585589
depr = if let Some(metas) = attr.meta_item_list() {
586590
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
587591
if item.is_some() {
588-
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false);
592+
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
589593
return false
590594
}
591595
if let Some(v) = meta.value_str() {
@@ -607,7 +611,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
607611
"note" => if !get(mi, &mut note) { continue 'outer },
608612
_ => {
609613
handle_errors(
610-
diagnostic,
614+
sess,
611615
meta.span,
612616
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
613617
false,
@@ -618,7 +622,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
618622
}
619623
NestedMetaItemKind::Literal(lit) => {
620624
let is_bytestr = lit.node.is_bytestr();
621-
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, is_bytestr);
625+
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, is_bytestr);
622626
continue 'outer
623627
}
624628
}
@@ -668,16 +672,17 @@ impl IntType {
668672
/// the same discriminant size that the corresponding C enum would or C
669673
/// structure layout, `packed` to remove padding, and `transparent` to elegate representation
670674
/// concerns to the only non-ZST field.
671-
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
675+
pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
672676
use self::ReprAttr::*;
673677

674678
let mut acc = Vec::new();
679+
let diagnostic = &sess.span_diagnostic;
675680
if attr.path == "repr" {
676681
if let Some(items) = attr.meta_item_list() {
677682
mark_used(attr);
678683
for item in items {
679684
if !item.is_meta_item() {
680-
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral, false);
685+
handle_errors(sess, item.span, AttrError::UnsupportedLiteral, false);
681686
continue
682687
}
683688

src/libsyntax/ext/tt/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
340340
}
341341
}
342342

343-
let unstable_feature = attr::find_stability(&sess.span_diagnostic,
343+
let unstable_feature = attr::find_stability(&sess,
344344
&def.attrs, def.span).and_then(|stability| {
345345
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
346346
Some((stability.feature, issue))

src/libsyntax_ext/deriving/generic/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ use syntax::source_map::{self, respan};
202202
use syntax::util::move_map::MoveMap;
203203
use syntax::ptr::P;
204204
use syntax::symbol::{Symbol, keywords};
205+
use syntax::parse::ParseSess;
205206
use syntax_pos::{DUMMY_SP, Span};
206-
use errors::Handler;
207207

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

@@ -412,7 +412,7 @@ impl<'a> TraitDef<'a> {
412412
match *item {
413413
Annotatable::Item(ref item) => {
414414
let is_packed = item.attrs.iter().any(|attr| {
415-
for r in attr::find_repr_attrs(&cx.parse_sess.span_diagnostic, attr) {
415+
for r in attr::find_repr_attrs(&cx.parse_sess, attr) {
416416
if let attr::ReprPacked(_) = r {
417417
return true;
418418
}
@@ -811,10 +811,10 @@ impl<'a> TraitDef<'a> {
811811
}
812812
}
813813

814-
fn find_repr_type_name(diagnostic: &Handler, type_attrs: &[ast::Attribute]) -> &'static str {
814+
fn find_repr_type_name(sess: &ParseSess, type_attrs: &[ast::Attribute]) -> &'static str {
815815
let mut repr_type_name = "isize";
816816
for a in type_attrs {
817-
for r in &attr::find_repr_attrs(diagnostic, a) {
817+
for r in &attr::find_repr_attrs(sess, a) {
818818
repr_type_name = match *r {
819819
attr::ReprPacked(_) | attr::ReprSimd | attr::ReprAlign(_) | attr::ReprTransparent =>
820820
continue,
@@ -1390,7 +1390,7 @@ impl<'a> MethodDef<'a> {
13901390
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
13911391
let mut discriminant_test = cx.expr_bool(sp, true);
13921392

1393-
let target_type_name = find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs);
1393+
let target_type_name = find_repr_type_name(&cx.parse_sess, type_attrs);
13941394

13951395
let mut first_ident = None;
13961396
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {

0 commit comments

Comments
 (0)