Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ script:
(cd diesel && travis-cargo test -- --no-default-features --features "extras with-deprecated $BACKEND") &&
(cd diesel_derives && travis-cargo test -- --features "diesel/$BACKEND") &&
(cd diesel_derives2 && travis-cargo test -- --features "$BACKEND") &&
if [[ "$BACKEND" == postgres ]]; then
(cd examples/postgres && ./test_all)
fi &&
if [[ "$BACKEND" == mysql ]]; then
(cd examples/mysql && ./test_all)
fi &&
if [[ "$BACKEND" == sqlite ]]; then
(cd examples/sqlite && ./test_all)
if [[ "$TRAVIS_RUST_VERSION" == nightly* ]]; then
(cd diesel_derives2 && travis-cargo test -- --features "nightly $BACKEND")
fi &&
(cd "examples/$BACKEND" && ./test_all) &&
(cd diesel_cli && travis-cargo test -- --no-default-features --features "$BACKEND") &&
(cd diesel_infer_schema/infer_schema_internals && travis-cargo test -- --no-default-features --features "$BACKEND") &&
(cd diesel_infer_schema/infer_schema_macros && travis-cargo test -- --no-default-features --features "$BACKEND") &&
Expand Down
2 changes: 1 addition & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tempdir = "^0.3.4"
[features]
default = ["with-deprecated"]
extras = ["chrono", "serde_json", "uuid", "deprecated-time", "network-address", "numeric", "r2d2"]
unstable = []
unstable = ["diesel_derives2/nightly"]
lint = ["clippy"]
large-tables = []
huge-tables = ["large-tables"]
Expand Down
20 changes: 20 additions & 0 deletions diesel_compile_tests/tests/ui/as_changeset_bad_column_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[macro_use]
extern crate diesel;

table! {
users {
id -> Integer,
}
}

#[derive(AsChangeset)]
#[table_name = "users"]
struct UserStruct {
name: String,
#[column_name = "hair_color"]
color_de_pelo: String,
}

