]> git.lizzy.rs Git - rust.git/blobdiff - src/spanned.rs
Merge commit 'c4416f20dcaec5d93077f72470e83e150fb923b1' into sync-rustfmt
[rust.git] / src / spanned.rs
index 504d18fb7fd1752729af5b226100a1a825af3844..2136cfeae1af107592aabbdf92d4aef98d104835 100644 (file)
@@ -1,22 +1,13 @@
-// 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 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use syntax::{ast, ptr, source_map::Span};
+use std::cmp::max;
 
-use macros::MacroArg;
-use utils::{mk_sp, outer_attributes};
+use rustc_ast::{ast, ptr};
+use rustc_span::{source_map, Span};
 
-use std::cmp::max;
+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;
 }
 
@@ -26,6 +17,12 @@ fn span(&self) -> Span {
     }
 }
 
+impl<T> Spanned for source_map::Spanned<T> {
+    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);
@@ -54,30 +51,29 @@ fn span(&self) -> Span {
 }
 
 // Implement `Spanned` for structs with `attrs` field.
+implement_spanned!(ast::AssocItem);
 implement_spanned!(ast::Expr);
-implement_spanned!(ast::Field);
+implement_spanned!(ast::ExprField);
 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,
         }
     }
 }
@@ -97,7 +93,7 @@ fn span(&self) -> Span {
 impl Spanned for ast::Arm {
     fn span(&self) -> Span {
         let lo = if self.attrs.is_empty() {
-            self.pats[0].span.lo()
+            self.pat.span.lo()
         } else {
             self.attrs[0].span.lo()
         };
@@ -105,10 +101,10 @@ fn span(&self) -> Span {
     }
 }
 
-impl Spanned for ast::Arg {
+impl Spanned for ast::Param {
     fn span(&self) -> Span {
-        if ::items::is_named_arg(self) {
-            mk_sp(self.pat.span.lo(), self.ty.span.hi())
+        if crate::items::is_named_param(self) {
+            mk_sp(crate::items::span_lo_for_param(self), self.ty.span.hi())
         } else {
             self.ty.span
         }
@@ -117,10 +113,10 @@ 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 lo = match self.kind {
+            _ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
+            ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
+            _ => self.ident.span.lo(),
         };
         let hi = if self.bounds.is_empty() {
             self.ident.span.hi()
@@ -129,7 +125,8 @@ fn span(&self) -> Span {
         };
         let ty_hi = if let ast::GenericParamKind::Type {
             default: Some(ref ty),
-        } = self.kind
+        }
+        | ast::GenericParamKind::Const { ref ty, .. } = self.kind
         {
             ty.span().hi()
         } else {
@@ -139,7 +136,7 @@ fn span(&self) -> Span {
     }
 }
 
-impl Spanned for ast::StructField {
+impl Spanned for ast::FieldDef {
     fn span(&self) -> Span {
         span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
     }
@@ -155,11 +152,11 @@ 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,
         }
     }
 }
@@ -169,6 +166,7 @@ fn span(&self) -> Span {
         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(),
         }
     }
 }
@@ -189,6 +187,13 @@ fn span(&self) -> 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()
+    }
+}