From 5cdbad4950bc02de7e40eb8abb0fe134b1353e4c Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Tue, 6 Jan 2015 23:14:00 -0500 Subject: [PATCH 1/4] Added lint for structs with zero fields in FFI blocks --- src/librustc/lint/builtin.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 425e34cd9f042..d9e93f34bdffa 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -426,6 +426,24 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { representation annotation in foreign module, consider \ adding a #[repr(...)] attribute to the type"); } + + // FIXME: DO THIS CORRECTLY + if let ty_struct(...) = tty.sty { + let ty_fields = ty::ty_to_def_id(tty).map(|ty_id| { + ty::lookup_struct_fields(self.cx.tcx, ty_id) + }); + match ty_fields { + Some(ty_fields_really) => { + if ty_fields_really.is_empty() { + self.cx.span_lint(IMPROPER_CTYPES, sp, + "found struct with zero fields in foreign \ + module, invalid C struct unless GCC extensions \ + enabled"); + } + }, + _ => unreachable!("struct should have fields!"); + } + } } _ => () } From 58bf85058519d36fc2e9f1d2c5def5bfa3ab87f4 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Wed, 7 Jan 2015 02:41:42 -0500 Subject: [PATCH 2/4] C lint fixed and building correctly --- src/librustc/lint/builtin.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index d9e93f34bdffa..8aaeab51d3c20 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -427,8 +427,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { adding a #[repr(...)] attribute to the type"); } - // FIXME: DO THIS CORRECTLY - if let ty_struct(...) = tty.sty { + if let ty::ty_struct(..) = tty.sty { let ty_fields = ty::ty_to_def_id(tty).map(|ty_id| { ty::lookup_struct_fields(self.cx.tcx, ty_id) }); @@ -436,12 +435,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { Some(ty_fields_really) => { if ty_fields_really.is_empty() { self.cx.span_lint(IMPROPER_CTYPES, sp, - "found struct with zero fields in foreign \ - module, invalid C struct unless GCC extensions \ - enabled"); + "found unit struct in use in foreign \ + module, which is invalid in standard C"); } }, - _ => unreachable!("struct should have fields!"); + _ => unreachable!("struct should have fields!") } } } From 85e8fe3374e6891372a837567f0663dfcead9142 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Tue, 6 Jan 2015 23:14:00 -0500 Subject: [PATCH 3/4] Added lint for structs with zero fields in FFI blocks --- src/librustc/lint/builtin.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 1af8e2f29ebd7..cfbeed5f1564b 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -426,6 +426,24 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { representation annotation in foreign module, consider \ adding a #[repr(...)] attribute to the type"); } + + // FIXME: DO THIS CORRECTLY + if let ty_struct(...) = tty.sty { + let ty_fields = ty::ty_to_def_id(tty).map(|ty_id| { + ty::lookup_struct_fields(self.cx.tcx, ty_id) + }); + match ty_fields { + Some(ty_fields_really) => { + if ty_fields_really.is_empty() { + self.cx.span_lint(IMPROPER_CTYPES, sp, + "found struct with zero fields in foreign \ + module, invalid C struct unless GCC extensions \ + enabled"); + } + }, + _ => unreachable!("struct should have fields!"); + } + } } _ => () } From 72d54f3125bf836b54fa70229ebf22430a9c920c Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Wed, 7 Jan 2015 02:41:42 -0500 Subject: [PATCH 4/4] C lint fixed and building correctly --- src/librustc/lint/builtin.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index cfbeed5f1564b..eeba5650e543a 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -427,8 +427,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { adding a #[repr(...)] attribute to the type"); } - // FIXME: DO THIS CORRECTLY - if let ty_struct(...) = tty.sty { + if let ty::ty_struct(..) = tty.sty { let ty_fields = ty::ty_to_def_id(tty).map(|ty_id| { ty::lookup_struct_fields(self.cx.tcx, ty_id) }); @@ -436,12 +435,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { Some(ty_fields_really) => { if ty_fields_really.is_empty() { self.cx.span_lint(IMPROPER_CTYPES, sp, - "found struct with zero fields in foreign \ - module, invalid C struct unless GCC extensions \ - enabled"); + "found unit struct in use in foreign \ + module, which is invalid in standard C"); } }, - _ => unreachable!("struct should have fields!"); + _ => unreachable!("struct should have fields!") } } }