#[derive(AsChangeset)]
#[table_name = "users"]
struct UserTuple(#[column_name = "name"] String);
40 changes: 40 additions & 0 deletions diesel_compile_tests/tests/ui/as_changeset_bad_column_name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error[E0412]: cannot find type `name` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:13:5
|
13 | name: String,
| ^^^^ not found in `users`

error[E0412]: cannot find type `hair_color` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:14:5
|
14 | #[column_name = "hair_color"]
| ^ not found in `users`

error[E0425]: cannot find value `name` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:13:5
|
13 | name: String,
| ^^^^ not found in `users`

error[E0425]: cannot find value `hair_color` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:14:5
|
14 | #[column_name = "hair_color"]
| ^ not found in `users`

error[E0412]: cannot find type `name` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:20:18
|
20 | struct UserTuple(#[column_name = "name"] String);
| ^ not found in `users`

error[E0425]: cannot find value `name` in module `users`
--> $DIR/as_changeset_bad_column_name.rs:20:18
|
20 | struct UserTuple(#[column_name = "name"] String);
| ^ not found in `users`

error[E0601]: main function not found

error: aborting due to 7 previous errors

17 changes: 17 additions & 0 deletions diesel_compile_tests/tests/ui/as_changeset_missing_table_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[macro_use]
extern crate diesel;

#[derive(AsChangeset)]
struct User {
id: i32,
name: String,
}

#[derive(AsChangeset)]
#[table_name = "users"]
struct UserForm {
id: i32,
name: String,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0433]: failed to resolve. Use of undeclared type or module `users`
--> $DIR/as_changeset_missing_table_import.rs:5:8
|
5 | struct User {
| ^^^^ Use of undeclared type or module `users`

error[E0433]: failed to resolve. Use of undeclared type or module `users`
--> <macro expansion>:1:1
|
1 | #[table_name = "users"]
| ^ Use of undeclared type or module `users`

error: aborting due to 2 previous errors

48 changes: 48 additions & 0 deletions diesel_compile_tests/tests/ui/update-references.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
#
# Copyright 2015 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.

# A script to update the references for particular tests. The idea is
# that you do a run, which will generate files in the build directory
# containing the (normalized) actual output of the compiler. This
# script will then copy that output and replace the "expected output"
# files. You can then commit the changes.
#
# If you find yourself manually editing a foo.stderr file, you're
# doing it wrong.

if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" == "" ]]; then
echo "usage: $0 <build-directory> <relative-path-to-rs-files>"
echo ""
echo "For example:"
echo " $0 ../../../build/x86_64-apple-darwin/test/ui *.rs */*.rs"
fi

MYDIR=$(dirname $0)

BUILD_DIR="$1"
shift

while [[ "$1" != "" ]]; do
STDERR_NAME="${1/%.rs/.stderr}"
STDOUT_NAME="${1/%.rs/.stdout}"
shift
if [ -f $BUILD_DIR/$STDOUT_NAME ] && \
! (diff $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME >& /dev/null); then
echo updating $MYDIR/$STDOUT_NAME
cp $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME
fi
if [ -f $BUILD_DIR/$STDERR_NAME ] && \
! (diff $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME >& /dev/null); then
echo updating $MYDIR/$STDERR_NAME
cp $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME
fi
done
2 changes: 2 additions & 0 deletions diesel_derives2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/diesel-rs/diesel/tree/master/diesel_derives"
syn = { version = "0.12.0", features = ["full"] }
quote = "0.4"
clippy = { optional = true, version = "=0.0.174" }
proc-macro2 = "0.2.0"

[dev-dependencies]
cfg-if = "0.1.0"
Expand All @@ -28,6 +29,7 @@ name = "tests"
[features]
default = []
lint = ["clippy"]
nightly = ["proc-macro2/nightly"]
postgres = ["diesel/postgres", "diesel_migrations/postgres"]
sqlite = ["diesel/sqlite", "diesel_migrations/sqlite"]
mysql = ["diesel/mysql", "diesel_migrations/mysql"]
Expand Down
14 changes: 7 additions & 7 deletions diesel_derives2/src/as_changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ pub fn derive(item: syn::DeriveInput) -> quote::Tokens {
);
}

wrap_in_dummy_const(
model.dummy_const_name("AS_CHANGESET"),
wrap_in_dummy_mod(
model.dummy_mod_name("as_changeset"),
quote!(
use diesel::query_builder::AsChangeset;
use self::diesel::query_builder::AsChangeset;

impl #impl_generics AsChangeset for &'update #struct_name #ty_generics
#where_clause
Expand All @@ -70,7 +70,7 @@ fn field_changeset_ty(
let column_name = field.column_name();
if !treat_none_as_null && is_option_ty(&field.ty) {
let field_ty = inner_of_option_ty(&field.ty);
parse_quote!(::std::option::Option<diesel::dsl::Eq<#table_name::#column_name, &'update #field_ty>>)
parse_quote!(std::option::Option<diesel::dsl::Eq<#table_name::#column_name, &'update #field_ty>>)
} else {
let field_ty = &field.ty;
parse_quote!(diesel::dsl::Eq<#table_name::#column_name, &'update #field_ty>)
Expand All @@ -82,11 +82,11 @@ fn field_changeset_expr(
table_name: syn::Ident,
treat_none_as_null: bool,
) -> syn::Expr {
let field_name = &field.name;
let field_access = &field.name;
let column_name = field.column_name();
if !treat_none_as_null && is_option_ty(&field.ty) {
parse_quote!(self.#field_name.as_ref().map(|x| #table_name::#column_name.eq(x)))
parse_quote!(self#field_access.as_ref().map(|x| #table_name::#column_name.eq(x)))
} else {
parse_quote!(#table_name::#column_name.eq(&self.#field_name))
parse_quote!(#table_name::#column_name.eq(&self#field_access))
}
}
16 changes: 14 additions & 2 deletions diesel_derives2/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use syn;
use proc_macro2::Span;
use quote;
use syn;

use meta::*;

Expand All @@ -16,7 +17,11 @@ impl Field {
.map(|m| m.expect_ident_value());
let name = match field.ident {
Some(x) => FieldName::Named(x),
None => FieldName::Unnamed(index.into()),
None => FieldName::Unnamed(syn::Index {
index: index as u32,
// https://github.com/rust-lang/rust/issues/47312
span: Span::call_site(),
}),
};

Self {
Expand All @@ -42,6 +47,13 @@ pub enum FieldName {

impl quote::ToTokens for FieldName {
fn to_tokens(&self, tokens: &mut quote::Tokens) {
use proc_macro2::{Spacing, TokenNode, TokenTree};

// https://github.com/rust-lang/rust/issues/47312
tokens.append(TokenTree {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quote_spanned! can make this more concise but it is up to you whether you prefer the explicit way or the concise way.

quote_spanned!(call_site=> .)

span: Span::call_site(),
kind: TokenNode::Op('.', Spacing::Alone),
});
match *self {
FieldName::Named(x) => x.to_tokens(tokens),
FieldName::Unnamed(ref x) => x.to_tokens(tokens),
Expand Down
2 changes: 2 additions & 0 deletions diesel_derives2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![recursion_limit = "1024"]
// Built-in Lints
#![deny(warnings, missing_copy_implementations)]
// Clippy lints
Expand All @@ -9,6 +10,7 @@
warn(wrong_pub_self_convention, mut_mut, non_ascii_literal, similar_names,
unicode_not_nfc, if_not_else, items_after_statements, used_underscore_binding))]

extern crate proc_macro2;
extern crate proc_macro;
#[macro_use]
extern crate quote;
Expand Down
18 changes: 11 additions & 7 deletions diesel_derives2/src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use proc_macro2::Span;
use syn;

pub struct MetaItem {
// Due to https://github.com/rust-lang/rust/issues/47941
// we can only ever get the span of the #, which is better than nothing
span: Span,
meta: syn::Meta,
}

Expand All @@ -11,9 +15,9 @@ impl MetaItem {
) -> Result<Self, MissingOption<'a>> {
attrs
.iter()
.filter_map(syn::Attribute::interpret_meta)
.find(|m| m.name() == name)
.map(|meta| Self { meta })
.filter_map(|attr| attr.interpret_meta().map(|m| (attr.pound_token.0[0], m)))
.find(|&(_, ref m)| m.name() == name)
.map(|(span, meta)| Self { span, meta })
.ok_or(MissingOption { name })
}

Expand All @@ -40,7 +44,7 @@ impl MetaItem {
);
x
}
_ => self.expect_str_value().into(),
_ => syn::Ident::new(&self.expect_str_value(), self.span.resolved_at(Span::call_site())),
}
}

Expand Down Expand Up @@ -69,7 +73,7 @@ impl MetaItem {
use syn::Meta::*;

match self.meta {
List(ref list) => Some(Nested(list.nested.iter())),
List(ref list) => Some(Nested(list.nested.iter(), self.span)),
_ => None,
}
}
Expand Down Expand Up @@ -112,7 +116,7 @@ impl<'a> RequiredOption<MetaItem> for Result<MetaItem, MissingOption<'a>> {
}

#[cfg_attr(rustfmt, rustfmt_skip)] // https://github.com/rust-lang-nursery/rustfmt/issues/2392
pub struct Nested<'a>(syn::punctuated::Iter<'a, syn::NestedMeta, Token![,]>);
pub struct Nested<'a>(syn::punctuated::Iter<'a, syn::NestedMeta, Token![,]>, Span);

impl<'a> Iterator for Nested<'a> {
type Item = MetaItem;
Expand All @@ -121,7 +125,7 @@ impl<'a> Iterator for Nested<'a> {
use syn::NestedMeta::*;

match self.0.next() {
Some(&Meta(ref item)) => Some(MetaItem { meta: item.clone() }),
Some(&Meta(ref item)) => Some(MetaItem { span: self.1, meta: item.clone() }),
Some(_) => self.next(),
None => None,
}
Expand Down
15 changes: 10 additions & 5 deletions diesel_derives2/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use syn;
use proc_macro2::Span;

use field::*;
use meta::*;
Expand Down Expand Up @@ -28,13 +29,17 @@ impl Model {
}

pub fn table_name(&self) -> syn::Ident {
self.table_name_from_attribute
.unwrap_or_else(|| infer_table_name(self.name.as_ref()).into())
self.table_name_from_attribute.unwrap_or_else(|| {
syn::Ident::new(
&infer_table_name(self.name.as_ref()),
self.name.span.resolved_at(Span::call_site()),
)
})
}

pub fn dummy_const_name(&self, trait_name: &str) -> syn::Ident {
let name = self.name.as_ref().to_uppercase();
format!("_IMPL_{}_FOR_{}", trait_name, name).into()
pub fn dummy_mod_name(&self, trait_name: &str) -> syn::Ident {
let name = self.name.as_ref().to_lowercase();
format!("_impl_{}_for_{}", trait_name, name).into()
}

pub fn fields(&self) -> &[Field] {
Expand Down
10 changes: 7 additions & 3 deletions diesel_derives2/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use syn::*;
use quote::Tokens;

pub fn wrap_in_dummy_const(const_name: Ident, item: Tokens) -> Tokens {
pub fn wrap_in_dummy_mod(const_name: Ident, item: Tokens) -> Tokens {
quote! {
const #const_name: () = {
mod #const_name {
// https://github.com/rust-lang/rust/issues/47314
extern crate std;

mod diesel {
__diesel_use_everything!();
}
use self::diesel::prelude::*;
#item
};
}
}
}

Expand Down