mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-15 05:26:47 +00:00
Add support for the default
keyword (#1025)
Adds support for Defaultness on impl methods. Fixes #945
This commit is contained in:
parent
d19844d492
commit
b6263735b1
@ -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,
|
||||
@ -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,
|
||||
@ -187,6 +189,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
generics,
|
||||
unsafety,
|
||||
constness,
|
||||
defaultness,
|
||||
abi,
|
||||
vis,
|
||||
span,
|
||||
@ -231,6 +234,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
&sig.generics,
|
||||
sig.unsafety,
|
||||
sig.constness,
|
||||
ast::Defaultness::Final,
|
||||
sig.abi,
|
||||
&ast::Visibility::Inherited,
|
||||
span,
|
||||
@ -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,
|
||||
@ -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 ");
|
||||
}
|
||||
|
@ -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) => {
|
||||
@ -151,6 +152,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
generics,
|
||||
unsafety,
|
||||
constness,
|
||||
defaultness,
|
||||
abi,
|
||||
vis,
|
||||
codemap::mk_sp(s.lo, b.span.lo),
|
||||
@ -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),
|
||||
@ -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(),
|
||||
@ -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,
|
||||
@ -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",
|
||||
|
5
tests/source/issue-945.rs
Normal file
5
tests/source/issue-945.rs
Normal file
@ -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
tests/target/issue-945.rs
Normal file
17
tests/target/issue-945.rs
Normal file
@ -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"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user