Skip to content

Commit be493fe

Browse files
committed
Auto merge of #69023 - Centril:parse_fn, r=petrochenkov
parse: unify function front matter parsing Part of #68728. - `const extern fn` feature gating is now done post-expansion such that we do not have conditional compatibilities of function qualifiers *in parsing*. - The `FnFrontMatter` grammar becomes: ```rust Extern = "extern" StringLit ; FnQual = "const"? "async"? "unsafe"? Extern? ; FnFrontMatter = FnQual "fn" ; ``` That is, all item contexts now *syntactically* allow `const async unsafe extern "C" fn` and use semantic restrictions to rule out combinations previously prevented syntactically. The semantic restrictions include in particular: - `fn`s in `extern { ... }` can have no qualifiers. - `const` and `async` cannot be combined. - We change `ast::{Unsafety, Spanned<Constness>}>` into `enum ast::{Unsafe, Const} { Yes(Span), No }` respectively. This change in formulation allow us to exclude `Span` in the case of `No`, which facilitates parsing. Moreover, we also add a `Span` to `IsAsync` which is renamed to `Async`. The new `Span`s in `Unsafety` and `Async` are then taken advantage of for better diagnostics. A reason this change was made is to have a more uniform and clear naming scheme. The HIR keeps the structures in AST (with those definitions moved into HIR) for now to avoid regressing perf. r? @petrochenkov
2 parents 2e6eace + 9828559 commit be493fe

File tree

94 files changed

+737
-694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+737
-694
lines changed

src/librustc/traits/auto_trait.rs

+1-2
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

+1-1
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

+4-4
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

+2-1
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

+2-2
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

+1-1
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

+2-2
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/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
106106
ref body,
107107
fn_decl_span,
108108
) => {
109-
if let IsAsync::Async { closure_id, .. } = asyncness {
109+
if let Async::Yes { closure_id, .. } = asyncness {
110110
self.lower_expr_async_closure(
111111
capture_clause,
112112
closure_id,

src/librustc_ast_lowering/item.rs

+35-22
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));
@@ -297,7 +299,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
297299
// `impl Future<Output = T>` here because lower_body
298300
// only cares about the input argument patterns in the function
299301
// declaration (decl), not the return types.
300-
let asyncness = header.asyncness.node;
302+
let asyncness = header.asyncness;
301303
let body_id =
302304
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
303305

@@ -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,
@@ -834,19 +836,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
834836
}
835837
AssocItemKind::Fn(ref sig, ref body) => {
836838
self.current_item = Some(i.span);
837-
let body_id = self.lower_maybe_async_body(
838-
i.span,
839-
&sig.decl,
840-
sig.header.asyncness.node,
841-
body.as_deref(),
842-
);
839+
let asyncness = sig.header.asyncness;
840+
let body_id =
841+
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
843842
let impl_trait_return_allow = !self.is_in_trait_impl;
844843
let (generics, sig) = self.lower_method_sig(
845844
&i.generics,
846845
sig,
847846
impl_item_def_id,
848847
impl_trait_return_allow,
849-
sig.header.asyncness.node.opt_return_id(),
848+
asyncness.opt_return_id(),
850849
);
851850

852851
(generics, hir::ImplItemKind::Method(sig, body_id))
@@ -1031,12 +1030,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10311030
&mut self,
10321031
span: Span,
10331032
decl: &FnDecl,
1034-
asyncness: IsAsync,
1033+
asyncness: Async,
10351034
body: Option<&Block>,
10361035
) -> hir::BodyId {
10371036
let closure_id = match asyncness {
1038-
IsAsync::Async { closure_id, .. } => closure_id,
1039-
IsAsync::NotAsync => return self.lower_fn_body_block(span, decl, body),
1037+
Async::Yes { closure_id, .. } => closure_id,
1038+
Async::No => return self.lower_fn_body_block(span, decl, body),
10401039
};
10411040

10421041
self.lower_body(|this| {
@@ -1245,9 +1244,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12451244

12461245
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
12471246
hir::FnHeader {
1248-
unsafety: h.unsafety,
1249-
asyncness: self.lower_asyncness(h.asyncness.node),
1250-
constness: h.constness.node,
1247+
unsafety: self.lower_unsafety(h.unsafety),
1248+
asyncness: self.lower_asyncness(h.asyncness),
1249+
constness: self.lower_constness(h.constness),
12511250
abi: self.lower_extern(h.ext),
12521251
}
12531252
}
@@ -1274,10 +1273,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
12741273
.emit();
12751274
}
12761275

1277-
fn lower_asyncness(&mut self, a: IsAsync) -> hir::IsAsync {
1276+
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
12781277
match a {
1279-
IsAsync::Async { .. } => hir::IsAsync::Async,
1280-
IsAsync::NotAsync => hir::IsAsync::NotAsync,
1278+
Async::Yes { .. } => hir::IsAsync::Async,
1279+
Async::No => hir::IsAsync::NotAsync,
1280+
}
1281+
}
1282+
1283+
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1284+
match c {
1285+
Const::Yes(_) => hir::Constness::Const,
1286+
Const::No => hir::Constness::NotConst,
1287+
}
1288+
}
1289+
1290+
pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety {
1291+
match u {
1292+
Unsafe::Yes(_) => hir::Unsafety::Unsafe,
1293+
Unsafe::No => hir::Unsafety::Normal,
12811294
}
12821295
}
12831296

src/librustc_ast_lowering/lib.rs

+1-1
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),

0 commit comments

Comments
 (0)