Skip to content

Commit e839b2e

Browse files
committed
Constness -> enum Const { Yes(Span), No }
Same idea for `Unsafety` & use new span for better diagnostics.
1 parent 2e6eace commit e839b2e

File tree

40 files changed

+238
-212
lines changed

40 files changed

+238
-212
lines changed

src/librustc/traits/auto_trait.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::ty::fold::TypeFolder;
99
use crate::ty::{Region, RegionVid};
1010

1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12-
use syntax::ast;
1312

1413
use std::collections::hash_map::Entry;
1514
use std::collections::VecDeque;
@@ -350,7 +349,7 @@ impl AutoTraitFinder<'tcx> {
350349
already_visited.remove(&pred);
351350
self.add_user_pred(
352351
&mut user_computed_preds,
353-
ty::Predicate::Trait(pred, ast::Constness::NotConst),
352+
ty::Predicate::Trait(pred, hir::Constness::NotConst),
354353
);
355354
predicates.push_back(pred);
356355
} else {

src/librustc/traits/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
695695
let unit_obligation = Obligation {
696696
predicate: ty::Predicate::Trait(
697697
predicate,
698-
ast::Constness::NotConst,
698+
hir::Constness::NotConst,
699699
),
700700
..obligation.clone()
701701
};

src/librustc/traits/select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ use crate::ty::fast_reject;
4040
use crate::ty::relate::TypeRelation;
4141
use crate::ty::subst::{Subst, SubstsRef};
4242
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
43-
use rustc_hir::def_id::DefId;
44-
4543
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4644
use rustc_hir as hir;
45+
use rustc_hir::def_id::DefId;
4746
use rustc_index::bit_set::GrowableBitSet;
4847
use rustc_span::symbol::sym;
4948
use rustc_target::spec::abi::Abi;
49+
use syntax::attr;
50+
5051
use std::cell::{Cell, RefCell};
5152
use std::cmp;
5253
use std::fmt::{self, Display};
5354
use std::iter;
5455
use std::rc::Rc;
55-
use syntax::{ast, attr};
5656

5757
pub use rustc::traits::types::select::*;
5858

@@ -677,7 +677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
677677
// if the regions match exactly.
678678
let cycle = stack.iter().skip(1).take_while(|s| s.depth >= cycle_depth);
679679
let cycle = cycle.map(|stack| {
680-
ty::Predicate::Trait(stack.obligation.predicate, ast::Constness::NotConst)
680+
ty::Predicate::Trait(stack.obligation.predicate, hir::Constness::NotConst)
681681
});
682682
if self.coinductive_match(cycle) {
683683
debug!("evaluate_stack({:?}) --> recursive, coinductive", stack.fresh_trait_ref);

src/librustc/ty/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
//! looking for, and does not need to visit anything else.
3333
3434
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
35+
use rustc_hir as hir;
3536
use rustc_hir::def_id::DefId;
3637

3738
use rustc_data_structures::fx::FxHashSet;
@@ -150,7 +151,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
150151
}
151152
}
152153

153-
impl TypeFoldable<'tcx> for syntax::ast::Constness {
154+
impl TypeFoldable<'tcx> for hir::Constness {
154155
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
155156
*self
156157
}

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ use rustc_data_structures::sync::{self, par_iter, Lrc, ParallelIterator};
3535
use rustc_hir as hir;
3636
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
3737
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
38-
use rustc_hir::{GlobMap, Node, TraitMap};
38+
use rustc_hir::{Constness, GlobMap, Node, TraitMap};
3939
use rustc_index::vec::{Idx, IndexVec};
4040
use rustc_macros::HashStable;
4141
use rustc_serialize::{self, Encodable, Encoder};
4242
use rustc_span::hygiene::ExpnId;
4343
use rustc_span::symbol::{kw, sym, Symbol};
4444
use rustc_span::Span;
4545
use rustc_target::abi::Align;
46-
use syntax::ast::{self, Constness, Ident, Name};
46+
use syntax::ast::{self, Ident, Name};
4747
use syntax::node_id::{NodeId, NodeMap, NodeSet};
4848

4949
use std::cell::RefCell;

