Skip to content

Commit b687086

Browse files
committed
Adds new keyword only
1 parent 3179a47 commit b687086

11 files changed

Lines changed: 35 additions & 7 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,6 +3181,8 @@ pub enum BoundPolarity {
31813181
Negative(Span),
31823182
/// `Type: ?Trait`
31833183
Maybe(Span),
3184+
/// `Type: only Trait`,
3185+
Only(Span),
31843186
}
31853187

31863188
impl BoundPolarity {
@@ -3189,6 +3191,7 @@ impl BoundPolarity {
31893191
Self::Positive => "",
31903192
Self::Negative(_) => "!",
31913193
Self::Maybe(_) => "?",
3194+
Self::Only(_) => "only",
31923195
}
31933196
}
31943197
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
27732773
BoundPolarity::Positive => BoundPolarity::Positive,
27742774
BoundPolarity::Negative(span) => BoundPolarity::Negative(self.lower_span(span)),
27752775
BoundPolarity::Maybe(span) => BoundPolarity::Maybe(self.lower_span(span)),
2776+
BoundPolarity::Only(span) => BoundPolarity::Only(self.lower_span(span)),
27762777
};
27772778
hir::TraitBoundModifiers { constness, polarity }
27782779
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,9 @@ impl<'a> State<'a> {
14781478
}
14791479
match polarity {
14801480
ast::BoundPolarity::Positive => {}
1481-
ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1481+
ast::BoundPolarity::Negative(_)
1482+
| ast::BoundPolarity::Maybe(_)
1483+
| ast::BoundPolarity::Only(_) => {
14821484
self.word(polarity.as_str());
14831485
}
14841486
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct CollectedBound {
3131
maybe: bool,
3232
/// `!Trait`
3333
negative: bool,
34+
/// `only Trait`
35+
only_modifier: bool,
3436
}
3537

3638
impl CollectedBound {
@@ -112,6 +114,7 @@ fn collect_bounds<'a, 'tcx>(
112114
match ptr.modifiers.polarity {
113115
hir::BoundPolarity::Maybe(_) => collect_into.maybe = true,
114116
hir::BoundPolarity::Negative(_) => collect_into.negative = true,
117+
hir::BoundPolarity::Only(_) => collect_into.only_modifier = true,
115118
hir::BoundPolarity::Positive => collect_into.positive = true,
116119
}
117120
});

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
986986
self.require_bound_to_relax_default_trait(trait_ref, span);
987987
true
988988
}
989+
hir::BoundPolarity::Only(_) => {
990+
true
991+
}
989992
};
990993
let bounds = if transient { &mut Vec::new() } else { bounds };
991994

992995
let polarity = match polarity {
993-
hir::BoundPolarity::Positive | hir::BoundPolarity::Maybe(_) => {
994-
ty::PredicatePolarity::Positive
995-
}
996+
// TODO: Unsure on this
997+
hir::BoundPolarity::Positive
998+
| hir::BoundPolarity::Maybe(_)
999+
| hir::BoundPolarity::Only(_) => ty::PredicatePolarity::Positive,
9961000
hir::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
9971001
};
9981002

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ impl<'a> State<'a> {
824824
hir::BoundPolarity::Positive => {}
825825
hir::BoundPolarity::Negative(_) => self.word("!"),
826826
hir::BoundPolarity::Maybe(_) => self.word("?"),
827+
hir::BoundPolarity::Only(_) => self.word_space("only"),
827828
}
828829
self.print_formal_generic_params(t.bound_generic_params);
829830
self.print_trait_ref(&t.trait_ref);

