Skip to content

Commit

Permalink
Add support for the default keyword (#1025)
Browse files Browse the repository at this point in the history
Adds support for Defaultness on impl methods.
Fixes #945
  • Loading branch information
lqd authored and marcusklaas committed May 31, 2016
1 parent d19844d commit b626373
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl<'a> FmtVisitor<'a> {
generics,
ast::Unsafety::Normal,
ast::Constness::NotConst,
ast::Defaultness::Final,
// These are not actually rust functions,
// but we format them as such.
abi::Abi::Rust,
Expand Down Expand Up @@ -168,6 +169,7 @@ impl<'a> FmtVisitor<'a> {
generics: &ast::Generics,
unsafety: ast::Unsafety,
constness: ast::Constness,
defaultness: ast::Defaultness,
abi: abi::Abi,
vis: &ast::Visibility,
span: Span,
Expand All @@ -187,6 +189,7 @@ impl<'a> FmtVisitor<'a> {
generics,
unsafety,
constness,
defaultness,
abi,
vis,
span,
Expand Down Expand Up @@ -231,6 +234,7 @@ impl<'a> FmtVisitor<'a> {
&sig.generics,
sig.unsafety,
sig.constness,
ast::Defaultness::Final,
sig.abi,
&ast::Visibility::Inherited,
span,
Expand Down Expand Up @@ -1220,6 +1224,7 @@ fn rewrite_fn_base(context: &RewriteContext,
generics: &ast::Generics,
unsafety: ast::Unsafety,
constness: ast::Constness,
defaultness: ast::Defaultness,
abi: abi::Abi,
vis: &ast::Visibility,
span: Span,
Expand All @@ -1236,6 +1241,10 @@ fn rewrite_fn_base(context: &RewriteContext,
// Vis unsafety abi.
result.push_str(&*format_visibility(vis));

if let ast::Defaultness::Default = defaultness {
result.push_str("default ");
}

if let ast::Constness::Const = constness {
result.push_str("const ");
}
Expand Down
14 changes: 10 additions & 4 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl<'a> FmtVisitor<'a> {
fd: &ast::FnDecl,
b: &ast::Block,
s: Span,
_: ast::NodeId) {
_: ast::NodeId,
defaultness: ast::Defaultness) {
let indent = self.block_indent;
let rewrite = match fk {
visit::FnKind::ItemFn(ident, ref generics, unsafety, constness, abi, vis) => {
Expand All @@ -151,6 +152,7 @@ impl<'a> FmtVisitor<'a> {
generics,
unsafety,
constness,
defaultness,
abi,
vis,
codemap::mk_sp(s.lo, b.span.lo),
Expand All @@ -163,6 +165,7 @@ impl<'a> FmtVisitor<'a> {
&sig.generics,
sig.unsafety,
sig.constness,
defaultness,
sig.abi,
vis.unwrap_or(&ast::Visibility::Inherited),
codemap::mk_sp(s.lo, b.span.lo),
Expand Down Expand Up @@ -332,7 +335,8 @@ impl<'a> FmtVisitor<'a> {
decl,
body,
item.span,
item.id)
item.id,
ast::Defaultness::Final)
}
ast::ItemKind::Ty(ref ty, ref generics) => {
let rewrite = rewrite_type_alias(&self.get_context(),
Expand Down Expand Up @@ -373,7 +377,8 @@ impl<'a> FmtVisitor<'a> {
&sig.decl,
&body,
ti.span,
ti.id);
ti.id,
ast::Defaultness::Final);
}
ast::TraitItemKind::Type(ref type_param_bounds, _) => {
let rewrite = rewrite_associated_type(ti.ident,
Expand All @@ -397,7 +402,8 @@ impl<'a> FmtVisitor<'a> {
&sig.decl,
body,
ii.span,
ii.id);
ii.id,
ii.defaultness);
}
ast::ImplItemKind::Const(ref ty, ref expr) => {
let rewrite = rewrite_static("const",
Expand Down
5 changes: 5 additions & 0 deletions tests/source/issue-945.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Bar { default const unsafe fn foo() { "hi" } }

impl Baz { default unsafe extern "C" fn foo() { "hi" } }

impl Foo for Bar { default fn foo() { "hi" } }
17 changes: 17 additions & 0 deletions tests/target/issue-945.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Bar {
default const unsafe fn foo() {
"hi"
}
}

impl Baz {
default unsafe extern "C" fn foo() {
"hi"
}
}

impl Foo for Bar {
default fn foo() {
"hi"
}
}

0 comments on commit b626373

Please sign in to comment.