src/librustc/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ define_print_and_forward_display! {
18181818
ty::Predicate<'tcx> {
18191819
match *self {
18201820
ty::Predicate::Trait(ref data, constness) => {
1821-
if let ast::Constness::Const = constness {
1821+
if let hir::Constness::Const = constness {
18221822
p!(write("const "));
18231823
}
18241824
p!(print(data))

src/librustc/ty/structural_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::mir::ProjectionKind;
77
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
88
use crate::ty::print::{FmtPrinter, Printer};
99
use crate::ty::{self, InferConst, Lift, Ty, TyCtxt};
10+
use rustc_hir as hir;
1011
use rustc_hir::def::Namespace;
1112
use rustc_hir::def_id::CRATE_DEF_INDEX;
1213
use rustc_index::vec::{Idx, IndexVec};
@@ -15,7 +16,6 @@ use smallvec::SmallVec;
1516
use std::fmt;
1617
use std::rc::Rc;
1718
use std::sync::Arc;
18-
use syntax::ast;
1919

2020
impl fmt::Debug for ty::GenericParamDef {
2121
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -236,7 +236,7 @@ impl fmt::Debug for ty::Predicate<'tcx> {
236236
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237237
match *self {
238238
ty::Predicate::Trait(ref a, constness) => {
239-
if let ast::Constness::Const = constness {
239+
if let hir::Constness::Const = constness {
240240
write!(f, "const ")?;
241241
}
242242
a.fmt(f)

src/librustc_ast_lowering/item.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6767
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
6868
let this = &mut ItemLowerer { lctx: this };
6969
if let ItemKind::Impl { constness, ref of_trait, .. } = item.kind {
70-
if constness == Constness::Const {
70+
if let Const::Yes(span) = constness {
7171
this.lctx
7272
.diagnostic()
73-
.span_err(item.span, "const trait impls are not yet implemented");
73+
.struct_span_err(item.span, "const trait impls are not yet implemented")
74+
.span_label(span, "const because of this")
75+
.emit();
7476
}
7577

7678
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
@@ -413,10 +415,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
413415
});
414416

415417
hir::ItemKind::Impl {
416-
unsafety,
418+
unsafety: self.lower_unsafety(unsafety),
417419
polarity,
418420
defaultness: self.lower_defaultness(defaultness, true /* [1] */),
419-
constness,
421+
constness: self.lower_constness(constness),
420422
generics,
421423
of_trait: trait_ref,
422424
self_ty: lowered_ty,
@@ -430,7 +432,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
430432
.alloc_from_iter(items.iter().map(|item| self.lower_trait_item_ref(item)));
431433
hir::ItemKind::Trait(
432434
is_auto,
433-
unsafety,
435+
self.lower_unsafety(unsafety),
434436
self.lower_generics(generics, ImplTraitContext::disallowed()),
435437
bounds,
436438
items,
@@ -1245,9 +1247,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12451247

12461248
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
12471249
hir::FnHeader {
1248-
unsafety: h.unsafety,
1250+
unsafety: self.lower_unsafety(h.unsafety),
12491251
asyncness: self.lower_asyncness(h.asyncness.node),
1250-
constness: h.constness.node,
1252+
constness: self.lower_constness(h.constness),
12511253
abi: self.lower_extern(h.ext),
12521254
}
12531255
}
@@ -1281,6 +1283,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
12811283
}
12821284
}
12831285

1286+
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1287+
match c {
1288+
Const::Yes(_) => hir::Constness::Const,
1289+
Const::No => hir::Constness::NotConst,
1290+
}
1291+
}
1292+
1293+
pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety {
1294+
match u {
1295+
Unsafe::Yes(_) => hir::Unsafety::Unsafe,
1296+
Unsafe::No => hir::Unsafety::Normal,
1297+
}
1298+
}
1299+
12841300
pub(super) fn lower_generics_mut(
12851301
&mut self,
12861302
generics: &Generics,

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11961196
&NodeMap::default(),
11971197
ImplTraitContext::disallowed(),
11981198
),
1199-
unsafety: f.unsafety,
1199+
unsafety: this.lower_unsafety(f.unsafety),
12001200
abi: this.lower_extern(f.ext),
12011201
decl: this.lower_fn_decl(&f.decl, None, false, None),
12021202
param_names: this.lower_fn_params_to_names(&f.decl),

src/librustc_ast_passes/ast_validation.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_parse::validate_attr;
1313
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
1414
use rustc_session::lint::LintBuffer;
1515
use rustc_session::Session;
16-
use rustc_span::source_map::Spanned;
1716
use rustc_span::symbol::{kw, sym};
1817
use rustc_span::Span;
1918
use std::mem;
@@ -234,16 +233,11 @@ impl<'a> AstValidator<'a> {
234233
}
235234
}
236235

237-
fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
238-
if constness.node == Constness::Const {
239-
struct_span_err!(
240-
self.session,
241-
constness.span,
242-
E0379,
243-
"trait fns cannot be declared const"
244-
)
245-
.span_label(constness.span, "trait fns cannot be const")
246-
.emit();
236+
fn check_trait_fn_not_const(&self, constness: Const) {
237+
if let Const::Yes(span) = constness {
238+
struct_span_err!(self.session, span, E0379, "trait fns cannot be declared const")
239+
.span_label(span, "trait fns cannot be const")
240+
.emit();
247241
}
248242
}
249243

@@ -487,7 +481,7 @@ impl<'a> AstValidator<'a> {
487481
(Some(FnCtxt::Foreign), _) => return,
488482
(Some(FnCtxt::Free), Some(header)) => match header.ext {
489483
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }) | Extern::Implicit
490-
if header.unsafety == Unsafety::Unsafe =>
484+
if matches!(header.unsafety, Unsafe::Yes(_)) =>
491485
{
492486
return;
493487
}
@@ -514,12 +508,13 @@ impl<'a> AstValidator<'a> {
514508
/// FIXME(const_generics): Is this really true / necessary? Discuss with @varkor.
515509
/// At any rate, the restriction feels too syntactic. Consider moving it to e.g. typeck.
516510
fn check_const_fn_const_generic(&self, span: Span, sig: &FnSig, generics: &Generics) {
517-
if sig.header.constness.node == Constness::Const {
511+
if let Const::Yes(const_span) = sig.header.constness {
518512
// Look for const generics and error if we find any.
519513
for param in &generics.params {
520514
if let GenericParamKind::Const { .. } = param.kind {
521515
self.err_handler()
522516
.struct_span_err(span, "const parameters are not permitted in `const fn`")
517+
.span_label(const_span, "`const fn` because of this")
523518
.emit();
524519
}
525520
}
@@ -754,13 +749,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
754749
.help("use `auto trait Trait {}` instead")
755750
.emit();
756751
}
757-
if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
752+
if let (Unsafe::Yes(span), ImplPolarity::Negative) = (unsafety, polarity) {
758753
struct_span_err!(
759754
this.session,
760755
item.span,
761756
E0198,
762757
"negative impls cannot be unsafe"
763758
)
759+
.span_label(span, "unsafe because of this")
764760
.emit();
765761
}
766762

@@ -782,13 +778,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
782778
&item.vis,
783779
Some("place qualifiers on individual impl items instead"),
784780
);
785-
if unsafety == Unsafety::Unsafe {
781+
if let Unsafe::Yes(span) = unsafety {
786782
struct_span_err!(
787783
self.session,
788784
item.span,
789785
E0197,
790786
"inherent impls cannot be unsafe"
791787
)
788+
.span_label(span, "unsafe because of this")
792789
.emit();
793790
}
794791
if polarity == ImplPolarity::Negative {
@@ -800,9 +797,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
800797
.note("only trait implementations may be annotated with default")
801798
.emit();
802799
}
803-
if constness == Constness::Const {
800+
if let Const::Yes(span) = constness {
804801
self.err_handler()
805802
.struct_span_err(item.span, "inherent impls cannot be `const`")
803+
.span_label(span, "`const` because of this")
806804
.note("only trait implementations may be annotated with `const`")
807805
.emit();
808806
}

0 commit comments

Comments
 (0)