Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

illegal declarations should give error at declaration time, not later at instantiation time #3011

Open
arnetheduck opened this issue Jun 28, 2015 · 8 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Jun 28, 2015

In the following snippet, context on line 3 has a bad type, but instead the compiler complains that line 9 is wrong: test.nim(9, 18) Error: invalid type: 'object' in this context: 'proc (): Value' - this is obviously very confusing! if Value is not used in the function declaration, no error at all is given!

type
  Type = ref object 
    context: ref object

type
  Value = ref object
    typ: Type

proc x(): Value =
  nil
@Araq
Copy link
Member

Araq commented Jun 29, 2015

Yeah we know, duplicate of some other bug.

@Araq
Copy link
Member

Araq commented Jul 1, 2015

BigBoneDaddy: Nope.

@rayman22201
Copy link
Contributor

Bug #3011 maybe? I tried to fix #3011 but it's nasty... It's not clear what is allowed and what not until it's used as a parameter/variable/etc.

@Araq, @timotheecour,

As Araq points out, it seems the way the compiler is designed makes this difficult to catch at declaration time.

As a compromise, maybe it's ok to have it detected at instantiation time, but create a better error message with more context information / better line numbers?
Similar to what I did in the PR: #10354

This is not perfect, because a bad type declaration that is never used will never be caught, but that seems much less harmful than a bad type declaration that is used but the programmer is unable to debug.

@timotheecour timotheecour changed the title Error points to wrong line for member error illegal declarations should give error at declaration time, not later at instantiation time Jan 24, 2019
@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
@ringabout
Copy link
Member

ringabout commented Nov 1, 2020

Now it is even worse. It compiles since Nim v1.0

@stale stale bot removed the stale Staled PR/issues; remove the label after fixing them label Nov 1, 2020
@bung87
Copy link
Collaborator

bung87 commented Sep 18, 2022

it compiles and the proc x return type is void.
Nim Compiler Version 1.7.1

@bung87
Copy link
Collaborator

bung87 commented Aug 28, 2023

!nim c

type
  Type = ref object 
    context: ref object

type
  Value = ref object
    typ: Type

proc x(): Value =
  nil

@github-actions
Copy link
Contributor

@bung87 (collaborator)

devel 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:41:43
  • Finished 2023-08-28T05:41:44
  • Duration
stable 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:41:44
  • Finished 2023-08-28T05:41:45
  • Duration
2.0.0 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:41:45
  • Finished 2023-08-28T05:41:46
  • Duration
1.6.0 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:41:48
  • Finished 2023-08-28T05:41:48
  • Duration
1.4.0 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:41:51
  • Finished 2023-08-28T05:41:51
  • Duration
1.2.0 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:42:05
  • Finished 2023-08-28T05:42:06
  • Duration
1.0.0 👍 $\color{green}\textbf{\large OK}$

Output


Stats

  • Started 2023-08-28T05:42:19
  • Finished 2023-08-28T05:42:19
  • Duration
0.20.2 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(7, 7) Error: invalid type: 'object' in this context: 'proc (): Value' for proc

Stats

  • Started 2023-08-28T05:42:28
  • Finished 2023-08-28T05:42:28
  • Duration

IR

Compiled filesize 81.94 Kb (83,904 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
	nimGC_setStackBottom(locals);
}
void PreMainInner(void) {
}
int cmdCount;
char** cmdLine;
char** gEnv;
void PreMain(void) {
	void (*volatile inner)(void);
	inner = PreMainInner;
	systemDatInit000();
	initStackBottomWith((void *)&inner);
	systemInit000();
	(*inner)();
}
N_CDECL(void, NimMainInner)(void) {
	NimMainModule();
}
N_CDECL(void, NimMain)(void) {
	void (*volatile inner)(void);
	PreMain();
	inner = NimMainInner;
	initStackBottomWith((void *)&inner);
	(*inner)();
}
int main(int argc, char** args, char** env) {
	cmdLine = args;
	cmdCount = argc;
	gEnv = env;
	NimMain();
	return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

AST

nnkStmtList.newTree(
  nnkTypeSection.newTree(
    nnkTypeDef.newTree(
      newIdentNode("Type"),
      newEmptyNode(),
      nnkRefTy.newTree(
        nnkObjectTy.newTree(
          newEmptyNode(),
          newEmptyNode(),
          nnkRecList.newTree(
            nnkIdentDefs.newTree(
              newIdentNode("context"),
              nnkRefTy.newTree(
                nnkObjectTy.newTree(
                )
              ),
              newEmptyNode()
            )
          )
        )
      )
    )
  ),
  nnkTypeSection.newTree(
    nnkTypeDef.newTree(
      newIdentNode("Value"),
      newEmptyNode(),
      nnkRefTy.newTree(
        nnkObjectTy.newTree(
          newEmptyNode(),
          newEmptyNode(),
          nnkRecList.newTree(
            nnkIdentDefs.newTree(
              newIdentNode("typ"),
              newIdentNode("Type"),
              newEmptyNode()
            )
          )
        )
      )
    )
  ),
  nnkProcDef.newTree(
    newIdentNode("x"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("Value")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newNilLit()
    )
  )
)
??? ➡️ 🐛

Diagnostics

The commit that introduced the bug can not be found, but the bug is in the commits:

(Can not find the commit because Nim can not be re-built commit-by-commit to bisect).

Stats
  • GCC 11.4.0
  • LibC 2.35
  • Valgrind 3.18.1
  • NodeJS 17.1
  • Linux 5.15.0
  • Created 2023-08-28T05:41:12Z
  • Comments 7
  • Commands nim c --run -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

🤖 Bug found in 44 minutes bisecting 232 commits at 5 commits per second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
@dom96 @Araq @rayman22201 @arnetheduck @bung87 @ringabout and others