Skip to content

Commit fd4a6a1

Browse files
Add a constness field to ast::TraitRef
This is used for both the `?const` syntax in bounds as well as the `impl const Trait` syntax. I also considered handling these separately by adding a variant of `TraitBoundModifier` and a field to `ItemKind::Impl`, but this approach was less intrusive.
1 parent 6fc4158 commit fd4a6a1

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/librustc_expand/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a> ExtCtxt<'a> {
110110
}
111111

112112
pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
113-
ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
113+
ast::TraitRef { path, constness: None, ref_id: ast::DUMMY_NODE_ID }
114114
}
115115

116116
pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {

src/libsyntax/ast.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ impl Expr {
10331033
pub fn to_bound(&self) -> Option<GenericBound> {
10341034
match &self.kind {
10351035
ExprKind::Path(None, path) => Some(GenericBound::Trait(
1036-
PolyTraitRef::new(Vec::new(), path.clone(), self.span),
1036+
PolyTraitRef::new(Vec::new(), path.clone(), None, self.span),
10371037
TraitBoundModifier::None,
10381038
)),
10391039
_ => None,
@@ -2376,6 +2376,15 @@ pub enum AttrKind {
23762376
pub struct TraitRef {
23772377
pub path: Path,
23782378
pub ref_id: NodeId,
2379+
2380+
/// The `const` modifier, if any, that appears before this trait.
2381+
///
2382+
/// | | `constness` |
2383+
/// |----------------|-----------------------------|
2384+
/// | `Trait` | `None` |
2385+
/// | `const Trait` | `Some(Constness::Const)` |
2386+
/// | `?const Trait` | `Some(Constness::NotConst)` |
2387+
pub constness: Option<Constness>,
23792388
}
23802389

23812390
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -2390,10 +2399,15 @@ pub struct PolyTraitRef {
23902399
}
23912400

23922401
impl PolyTraitRef {
2393-
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2402+
pub fn new(
2403+
generic_params: Vec<GenericParam>,
2404+
path: Path,
2405+
constness: Option<Constness>,
2406+
span: Span,
2407+
) -> Self {
23942408
PolyTraitRef {
23952409
bound_generic_params: generic_params,
2396-
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
2410+
trait_ref: TraitRef { path, constness, ref_id: DUMMY_NODE_ID },
23972411
span,
23982412
}
23992413
}

src/libsyntax/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,8 @@ pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut
838838
}
839839
}
840840

841-
pub fn noop_visit_trait_ref<T: MutVisitor>(TraitRef { path, ref_id }: &mut TraitRef, vis: &mut T) {
841+
pub fn noop_visit_trait_ref<T: MutVisitor>(tr: &mut TraitRef, vis: &mut T) {
842+
let TraitRef { path, ref_id, constness: _ } = tr;
842843
vis.visit_path(path);
843844
vis.visit_id(ref_id);
844845
}

0 commit comments

Comments
 (0)