Skip to content

Commit ae50725

Browse files
authored
Rollup merge of #69361 - Centril:free-ty-alias, r=petrochenkov
parse: allow `type Foo: Ord` syntactically This addresses: > (Work still remains to fuse this with free type aliases, but this can be done later.) in #69194. r? @petrochenkov
2 parents cec0003 + 9f3dfd2 commit ae50725

22 files changed

+242
-116
lines changed

src/librustc_ast_lowering/item.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -297,28 +297,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
297297
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
298298
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
299299
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
300-
ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() {
300+
ItemKind::TyAlias(ref generics, _, Some(ref ty)) => match ty.kind.opaque_top_hack() {
301301
None => {
302302
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
303303
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
304304
hir::ItemKind::TyAlias(ty, generics)
305305
}
306306
Some(bounds) => {
307+
let ctx = || ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc);
307308
let ty = hir::OpaqueTy {
308-
generics: self.lower_generics(
309-
generics,
310-
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
311-
),
312-
bounds: self.lower_param_bounds(
313-
bounds,
314-
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
315-
),
309+
generics: self.lower_generics(generics, ctx()),
310+
bounds: self.lower_param_bounds(bounds, ctx()),
316311
impl_trait_fn: None,
317312
origin: hir::OpaqueTyOrigin::TypeAlias,
318313
};
319314
hir::ItemKind::OpaqueTy(ty)
320315
}
321316
},
317+
ItemKind::TyAlias(ref generics, _, None) => {
318+
let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err));
319+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
320+
hir::ItemKind::TyAlias(ty, generics)
321+
}
322322
ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemKind::Enum(
323323
hir::EnumDef {
324324
variants: self.arena.alloc_from_iter(

src/librustc_ast_lowering/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
462462
ItemKind::Struct(_, ref generics)
463463
| ItemKind::Union(_, ref generics)
464464
| ItemKind::Enum(_, ref generics)
465-
| ItemKind::TyAlias(_, ref generics)
465+
| ItemKind::TyAlias(ref generics, ..)
466466
| ItemKind::Trait(_, _, ref generics, ..) => {
467467
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
468468
let count = generics

src/librustc_ast_passes/ast_validation.rs

+7
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
969969
let msg = "free static item without body";
970970
self.error_item_without_body(item.span, "static", msg, " = <expr>;");
971971
}
972+
ItemKind::TyAlias(_, ref bounds, ref body) => {
973+
if body.is_none() {
974+
let msg = "free type alias without body";
975+
self.error_item_without_body(item.span, "type", msg, " = <type>;");
976+
}
977+
self.check_type_no_bounds(bounds, "this context");
978+
}
972979
_ => {}
973980
}
974981

src/librustc_ast_passes/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
372372
gate_feature_post!(&self, decl_macro, i.span, msg);
373373
}
374374

375-
ast::ItemKind::TyAlias(ref ty, ..) => self.check_impl_trait(&ty),
375+
ast::ItemKind::TyAlias(_, _, Some(ref ty)) => self.check_impl_trait(&ty),
376376

377377
_ => {}
378378
}

src/librustc_ast_pretty/pprust.rs

+37-33
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::pp::Breaks::{Consistent, Inconsistent};
22
use crate::pp::{self, Breaks};
33