compiler/rustc_parse/src/parser/token_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub enum TokenType {
106106
KwMod,
107107
KwMove,
108108
KwMut,
109+
KwOnly,
109110
KwPub,
110111
KwRaw,
111112
KwRef,
@@ -540,6 +541,7 @@ macro_rules! exp {
540541
(Mod) => { exp!(@kw, Mod, KwMod) };
541542
(Move) => { exp!(@kw, Move, KwMove) };
542543
(Mut) => { exp!(@kw, Mut, KwMut) };
544+
(Only) => { exp!(@kw, Only, KwOnly) };
543545
(Pub) => { exp!(@kw, Pub, KwPub) };
544546
(Raw) => { exp!(@kw, Raw, KwRaw) };
545547
(Ref) => { exp!(@kw, Ref, KwRef) };

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,7 @@ impl<'a> Parser<'a> {
11201120
|| self.check(exp!(OpenParen))
11211121
|| self.can_begin_maybe_const_bound()
11221122
|| self.check_keyword(exp!(Const))
1123+
|| self.check_keyword(exp!(Only))
11231124
|| self.check_keyword(exp!(Async))
11241125
|| self.check_keyword(exp!(Use))
11251126
}
@@ -1201,7 +1202,9 @@ impl<'a> Parser<'a> {
12011202

12021203
match polarity {
12031204
BoundPolarity::Positive => {}
1204-
BoundPolarity::Negative(span) | BoundPolarity::Maybe(span) => {
1205+
BoundPolarity::Negative(span)
1206+
| BoundPolarity::Maybe(span)
1207+
| BoundPolarity::Only(span) => {
12051208
return self
12061209
.dcx()
12071210
.emit_err(errors::ModifierLifetime { span, modifier: polarity.as_str() });
@@ -1265,6 +1268,8 @@ impl<'a> Parser<'a> {
12651268
} else if self.eat(exp!(Bang)) {
12661269
self.psess.gated_spans.gate(sym::negative_bounds, self.prev_token.span);
12671270
BoundPolarity::Negative(self.prev_token.span)
1271+
} else if self.eat_keyword(exp!(Only)) {
1272+
BoundPolarity::Only(self.prev_token.span)
12681273
} else {
12691274
BoundPolarity::Positive
12701275
};
@@ -1274,7 +1279,9 @@ impl<'a> Parser<'a> {
12741279
BoundPolarity::Positive => {
12751280
// All trait bound modifiers allowed to combine with positive polarity
12761281
}
1277-
BoundPolarity::Maybe(polarity_span) | BoundPolarity::Negative(polarity_span) => {
1282+
BoundPolarity::Maybe(polarity_span)
1283+
| BoundPolarity::Negative(polarity_span)
1284+
| BoundPolarity::Only(polarity_span) => {
12781285
match (asyncness, constness) {
12791286
(BoundAsyncness::Normal, BoundConstness::Never) => {
12801287
// Ok, no modifiers.
@@ -1346,7 +1353,9 @@ impl<'a> Parser<'a> {
13461353

13471354
if let Some(binder_span) = binder_span {
13481355
match modifiers.polarity {
1349-
BoundPolarity::Negative(polarity_span) | BoundPolarity::Maybe(polarity_span) => {
1356+
BoundPolarity::Negative(polarity_span)
1357+
| BoundPolarity::Maybe(polarity_span)
1358+
| BoundPolarity::Only(polarity_span) => {
13501359
self.dcx().emit_err(errors::BinderAndPolarity {
13511360
binder_span,
13521361
polarity_span,

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ symbols! {
6161
Mod: "mod",
6262
Move: "move",
6363
Mut: "mut",
64+
Only: "only",
6465
Pub: "pub",
6566
Ref: "ref",
6667
Return: "return",

src/librustdoc/html/format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub(crate) fn print_generic_bound(
267267
hir::BoundPolarity::Positive => "",
268268
hir::BoundPolarity::Maybe(_) => "?",
269269
hir::BoundPolarity::Negative(_) => "!",
270+
hir::BoundPolarity::Only(_) => "only",
270271
})?;
271272
print_poly_trait(ty, cx).fmt(f)
272273
}

0 commit comments

Comments
 (0)