X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fspanned.rs;h=7bf370c131ed46d422b101dcff4776c28f111d52;hb=9ba373f822eea6bdb4f3672d93bfa94092bfa255;hp=7181512273efc4801e16ee8cf159e255600f8ccb;hpb=3c08da3433fe63595e4d46b95f0b1483c1c2650d;p=rust.git diff --git a/src/spanned.rs b/src/spanned.rs index 7181512273e..7bf370c131e 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -1,41 +1,43 @@ -// Copyright 2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use syntax::ast; -use syntax::codemap::Span; - -use macros::MacroArg; -use utils::{mk_sp, outer_attributes}; +use std::cmp::max; + +use rustc_ast::{ast, ptr}; +use rustc_span::{source_map, Span}; + +use crate::macros::MacroArg; +use crate::utils::{mk_sp, outer_attributes}; /// Spanned returns a span including attributes, if available. -pub trait Spanned { +pub(crate) trait Spanned { fn span(&self) -> Span; } +impl Spanned for ptr::P { + fn span(&self) -> Span { + (**self).span() + } +} + +impl Spanned for source_map::Spanned { + fn span(&self) -> Span { + self.span + } +} + macro_rules! span_with_attrs_lo_hi { - ($this:ident, $lo:expr, $hi:expr) => { - { - let attrs = outer_attributes(&$this.attrs); - if attrs.is_empty() { - mk_sp($lo, $hi) - } else { - mk_sp(attrs[0].span.lo(), $hi) - } + ($this:ident, $lo:expr, $hi:expr) => {{ + let attrs = outer_attributes(&$this.attrs); + if attrs.is_empty() { + mk_sp($lo, $hi) + } else { + mk_sp(attrs[0].span.lo(), $hi) } - } + }}; } macro_rules! span_with_attrs { ($this:ident) => { span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi()) - } + }; } macro_rules! implement_spanned { @@ -45,34 +47,33 @@ fn span(&self) -> Span { span_with_attrs!(self) } } - } + }; } // Implement `Spanned` for structs with `attrs` field. +implement_spanned!(ast::AssocItem); implement_spanned!(ast::Expr); implement_spanned!(ast::Field); implement_spanned!(ast::ForeignItem); implement_spanned!(ast::Item); implement_spanned!(ast::Local); -implement_spanned!(ast::TraitItem); -implement_spanned!(ast::ImplItem); impl Spanned for ast::Stmt { fn span(&self) -> Span { - match self.node { + match self.kind { ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()), ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()), ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => { mk_sp(expr.span().lo(), self.span.hi()) } - ast::StmtKind::Mac(ref mac) => { - let (_, _, ref attrs) = **mac; - if attrs.is_empty() { + ast::StmtKind::MacCall(ref mac_stmt) => { + if mac_stmt.attrs.is_empty() { self.span } else { - mk_sp(attrs[0].span.lo(), self.span.hi()) + mk_sp(mac_stmt.attrs[0].span.lo(), self.span.hi()) } } + ast::StmtKind::Empty => self.span, } } } @@ -91,13 +92,18 @@ fn span(&self) -> Span { impl Spanned for ast::Arm { fn span(&self) -> Span { - span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi()) + let lo = if self.attrs.is_empty() { + self.pat.span.lo() + } else { + self.attrs[0].span.lo() + }; + span_with_attrs_lo_hi!(self, lo, self.body.span.hi()) } } -impl Spanned for ast::Arg { +impl Spanned for ast::Param { fn span(&self) -> Span { - if ::items::is_named_arg(self) { + if crate::items::is_named_param(self) { mk_sp(self.pat.span.lo(), self.ty.span.hi()) } else { self.ty.span @@ -105,6 +111,30 @@ fn span(&self) -> Span { } } +impl Spanned for ast::GenericParam { + fn span(&self) -> Span { + let lo = if self.attrs.is_empty() { + self.ident.span.lo() + } else { + self.attrs[0].span.lo() + }; + let hi = if self.bounds.is_empty() { + self.ident.span.hi() + } else { + self.bounds.last().unwrap().span().hi() + }; + let ty_hi = if let ast::GenericParamKind::Type { + default: Some(ref ty), + } = self.kind + { + ty.span().hi() + } else { + hi + }; + mk_sp(lo, max(hi, ty_hi)) + } +} + impl Spanned for ast::StructField { fn span(&self) -> Span { span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi()) @@ -121,60 +151,48 @@ fn span(&self) -> Span { } } -impl Spanned for ast::FunctionRetTy { +impl Spanned for ast::FnRetTy { fn span(&self) -> Span { match *self { - ast::FunctionRetTy::Default(span) => span, - ast::FunctionRetTy::Ty(ref ty) => ty.span, + ast::FnRetTy::Default(span) => span, + ast::FnRetTy::Ty(ref ty) => ty.span, } } } -impl Spanned for ast::TyParam { +impl Spanned for ast::GenericArg { fn span(&self) -> Span { - // Note that ty.span is the span for ty.ident, not the whole item. - let lo = if self.attrs.is_empty() { - self.span.lo() - } else { - self.attrs[0].span.lo() - }; - if let Some(ref def) = self.default { - return mk_sp(lo, def.span.hi()); - } - if self.bounds.is_empty() { - return mk_sp(lo, self.span.hi()); + match *self { + ast::GenericArg::Lifetime(ref lt) => lt.ident.span, + ast::GenericArg::Type(ref ty) => ty.span(), + ast::GenericArg::Const(ref _const) => _const.value.span(), } - let hi = self.bounds[self.bounds.len() - 1].span().hi(); - mk_sp(lo, hi) } } -impl Spanned for ast::TyParamBound { +impl Spanned for ast::GenericBound { fn span(&self) -> Span { match *self { - ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span, - ast::TyParamBound::RegionTyParamBound(ref l) => l.span, + ast::GenericBound::Trait(ref ptr, _) => ptr.span, + ast::GenericBound::Outlives(ref l) => l.ident.span, } } } -impl Spanned for ast::LifetimeDef { - fn span(&self) -> Span { - let hi = if self.bounds.is_empty() { - self.lifetime.span.hi() - } else { - self.bounds[self.bounds.len() - 1].span.hi() - }; - mk_sp(self.lifetime.span.lo(), hi) - } -} - impl Spanned for MacroArg { fn span(&self) -> Span { match *self { MacroArg::Expr(ref expr) => expr.span(), MacroArg::Ty(ref ty) => ty.span(), MacroArg::Pat(ref pat) => pat.span(), + MacroArg::Item(ref item) => item.span(), + MacroArg::Keyword(_, span) => span, } } } + +impl Spanned for ast::NestedMetaItem { + fn span(&self) -> Span { + self.span() + } +}