44
use rustc_span::edition::Edition;
5-
use rustc_span::source_map::{dummy_spanned, SourceMap, Spanned};
5+
use rustc_span::source_map::{SourceMap, Spanned};
66
use rustc_span::symbol::{kw, sym};
77
use rustc_span::{BytePos, FileName, Span};
88
use syntax::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
@@ -1026,27 +1026,26 @@ impl<'a> State<'a> {
10261026
span: Span,
10271027
ident: ast::Ident,
10281028
attrs: &[Attribute],
1029-
defaultness: ast::Defaultness,
1029+
def: ast::Defaultness,
10301030
kind: &ast::AssocItemKind,
10311031
vis: &ast::Visibility,
10321032
) {
10331033
self.ann.pre(self, AnnNode::SubItem(id));
10341034
self.hardbreak_if_not_bol();
10351035
self.maybe_print_comment(span.lo());
10361036
self.print_outer_attributes(attrs);
1037-
self.print_defaultness(defaultness);
10381037
match kind {
10391038
ast::ForeignItemKind::Fn(sig, gen, body) => {
1040-
self.print_fn_full(sig, ident, gen, vis, body.as_deref(), attrs);
1039+
self.print_fn_full(sig, ident, gen, vis, def, body.as_deref(), attrs);
10411040
}
10421041
ast::ForeignItemKind::Const(ty, body) => {
1043-
self.print_item_const(ident, None, ty, body.as_deref(), vis);
1042+
self.print_item_const(ident, None, ty, body.as_deref(), vis, def);
10441043
}
10451044
ast::ForeignItemKind::Static(ty, mutbl, body) => {
1046-
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis);
1045+
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
10471046
}
10481047
ast::ForeignItemKind::TyAlias(generics, bounds, ty) => {
1049-
self.print_associated_type(ident, generics, bounds, ty.as_deref());
1048+
self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, def);
10501049
}
10511050
ast::ForeignItemKind::Macro(m) => {
10521051
self.print_mac(m);
@@ -1065,13 +1064,17 @@ impl<'a> State<'a> {
10651064
ty: &ast::Ty,
10661065
body: Option<&ast::Expr>,
10671066
vis: &ast::Visibility,
1067+
defaultness: ast::Defaultness,
10681068
) {
1069+
self.head("");
1070+
self.print_visibility(vis);
1071+
self.print_defaultness(defaultness);
10691072
let leading = match mutbl {
10701073
None => "const",
10711074
Some(ast::Mutability::Not) => "static",
10721075
Some(ast::Mutability::Mut) => "static mut",
10731076
};
1074-
self.head(visibility_qualified(vis, leading));
1077+
self.word_space(leading);
10751078
self.print_ident(ident);
10761079
self.word_space(":");
10771080
self.print_type(ty);
@@ -1091,7 +1094,12 @@ impl<'a> State<'a> {
10911094
generics: &ast::Generics,
10921095
bounds: &ast::GenericBounds,
10931096
ty: Option<&ast::Ty>,
1097+
vis: &ast::Visibility,
1098+
defaultness: ast::Defaultness,
10941099
) {
1100+
self.head("");
1101+
self.print_visibility(vis);
1102+
self.print_defaultness(defaultness);
10951103
self.word_space("type");
10961104
self.print_ident(ident);
10971105
self.print_generic_params(&generics.params);
@@ -1102,7 +1110,9 @@ impl<'a> State<'a> {
11021110
self.word_space("=");
11031111
self.print_type(ty);
11041112
}
1105-
self.s.word(";")
1113+
self.s.word(";");
1114+
self.end(); // end inner head-block
1115+
self.end(); // end outer head-block
11061116
}
11071117

11081118
/// Pretty-prints an item.
@@ -1133,13 +1143,17 @@ impl<'a> State<'a> {
11331143
self.end(); // end outer head-block
11341144
}
11351145
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
1136-
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis);
1146+
let def = ast::Defaultness::Final;
1147+
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
11371148
}
11381149
ast::ItemKind::Const(ref ty, ref body) => {
1139-
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
1150+
let def = ast::Defaultness::Final;
1151+
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
11401152
}
11411153
ast::ItemKind::Fn(ref sig, ref gen, ref body) => {
1142-
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
1154+
let def = ast::Defaultness::Final;
1155+
let body = body.as_deref();
1156+
self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs);
11431157
}
11441158
ast::ItemKind::Mod(ref _mod) => {
11451159
self.head(visibility_qualified(&item.vis, "mod"));
@@ -1171,18 +1185,10 @@ impl<'a> State<'a> {
11711185
self.s.word(ga.asm.to_string());
11721186
self.end();
11731187
}
1174-
ast::ItemKind::TyAlias(ref ty, ref generics) => {
1175-
self.head(visibility_qualified(&item.vis, "type"));
1176-
self.print_ident(item.ident);
1177-
self.print_generic_params(&generics.params);
1178-
self.end(); // end the inner ibox
1179-
1180-
self.print_where_clause(&generics.where_clause);
1181-
self.s.space();
1182-
self.word_space("=");
1183-
self.print_type(ty);
1184-
self.s.word(";");
1185-
self.end(); // end the outer ibox
1188+
ast::ItemKind::TyAlias(ref generics, ref bounds, ref ty) => {
1189+
let def = ast::Defaultness::Final;
1190+
let ty = ty.as_deref();
1191+
self.print_associated_type(item.ident, generics, bounds, ty, &item.vis, def);
11861192
}
11871193
ast::ItemKind::Enum(ref enum_definition, ref params) => {
11881194
self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
@@ -2370,13 +2376,16 @@ impl<'a> State<'a> {
23702376
name: ast::Ident,
23712377
generics: &ast::Generics,
23722378
vis: &ast::Visibility,
2379+
defaultness: ast::Defaultness,
23732380
body: Option<&ast::Block>,
23742381
attrs: &[ast::Attribute],
23752382
) {
23762383
if body.is_some() {
23772384
self.head("");
23782385
}
2379-
self.print_fn(&sig.decl, sig.header, Some(name), generics, vis);
2386+
self.print_visibility(vis);
2387+
self.print_defaultness(defaultness);
2388+
self.print_fn(&sig.decl, sig.header, Some(name), generics);
23802389
if let Some(body) = body {
23812390
self.nbsp();
23822391
self.print_block_with_attrs(body, attrs);
@@ -2391,10 +2400,8 @@ impl<'a> State<'a> {
23912400
header: ast::FnHeader,
23922401
name: Option<ast::Ident>,
23932402
generics: &ast::Generics,
2394-
vis: &ast::Visibility,
23952403
) {
2396-
self.print_fn_header_info(header, vis);
2397-
2404+
self.print_fn_header_info(header);
23982405
if let Some(name) = name {
23992406
self.nbsp();
24002407
self.print_ident(name);
@@ -2672,8 +2679,7 @@ impl<'a> State<'a> {
26722679
span: rustc_span::DUMMY_SP,
26732680
};
26742681
let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
2675-
let vis = dummy_spanned(ast::VisibilityKind::Inherited);
2676-
self.print_fn(decl, header, name, &generics, &vis);
2682+
self.print_fn(decl, header, name, &generics);
26772683
self.end();
26782684
}
26792685

@@ -2700,9 +2706,7 @@ impl<'a> State<'a> {
27002706
}
27012707
}
27022708

2703-
crate fn print_fn_header_info(&mut self, header: ast::FnHeader, vis: &ast::Visibility) {
2704-
self.s.word(visibility_qualified(vis, ""));
2705-
2709+
crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
27062710
self.print_constness(header.constness);
27072711
self.print_asyncness(header.asyncness);
27082712
self.print_unsafety(header.unsafety);

src/librustc_ast_pretty/pprust/tests.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
use rustc_span;
4-
use rustc_span::source_map::{dummy_spanned, respan};
4+
use rustc_span::source_map::respan;
55
use syntax::ast;
66
use syntax::with_default_globals;
77

@@ -13,13 +13,7 @@ fn fun_to_string(
1313
) -> String {
1414
to_string(|s| {
1515
s.head("");
16-
s.print_fn(
17-
decl,
18-
header,
19-
Some(name),
20-
generics,
21-
&dummy_spanned(ast::VisibilityKind::Inherited),
22-
);
16+
s.print_fn(decl, header, Some(name), generics);
2317
s.end(); // Close the head box.
2418
s.end(); // Close the outer box.
2519
})

src/librustc_parse/parser/item.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ impl<'a> Parser<'a> {
156156
self.parse_item_mod(attrs)?
157157
} else if self.eat_keyword(kw::Type) {
158158
// TYPE ITEM
159-
let (ident, ty, generics) = self.parse_type_alias()?;
160-
(ident, ItemKind::TyAlias(ty, generics))
159+
self.parse_type_alias()?
161160
} else if self.eat_keyword(kw::Enum) {
162161
// ENUM ITEM
163162
self.parse_item_enum()?
@@ -676,7 +675,10 @@ impl<'a> Parser<'a> {
676675
vis: &Visibility,
677676
) -> PResult<'a, (Ident, AssocItemKind)> {
678677
if self.eat_keyword(kw::Type) {
679-
self.parse_assoc_ty()
678+
match self.parse_type_alias()? {
679+
(ident, ItemKind::TyAlias(a, b, c)) => Ok((ident, AssocItemKind::TyAlias(a, b, c))),
680+
_ => unreachable!(),
681+
}
680682
} else if self.check_fn_front_matter() {
681683
let (ident, sig, generics, body) = self.parse_fn(at_end, attrs, req_name)?;
682684
Ok((ident, AssocItemKind::Fn(sig, generics, body)))
@@ -700,10 +702,12 @@ impl<'a> Parser<'a> {
700702
}
701703
}
702704

703-
/// Parses the following grammar:
704-
///
705-
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
706-
fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind)> {
705+
/// Parses a `type` alias with the following grammar:
706+
/// ```
707+
/// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
708+
/// ```
709+
/// The `"type"` has already been eaten.
710+
fn parse_type_alias(&mut self) -> PResult<'a, (Ident, ItemKind)> {
707711
let ident = self.parse_ident()?;
708712
let mut generics = self.parse_generics()?;
709713

@@ -715,7 +719,7 @@ impl<'a> Parser<'a> {
715719
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
716720
self.expect_semi()?;
717721

718-
Ok((ident, AssocItemKind::TyAlias(generics, bounds, default)))
722+
Ok((ident, ItemKind::TyAlias(generics, bounds, default)))
719723
}
720724

721725
/// Parses a `UseTree`.
@@ -989,18 +993,6 @@ impl<'a> Parser<'a> {
989993
P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
990994
}
991995

992-
/// Parses the grammar:
993-
/// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
994-
fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
995-
let ident = self.parse_ident()?;
996-
let mut tps = self.parse_generics()?;
997-
tps.where_clause = self.parse_where_clause()?;
998-
self.expect(&token::Eq)?;
999-
let ty = self.parse_ty()?;
1000-
self.expect_semi()?;
1001-
Ok((ident, ty, tps))
1002-
}
1003-
1004996
/// Parses an enum declaration.
1005997
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1006998
let id = self.parse_ident()?;

src/librustc_resolve/build_reduced_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
718718
}
719719

720720
// These items live in the type namespace.
721-
ItemKind::TyAlias(ref ty, _) => {
722-
let def_kind = match ty.kind.opaque_top_hack() {
721+
ItemKind::TyAlias(_, _, ref ty) => {
722+
let def_kind = match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
723723
None => DefKind::TyAlias,
724724
Some(_) => DefKind::OpaqueTy,
725725
};

src/librustc_resolve/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
797797
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
798798

799799
match item.kind {
800-
ItemKind::TyAlias(_, ref generics) | ItemKind::Fn(_, ref generics, _) => {
800+
ItemKind::TyAlias(ref generics, _, _) | ItemKind::Fn(_, ref generics, _) => {
801801
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
802802
visit::walk_item(this, item)
803803
});

src/librustc_save_analysis/dump_visitor.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1311,12 +1311,15 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
13111311
self.process_mod(item);
13121312
visit::walk_mod(self, m);
13131313
}
1314-
TyAlias(ref ty, ref ty_params) => {
1314+
TyAlias(ref ty_params, _, ref ty) => {
13151315
let qualname = format!(
13161316
"::{}",
13171317
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
13181318
);
1319-
let value = ty_to_string(&ty);
1319+
let value = match ty {
1320+
Some(ty) => ty_to_string(&ty),
1321+
None => "_".to_string(),
1322+
};
13201323
if !self.span.filter_generated(item.ident.span) {
13211324
let span = self.span_from_span(item.ident.span);
13221325
let id = id_from_node_id(item.id, &self.save_ctxt);
@@ -1341,7 +1344,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
13411344
);
13421345
}
13431346

1344-
self.visit_ty(&ty);
1347+
walk_list!(self, visit_ty, ty);
13451348
self.process_generic_params(ty_params, &qualname, item.id);
13461349
}
13471350
Mac(_) => (),

0 commit comments

Comments
 (0)