Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse the syntax described in RFC 2632 #67820

Merged
merged 13 commits into from
Jan 10, 2020
Merged
Prev Previous commit
Next Next commit
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.
ecstatic-morse committed Jan 10, 2020
commit fd4a6a12136c5b5d6bce4081e95890df1fd1febd
2 changes: 1 addition & 1 deletion src/librustc_expand/build.rs
Original file line number Diff line number Diff line change
@@ -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 {
20 changes: 17 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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)]
@@ -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,
}
}
3 changes: 2 additions & 1 deletion src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
@@ -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);
}