Skip to content

Commit

Permalink
Add a constness field to ast::TraitRef
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ecstatic-morse committed Jan 10, 2020
1 parent 6fc4158 commit fd4a6a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/librustc_expand/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> ExtCtxt<'a> {
}

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

pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
Expand Down
20 changes: 17 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ impl Expr {
pub fn to_bound(&self) -> Option<GenericBound> {
match &self.kind {
ExprKind::Path(None, path) => Some(GenericBound::Trait(
PolyTraitRef::new(Vec::new(), path.clone(), self.span),
PolyTraitRef::new(Vec::new(), path.clone(), None, self.span),
TraitBoundModifier::None,
)),
_ => None,
Expand Down Expand Up @@ -2376,6 +2376,15 @@ pub enum AttrKind {
pub struct TraitRef {
pub path: Path,
pub ref_id: NodeId,

/// The `const` modifier, if any, that appears before this trait.
///
/// | | `constness` |
/// |----------------|-----------------------------|
/// | `Trait` | `None` |
/// | `const Trait` | `Some(Constness::Const)` |
/// | `?const Trait` | `Some(Constness::NotConst)` |
pub constness: Option<Constness>,
}

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

impl PolyTraitRef {
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
pub fn new(
generic_params: Vec<GenericParam>,
path: Path,
constness: Option<Constness>,
span: Span,
) -> Self {
PolyTraitRef {
bound_generic_params: generic_params,
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
trait_ref: TraitRef { path, constness, ref_id: DUMMY_NODE_ID },
span,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut
}
}

pub fn noop_visit_trait_ref<T: MutVisitor>(TraitRef { path, ref_id }: &mut TraitRef, vis: &mut T) {
pub fn noop_visit_trait_ref<T: MutVisitor>(tr: &mut TraitRef, vis: &mut T) {
let TraitRef { path, ref_id, constness: _ } = tr;
vis.visit_path(path);
vis.visit_id(ref_id);
}
Expand Down

0 comments on commit fd4a6a1

Please sign in to comment.